Android編程實現(xiàn)自定義輸入法功能示例【輸入密碼時防止第三方竊取】
本文實例講述了Android編程實現(xiàn)自定義輸入法功能。分享給大家供大家參考,具體如下:
對于Android用戶而言,一般都會使用第三方的輸入法??墒?,在輸入密碼時(尤其是支付相關的密碼),使用第三方輸入法有極大的安全隱患。目前很多網(wǎng)銀類的APP和支付寶等軟件在用戶輸入密碼時,都會彈出自定義的輸入法而不是直接使用系統(tǒng)輸入法。
這里介紹的就是如何實現(xiàn)一個簡單的自定義輸入法。當然,也可以自己寫一個Dialog加上幾十個按鈕讓用戶輸入,只不過這樣顯得不夠專業(yè)。
(一)首先上效果圖:
1.前面兩個輸入框使用了自定義的輸入法:
2.第三個輸入框沒有進行任何設置,因此將使用默認的輸入法:
(二)代碼簡介:
1.主頁面布局,由3個輸入框加上一個android.inputmethodservice.KeyboardView組成。android.inputmethodservice.KeyboardView是一個系統(tǒng)自帶的繼承自View的組件,但是它不在android.view這個包下面,因此這里需要寫上完整的包名。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!--前兩個EditText均使用自定義的輸入法--> <EditText android:id="@+id/input_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:hint="one password" android:layout_alignParentTop="true" android:inputType="textPassword" /> <EditText android:id="@+id/input_password2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/input_password" android:layout_margin="8dp" android:hint="another password" android:inputType="textPassword" /> <!--這個EditText使用默認的輸入法--> <EditText android:id="@+id/input_normal_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/input_password2" android:layout_margin="8dp" android:hint="normal text" /> <android.inputmethodservice.KeyboardView android:id="@+id/keyboardview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:focusable="true" android:focusableInTouchMode="true" android:visibility="gone" /> </RelativeLayout>
2.KeyboardView是一個顯示輸入法的容器控件,使用時需要設置具體的輸入法面板內容。
(1)首先在res下新建xml目錄,然后創(chuàng)建文件keys_layout.xml,即輸入法面板的內容。每個row表示一行,Keyboad的屬性keyWidth和keyHeight表示每個按鍵的大小,25%p表示占父組件的25%. Key的屬性codes表示該按鍵的編號(點擊時系統(tǒng)回調方法中會返回這個值,用以區(qū)分不同的按鍵),keyLabel表示按鍵上面顯示的文字。還有很多其它的屬性,不再陳述。
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="25%p" android:keyHeight="10%p"> <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" /> <!--刪除按鍵長按時連續(xù)響應--> <Key android:codes="60001" android:keyLabel="DEL" android:isRepeatable="true" /> </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" /> <Key android:codes="48" android:keyLabel="0" /> </Row> <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="60002" android:keyLabel="Cancel" /> </Row> </Keyboard>
(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(); } } }
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android開發(fā)動畫技巧匯總》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android布局layout技巧總結》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- Android文本輸入框(EditText)輸入密碼時顯示與隱藏
- Android 實現(xiàn)密碼輸入框動態(tài)明文/密文切換顯示效果
- Android程序開發(fā)之防止密碼輸入錯誤 密碼明文顯示功能
- Android實現(xiàn)動態(tài)顯示或隱藏密碼輸入框的內容
- Android仿支付寶支付密碼輸入框
- Android 自定義輸入支付密碼的軟鍵盤實例代碼
- Android仿支付寶、京東的密碼鍵盤和輸入框
- Android自定義View仿支付寶輸入六位密碼功能
- Android的支付密碼輸入框實現(xiàn)淺析
- Android高仿微信支付密碼輸入控件
- Android仿微信/支付寶密碼輸入框
- Android編程實現(xiàn)打勾顯示輸入密碼功能
相關文章
Android中的webview支持頁面中的文件上傳實例代碼
本篇文章主要介紹了Android中的webview支持頁面中的文件上傳,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android自定義ListView實現(xiàn)仿QQ可拖拽列表功能
這篇文章主要介紹了Android自定義ListView實現(xiàn)仿QQ可拖拽列表功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08詳解Android自定義控件屬性TypedArray以及attrs
這篇文章主要為大家介紹了android自定義控件屬性TypedArray以及attrs,感興趣的小伙伴們可以參考一下2016-01-01