android 自定义软键盘
哦然间发现了android.inputmethodservice.Keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考,
首先看看效果图:
键盘内容布局:keycontent.xml
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="25%p" android:horizontalGap="0px" android:verticalGap="0px" android:keyHeight="50dip"> <Row> <Key android:codes="49" android:keyLabel="1" /> <Key android:codes="50" android:keyLabel="2" /> <Key android:codes="51" android:keyLabel="3" /> <Key android:codes="57419" android:keyEdgeFlags="right" android:keyIcon="@drawable/keyboard_capslock" /> </Row> <Row> <Key android:codes="52" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyLabel="6" /> <Key android:codes="57421" android:keyEdgeFlags="right" android:keyIcon="@drawable/keyboard_big_capslock" /> </Row> <Row> <Key android:codes="55" android:keyLabel="7" /> <Key android:codes="56" android:keyLabel="8" /> <Key android:codes="57" android:keyLabel="9" /> <Key android:codes="-5" android:keyEdgeFlags="right" android:isRepeatable="true" android:keyLabel="delete" android:keyIcon="@drawable/keyboard_delete" /> </Row> <Row> <Key android:codes="-3" android:keyIcon="@drawable/keyboard_return" /> <Key android:codes="48" android:keyLabel="0" /> <Key android:codes="88" android:keyLabel="X" /> </Row> </Keyboard>
键盘布局 keyboardview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <android.inputmethodservice.KeyboardView android:id="@+id/keyboard_view" android:visibility="gone" android:focusable="true" android:focusableInTouchMode="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout>
package com.pioneersoft.temp; import android.app.Activity; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.inputmethodservice.KeyboardView.OnKeyboardActionListener; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; public class KeyboardActivity extends Activity{ EditText edit; KeyboardView keyboardView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.keyboardview); edit = (EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL); edit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showKeyboard(); } }); keyboardView = (KeyboardView)findViewById(R.id.keyboard_view); keyboardView.setKeyboard(new Keyboard(this, R.layout.keycontent)); keyboardView.setEnabled(true); keyboardView.setPreviewEnabled(true); keyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener() { @Override public void onKey(int primaryCode, int[] keyCodes) { Editable editable = edit.getText(); int start = edit.getSelectionStart(); if (primaryCode == Keyboard.KEYCODE_CANCEL) { hideKeyboard(); } else if (primaryCode == Keyboard.KEYCODE_DELETE) { if (editable != null && editable.length() > 0) { editable.delete(start - 1, start); } } else if (primaryCode == 57419) { // go left if (start > 0) { edit.setSelection(start - 1); } } else if (primaryCode == 57421) { // go right if (start < edit.length()) { edit.setSelection(start + 1); } } else { editable.insert(start, Character.toString((char)primaryCode)); } } @Override public void onPress(int primaryCode) { // TODO Auto-generated method stub } @Override public void onRelease(int primaryCode) { // TODO Auto-generated method stub } @Override public void onText(CharSequence text) { // TODO Auto-generated method stub } @Override public void swipeDown() { // TODO Auto-generated method stub } @Override public void swipeLeft() { // TODO Auto-generated method stub } @Override public void swipeRight() { // TODO Auto-generated method stub } @Override public void swipeUp() { // TODO Auto-generated method stub } }); } private void showKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.GONE || visibility == View.INVISIBLE) { keyboardView.setVisibility(View.VISIBLE); } } private void hideKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.VISIBLE) { keyboardView.setVisibility(View.INVISIBLE); } } }
以上就是自定义键盘布局的内容,如需详细内容需要参考api。