自定义键盘实现

来源:互联网 发布:大型分析仪器仿真软件 编辑:程序博客网 时间:2024/05/10 02:50
#1 效果图(多种键盘视图,请看完gif)
ss
ss
ss
#2 步骤(只显示关键代码,具体实现在示例代码中)
1.屏蔽系统键盘
@Override
public void onCreate(Bundle savedInstanceState) {
...
if (android.os.Build.VERSION.SDK_INT <= 10) { //解决sdk4.0以上光标不显示问题
etzqdm.setInputType(InputType.TYPE_NULL);
} else {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(etzqdm, false);
} catch (Exception e) {
e.printStackTrace();
}
}
...
}

2. 配置KeyBoard键盘
1)xml中声明

2)res中新建xml文件夹,配置键盘视图(相关xml属性见个人前一篇博客)
一个简单的数字键盘
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0dp"
android:keyHeight="10%p"
android:keyWidth="25%p"
android:verticalGap="0dp">
<Row>
<Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" />
<Key
android:codes="-5"
android:keyHeight="20%p"
android:keyEdgeFlags="right"
android:keyIcon="@drawable/keyboard_delete"
/>
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
</Row>
<Row>
<Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key
android:codes="-4"
android:keyHeight="20%p"
android:keyEdgeFlags="right"
android:keyLabel="买入"
/>
</Row>
<Row>
<Key android:codes="46" android:keyLabel="." android:keyEdgeFlags="left" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="-3" android:keyLabel="收起" />
</Row>
</Keyboard>
效果预览:

3.代码中初始化键盘
private void initKeyboard() {

mKeyboardView.setKeyboard(new Keyboard(this, R.xml.keyboard_number));
mKeyboardView.setEnabled(true);
mKeyboardView.setPreviewEnabled(true);
mKeyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = et.getText();//et为EditText
int start = et.getSelectionStart();
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
break;
case Keyboard.KEYCODE_DONE:
hideKeyboard();
Toast.makeText(NumberboardActivity.this,"买入",Toast.LENGTH_SHORT).show();
break;
case Keyboard.KEYCODE_CANCEL:
hideKeyboard();
break;
default:
String str = Character.toString((char) primaryCode);
editable.insert(start, str);
break;
}
}

public void onPress(int primaryCode) {
}

public void onRelease(int primaryCode) {
}

public void onText(CharSequence text) {
}

public void swipeDown() {
}

public void swipeLeft() {
}

public void swipeRight() {
}

public void swipeUp() {
}
});
}

4. 提供键盘隐藏显示方法,EditText调用
private void hideKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.VISIBLE) {
mKeyboardView.setVisibility(View.GONE);
}
}

private void showKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
mKeyboardView.setVisibility(View.VISIBLE);
}
}

...
et.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
showKeyboard();
}
});
...



#3 示例代码
http://download.csdn.net/detail/baopengjian/9817320

0 0