第一步:自定义 键盘组件:KeyboardView是google官方自己提供的键盘组件
public class CustomKeyboardView extends KeyboardView {http://www.baidu.com/link?url=y_iCppnsVfwmUMOZ-o_9wg2Yb3XRrzZ6FmDQfvZ-S2FctZPKkv9HUz5gvMnc_oDTN_w2vz9qMd8dmprhgqEBhR2aXmoZwb-QAFuu-H7vae889mTGncbEudBn1HksvRYT
}
第二步:xml文件:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:layout_height="wrap_content"
android:text="输入密码:"
android:textColor="@color/Black"
android:textSize="25dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:singleLine="true" />
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:layout_height="wrap_content"
android:text="重复密码:"
android:textColor="@color/Black"
android:textSize="25dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword"
android:singleLine="true" >
android:layout_height="wrap_content"
android:background="@drawable/btn_input_press_1" >
android:id="@+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_input_normal"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/btn_keyboard_key"
android:keyPreviewLayout="@layout/textview"
android:keyTextColor="@color/Black"
android:keyTextSize="20dp"
android:visibility="gone" />
第三部:已定义一个KeyUtil
public class KeyboardUtil {
private Context context;
private Activity activity;
private CustomKeyboardView keyboardView;
private Keyboard ketword;// 字母键盘
private Keyboard keynum, keypun;// 数字键盘
public boolean isnun = false;// 是否数据键盘
public boolean isupper = false;// 是否大写
private EditText ed;
public KeyboardUtil(Activity activity, Context context, EditText edit) {
this.activity = activity;
this.context = context;
this.ed = edit;
//这三个分别是标点符号还有 字母等,是一个存xml文件,
ketword = new Keyboard(context, R.xml.qwerty);
keynum = new Keyboard(context, R.xml.symbols);
keypun = new Keyboard(context, R.xml.punctuation);
keyboardView = (CustomKeyboardView) activity.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(ketword); //设置默认键盘
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) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
changeKey();
keyboardView.setKeyboard(ketword);
} /*
* else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换 //这是官方文档自己内嵌的键值
* if (isnun) { isnun = false; keyboardView.setKeyboard(k1); } else
* { isnun = true; keyboardView.setKeyboard(k2); } }
*/else if (primaryCode == 57419) { // go left
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == 57000) { // go left //这个数字是可以自己定义的
keyboardView.setKeyboard(keynum);
} else if (primaryCode == 57001) { // go left
keyboardView.setKeyboard(ketword);
} else if (primaryCode == 57002) { // go left
keyboardView.setKeyboard(keypun);
}
else if (primaryCode == 57421) { // go right
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};
/**
* 键盘大小写切换
*/
private void changeKey() {
List
if (isupper) {// 大写切换小写
isupper = false;
for (Key key : keylist) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0] + 32;
}
}
} else {// 小写切换大写
isupper = true;
for (Key key : keylist) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0] - 32;
}
}
}
}
//这个就是用于回调当单击了 edittext的时候进行的响应
/*public boolean onTouch(View v, MotionEvent event) {
int type=editsce.getInputType();
editsce.setInputType(InputType.TYPE_NULL);
new KeyboardUtil(activity, context, editsce).showKeyboard();
//editsce.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
editsce.setInputType(type);
return false;
}*/
//显示的时候加载动画,这个可以自己去定义动画的效果
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
Animation animation = AnimationUtils.loadAnimation(activity,
R.anim.slide_in_bottom);
keyboardView.showWithAnimation(animation);
keyboardView.setVisibility(View.VISIBLE);
}
}
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}
private boolean isword(String str) {
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase()) > -1) {
return true;
}
return false;
}
}
第四部:主文件中对edittext单击时候的响应
editsce.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int type=editsce.getInputType();
editsce.setInputType(InputType.TYPE_NULL);
new KeyboardUtil(activity, context, editsce).showKeyboard();
//editsce.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
editsce.setInputType(type);
return false;
}
});
editfirst.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// int inputback = editfirst.getInputType();
editfirst.setInputType(InputType.TYPE_NULL);
new KeyboardUtil(activity, context, editfirst).showKeyboard();
// editfirst.setInputType(inputback);
return false;
}
});
第五步:字符键盘的定义:
android:keyHeight="@dimen/key_height"
android:keyWidth="10.000002%p"
android:verticalGap="0.0px" >
android:keyEdgeFlags="left"
android:keyLabel="q" />
android:keyLabel="w" />
android:keyLabel="e" />
android:keyLabel="r" />
android:keyLabel="t" />
android:keyLabel="y" />
android:keyLabel="u" />
android:keyLabel="i" />
android:keyLabel="o" />
android:keyEdgeFlags="right"
android:keyLabel="p" />
android:horizontalGap="4.999995%p"
android:keyEdgeFlags="left"
android:keyLabel="a" />
android:keyLabel="s" />
android:keyLabel="d" />
android:keyLabel="f" />
android:keyLabel="g" />
android:keyLabel="h" />
android:keyLabel="j" />
android:keyLabel="k" />
android:keyEdgeFlags="right"
android:keyLabel="l" />
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left"
android:keyIcon="@drawable/sym_keyboard_shift"
android:keyWidth="14.999998%p" />
android:keyLabel="z" />
android:keyLabel="x" />
android:keyLabel="c" />
android:keyLabel="v" />
android:keyLabel="b" />
android:keyLabel="n" />
android:keyLabel="m" />
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_delete"
android:keyWidth="14.999998%p" />
android:keyLabel="123"
android:keyWidth="20.000004%p" />
android:keyLabel="符"
android:keyWidth="20.000004%p"
/>
android:keyLabel=","
android:keyWidth="9.999998%p" />
android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_space"
android:keyWidth="20.999996%p" />
android:keyLabel="."
android:keyWidth="9.999998%p" />
android:keyEdgeFlags="right"
android:keyLabel="完成"
android:keyWidth="20.000004%p" />
android:keyHeight="@dimen/key_height"
android:keyWidth="25%p"
android:verticalGap="0px" >
android:keyLabel="1" />
android:keyLabel="2" />
android:keyLabel="3" />
android:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_delete" />
android:keyLabel="4" />
android:keyLabel="5" />
android:keyLabel="6" />
android:keyEdgeFlags="right"
android:keyLabel="符"
/>
android:keyLabel="7" />
android:keyLabel="8" />
android:keyLabel="9" />
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyHeight="100dip"
android:keyLabel="完成" />
android:keyLabel="ABC" />
android:keyLabel="0" />
android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_space"
/>