十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在Android开发中怎么实现一个自定义输入法功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
专注于为中小企业提供成都网站设计、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业明水免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
1.前面两个输入框使用了自定义的输入法:
2.第三个输入框没有进行任何设置,因此将使用默认的输入法:
(二)代码简介:
1.主页面布局,由3个输入框加上一个android.inputmethodservice.KeyboardView组成。android.inputmethodservice.KeyboardView是一个系统自带的继承自View的组件,但是它不在android.view这个包下面,因此这里需要写上完整的包名。
2.KeyboardView是一个显示输入法的容器控件,使用时需要设置具体的输入法面板内容。
(1)首先在res下新建xml目录,然后创建文件keys_layout.xml,即输入法面板的内容。每个row表示一行,Keyboad的属性keyWidth和keyHeight表示每个按键的大小,25%p表示占父组件的25%. Key的属性codes表示该按键的编号(点击时系统回调方法中会返回这个值,用以区分不同的按键),keyLabel表示按键上面显示的文字。还有很多其它的属性,不再陈述。
<?xml version="1.0" encoding="utf-8"?>|
|
|
(2)为了使用方便,新建一个类:KeyboardBuilder.java,用于初始化自定义输入法和绑定EditText,代码如下:
public class KeyboardBuilder { private static final String TAG = "KeyboardBuilder"; private Activity mActivity; private KeyboardView mKeyboardView; public KeyboardBuilder(Activity ac, KeyboardView keyboardView, int keyBoardXmlResId) { mActivity = ac; mKeyboardView = keyboardView; Keyboard mKeyboard = new Keyboard(mActivity, keyBoardXmlResId); // Attach the keyboard to the view mKeyboardView.setKeyboard(mKeyboard); // Do not show the preview balloons mKeyboardView.setPreviewEnabled(false); KeyboardView.OnKeyboardActionListener keyboardListener = new KeyboardView.OnKeyboardActionListener() { @Override public void onKey(int primaryCode, int[] keyCodes) { // Get the EditText and its Editable View focusCurrent = mActivity.getWindow().getCurrentFocus(); if (focusCurrent == null || !(focusCurrent instanceof EditText)) { return; } EditText edittext = (EditText) focusCurrent; Editable editable = edittext.getText(); int start = edittext.getSelectionStart(); // Handle key if (primaryCode == Constant.CodeCancel) { hideCustomKeyboard(); } else if (primaryCode == Constant.CodeDelete) { if (editable != null && start > 0) { editable.delete(start - 1, start); } } else { // Insert character editable.insert(start, Character.toString((char) primaryCode)); } } @Override public void onPress(int arg0) { } @Override public void onRelease(int primaryCode) { } @Override public void onText(CharSequence text) { } @Override public void swipeDown() { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeUp() { } }; mKeyboardView.setOnKeyboardActionListener(keyboardListener); } //绑定一个EditText public void registerEditText(EditText editText) { // Make the custom keyboard appear editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { showCustomKeyboard(v); } else { hideCustomKeyboard(); } } }); editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick"); showCustomKeyboard(v); } }); editText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.d(TAG, "onTouch"); EditText edittext = (EditText) v; int inType = edittext.getInputType(); // Backup the input type edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard edittext.onTouchEvent(event); // Call native handler edittext.setInputType(inType); // Restore input type edittext.setSelection(edittext.getText().length()); return true; } }); } public void hideCustomKeyboard() { mKeyboardView.setVisibility(View.GONE); mKeyboardView.setEnabled(false); } public void showCustomKeyboard(View v) { mKeyboardView.setVisibility(View.VISIBLE); mKeyboardView.setEnabled(true); if (v != null) { ((InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0); } } public boolean isCustomKeyboardVisible() { return mKeyboardView.getVisibility() == View.VISIBLE; } }
3.最后是主Activity的代码,这里就很简单了。
/** * 自定义安全输入法 */ public class MainActivity extends ActionBarActivity { private KeyboardBuilder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardview); builder = new KeyboardBuilder(this, keyboardView, R.xml.keys_layout); EditText editText = (EditText) findViewById(R.id.input_password); builder.registerEditText(editText); EditText editText2 = (EditText) findViewById(R.id.input_password2); builder.registerEditText(editText2); } @Override public void onBackPressed() { if (builder != null && builder.isCustomKeyboardVisible()) { builder.hideCustomKeyboard(); } else { this.finish(); } } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。