参考:
http://blog.csdn.net/hfsu0419/article/details/7924673
布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" /> <RelativeLayout android:layout_width="match_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="@android:color/darker_gray" android:keyTextColor="#00ff00" android:keyPreviewLayout="@layout/keyborad_preview_layout" android:keyPreviewHeight="40dip" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
预览文件keyborad_preview_layout.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="#ffffff" android:background="@drawable/prewlayout_bg" android:gravity="center" android:minHeight="0dip" android:minWidth="0dip" android:textSize="20sp" />
样式
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#ff000000"/> <stroke android:color="#9900f010" android:width="2dip"/> </shape>
键盘布局文件
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:horizontalGap="0.0px" android:keyHeight="50dp" android:keyWidth="10.000002%p" android:verticalGap="0.0px" > <Row> <Key android:codes="49" android:keyEdgeFlags="left" android:keyLabel="1" /> <Key android:codes="50" android:keyLabel="2" /> <Key android:codes="51" android:keyLabel="3" /> <Key android:codes="52" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyLabel="6" /> <Key android:codes="55" android:keyLabel="7" /> <Key android:codes="56" android:keyLabel="8" /> <Key android:codes="57" android:keyLabel="9" /> <Key android:codes="48" android:keyEdgeFlags="right" android:keyLabel="0" /> </Row> <Row> <Key android:codes="113" android:keyEdgeFlags="left" android:keyLabel="q" /> <Key android:codes="119" android:keyLabel="w" /> <Key android:codes="101" android:keyLabel="e" /> <Key android:codes="114" android:keyLabel="r" /> <Key android:codes="116" android:keyLabel="t" /> <Key android:codes="121" android:keyLabel="y" /> <Key android:codes="117" android:keyLabel="u" /> <Key android:codes="105" android:keyLabel="i" /> <Key android:codes="111" android:keyLabel="o" /> <Key android:codes="112" android:keyEdgeFlags="right" android:keyLabel="p" /> </Row> <Row> <Key android:codes="97" android:keyEdgeFlags="left" android:keyLabel="a" /> <Key android:codes="115" android:keyLabel="s" /> <Key android:codes="100" android:keyLabel="d" /> <Key android:codes="102" android:keyLabel="f" /> <Key android:codes="103" android:keyLabel="g" /> <Key android:codes="104" android:keyLabel="h" /> <Key android:codes="106" android:keyLabel="j" /> <Key android:codes="107" android:keyLabel="k" /> <Key android:codes="108" android:keyLabel="l" /> <Key android:codes="42" android:keyEdgeFlags="right" android:keyLabel="@string/at_flag" /> </Row> <Row> <Key android:codes="-1" android:isModifier="true" android:isSticky="true" android:keyEdgeFlags="left" android:keyLabel="大写" android:keyWidth="14.999998%p" /> <Key android:codes="122" android:keyLabel="z" /> <Key android:codes="120" android:keyLabel="x" /> <Key android:codes="99" android:keyLabel="c" /> <Key android:codes="118" android:keyLabel="v" /> <Key android:codes="98" android:keyLabel="b" /> <Key android:codes="110" android:keyLabel="n" /> <Key android:codes="109" android:keyLabel="m" /> <Key android:codes="-5" android:isRepeatable="true" android:keyEdgeFlags="right" android:keyLabel="退格" android:keyWidth="14.999998%p" /> </Row> <Row android:rowEdgeFlags="bottom" > <Key android:codes="44" android:keyEdgeFlags="left" android:keyLabel="," android:keyWidth="25%p" /> <Key android:codes="32" android:isRepeatable="true" android:keyLabel="空格" android:keyWidth="25%p" /> <Key android:codes="46" android:keyLabel="." android:keyWidth="25%p" /> <Key android:codes="45" android:keyLabel="-" android:keyWidth="25%p" /> <Key android:codes="95" android:keyLabel="_" android:keyWidth="25%p" /> <Key android:codes="-3" android:keyEdgeFlags="right" android:keyLabel="完成" android:keyWidth="25%p" /> </Row> </Keyboard>
KeyboardUtil.java
package com.app.mobile.keyboard; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.inputmethodservice.Keyboard; import android.inputmethodservice.Keyboard.Key; import android.inputmethodservice.KeyboardView; import android.inputmethodservice.KeyboardView.OnKeyboardActionListener; import android.text.Editable; import android.util.Log; import android.view.View; import android.view.WindowManager; import android.widget.EditText; public class KeyboardUtil { private KeyboardView keyboardView; private Keyboard k1; public boolean isupper = false;// 是否大写 boolean isShow = false; private EditText inputEditText; public KeyboardUtil( EditText edit) { this.inputEditText = edit; Activity act = (Activity) this.inputEditText.getContext(); k1 = new Keyboard(act, R.xml.custom_softkeyboard_v1); keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view); keyboardView.setKeyboard(k1); keyboardView.setEnabled(true); keyboardView.setOnKeyboardActionListener(listener); setSoftKeyboradHide(act, edit); } private void setSoftKeyboradHide(Activity ctx,EditText edit) { ctx.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); try { Class<EditText> cls = EditText.class; Method setShowSoftInputOnFocus = null; String method_setSoftInputOnFocus = "setShowSoftInputOnFocus"; if(android.os.Build.VERSION.SDK_INT>16) { method_setSoftInputOnFocus = "setSoftInputOnFocus"; } setShowSoftInputOnFocus = cls.getMethod(method_setSoftInputOnFocus, boolean.class); setShowSoftInputOnFocus.setAccessible(false); setShowSoftInputOnFocus.invoke(edit, false); } catch (Exception e) { e.printStackTrace(); } } 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) { checkIShowPrewiew(primaryCode); } private void checkIShowPrewiew(int primaryCode) { List<Integer> list = Arrays.asList(Keyboard.KEYCODE_CANCEL,Keyboard.KEYCODE_DELETE,Keyboard.KEYCODE_SHIFT,46,32,44); if(list.contains(primaryCode)) { keyboardView.setPreviewEnabled(false); }else{ keyboardView.setPreviewEnabled(true); } } @Override public void onKey(int primaryCode, int[] keyCodes) { checkIShowPrewiew(primaryCode); Log.i("KeyBoard", "primaryCode="+primaryCode); Editable editable = inputEditText.getText(); int start = inputEditText.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(k1); } else if (primaryCode == 57419) { // go left if (start > 0) { inputEditText.setSelection(start - 1); } } else if (primaryCode == 57421) { // go right if (start < inputEditText.length()) { inputEditText.setSelection(start + 1); } }else{ editable.insert(start, Character.toString((char) primaryCode)); } } }; /** * 键盘大小写切换 */ private void changeKey() { List<Key> keylist = k1.getKeys(); 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 if (key.label.toString().equals("小写")) { key.label = "大写"; } } } 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; } else if (key.label.toString().equals("大写")) { key.label = "小写"; } } } } public void showKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.GONE || visibility == View.INVISIBLE) { keyboardView.setVisibility(View.VISIBLE); isShow = true; } } public void hideKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.VISIBLE) { keyboardView.setVisibility(View.INVISIBLE); isShow = false; } } private boolean isword(String str) { String wordstr = "abcdefghijklmnopqrstuvwxyz"; if (wordstr.indexOf(str.toLowerCase()) > -1) { return true; } return false; } }
Activity
package com.app.mobile.keyboard; import android.app.Activity; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.text.InputType; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.EditText; public class MainActivity extends ActionBarActivity { private Activity act; private EditText edit; private KeyboardUtil util; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); act = this; edit = (EditText) this.findViewById(R.id.edit1); edit.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { showKeyBoardView(); return false; } }); } private void showKeyBoardView() { int inputType = edit.getInputType(); edit.setInputType(InputType.TYPE_NULL); if(util==null) { util=new KeyboardUtil(edit); } if(!util.isShow) { util.showKeyboard(); } edit.setInputType(inputType); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ if(util!=null && util.isShow){ util.hideKeyboard(); }else{ finish(); } return false; } return super.onKeyDown(keyCode, event); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
时间: 2024-10-12 12:33:09