在Android開發(fā)中怎么實現(xiàn)一個自定義輸入法功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)明水免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
1.前面兩個輸入框使用了自定義的輸入法:
2.第三個輸入框沒有進行任何設(shè)置,因此將使用默認的輸入法:
(二)代碼簡介:
1.主頁面布局,由3個輸入框加上一個android.inputmethodservice.KeyboardView組成。android.inputmethodservice.KeyboardView是一個系統(tǒng)自帶的繼承自View的組件,但是它不在android.view這個包下面,因此這里需要寫上完整的包名。
2.KeyboardView是一個顯示輸入法的容器控件,使用時需要設(shè)置具體的輸入法面板內(nèi)容。
(1)首先在res下新建xml目錄,然后創(chuàng)建文件keys_layout.xml,即輸入法面板的內(nèi)容。每個row表示一行,Keyboad的屬性keyWidth和keyHeight表示每個按鍵的大小,25%p表示占父組件的25%. Key的屬性codes表示該按鍵的編號(點擊時系統(tǒng)回調(diào)方法中會返回這個值,用以區(qū)分不同的按鍵),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(); } } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。