自定义数字键盘

来源:互联网 发布:netflix 获奖算法 编辑:程序博客网 时间:2024/05/04 01:01

1.KeydemoActivity.java

import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.EditText;


public class KeydemoActivity extends Activity {
private Context ctx;
private Activity act;
private EditText edit;
private EditText edit1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx = this;
act = this;
edit = (EditText) this.findViewById(R.id.edit);
edit1 = (EditText) this.findViewById(R.id.edit1);
edit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
edit.requestFocus();  //请求获取光标焦点
edit.setSelection(edit.getText().toString().length());//每次定位光标到末尾
showKeyBoardView(edit);
return false;
}
});
edit1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
edit1.requestFocus();
edit1.setSelection(edit1.getText().toString().length());
showKeyBoardView(edit1);
return false;
}
});
}
private void showKeyBoardView(EditText edit){
//屏蔽系统键盘
if (android.os.Build.VERSION.SDK_INT <= 10) {
edit.setInputType(InputType.TYPE_NULL);
   } else {
   getWindow().setSoftInputMode(
   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
   try {
   Class<EditText> cls = EditText.class;
   Method setShowSoftInputOnFocus;
   setShowSoftInputOnFocus = cls.getMethod(
   "setShowSoftInputOnFocus", boolean.class);
   setShowSoftInputOnFocus.setAccessible(true);
   setShowSoftInputOnFocus.invoke(edit, false);
   } catch (Exception e) {
   e.printStackTrace();
   }
  }
new KeyboardUtil(act, ctx, edit).showKeyboard();


}
}

2.KeyboardUtil.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;


@SuppressLint("DefaultLocale")
public class KeyboardUtil {
private KeyboardView keyboardView;
private Keyboard k1;// 数字键盘
private EditText ed;
public KeyboardUtil(Activity act, Context ctx, EditText edit) {
this.ed = edit;
k1 = new Keyboard(ctx, R.xml.symbols);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
}


private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
@Override
public void swipeUp() {
}


@Override
public void swipeRight() {
}


@Override
public void swipeLeft() {
}


@Override
public void swipeDown() {
}


@Override
public void onText(CharSequence text) {
}


@Override
public void onRelease(int primaryCode) {
}


@Override
public void onPress(int primaryCode) {
}


@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// enter 
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}

else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};

    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }
    
    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.INVISIBLE);
        }
    }
}

3.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint=""
        android:inputType="text"/>        
    <EditText
        android:id="@+id/edit1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboard_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@color/lightblack"
            android:keyBackground="@drawable/btn_keyboard_key" 
            android:keyTextColor="@color/white"
            android:visibility="gone" />
    </RelativeLayout>
</LinearLayout>

4.symbols.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint=""
        android:inputType="text"/>        
    <EditText
        android:id="@+id/edit1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboard_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@color/lightblack"
            android:keyBackground="@drawable/btn_keyboard_key" 
            android:keyTextColor="@color/white"
            android:visibility="gone" />
    </RelativeLayout>
</LinearLayout>

0 0
原创粉丝点击