国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程實(shí)現(xiàn)自定義輸入法功能示例【輸入密碼時(shí)防止第三方竊取】

2019-12-12 03:57:15
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Android編程實(shí)現(xiàn)自定義輸入法功能。分享給大家供大家參考,具體如下:

對于Android用戶而言,一般都會使用第三方的輸入法。可是,在輸入密碼時(shí)(尤其是支付相關(guān)的密碼),使用第三方輸入法有極大的安全隱患。目前很多網(wǎng)銀類的APP和支付寶等軟件在用戶輸入密碼時(shí),都會彈出自定義的輸入法而不是直接使用系統(tǒng)輸入法。

這里介紹的就是如何實(shí)現(xiàn)一個(gè)簡單的自定義輸入法。當(dāng)然,也可以自己寫一個(gè)Dialog加上幾十個(gè)按鈕讓用戶輸入,只不過這樣顯得不夠?qū)I(yè)。

(一)首先上效果圖:

1.前面兩個(gè)輸入框使用了自定義的輸入法:

2.第三個(gè)輸入框沒有進(jìn)行任何設(shè)置,因此將使用默認(rèn)的輸入法:

(二)代碼簡介:

1.主頁面布局,由3個(gè)輸入框加上一個(gè)android.inputmethodservice.KeyboardView組成。android.inputmethodservice.KeyboardView是一個(gè)系統(tǒng)自帶的繼承自View的組件,但是它不在android.view這個(gè)包下面,因此這里需要寫上完整的包名。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent">  <!--前兩個(gè)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" />  <!--這個(gè)EditText使用默認(rèn)的輸入法-->  <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是一個(gè)顯示輸入法的容器控件,使用時(shí)需要設(shè)置具體的輸入法面板內(nèi)容。

(1)首先在res下新建xml目錄,然后創(chuàng)建文件keys_layout.xml,即輸入法面板的內(nèi)容。每個(gè)row表示一行,Keyboad的屬性keyWidth和keyHeight表示每個(gè)按鍵的大小,25%p表示占父組件的25%. Key的屬性codes表示該按鍵的編號(點(diǎn)擊時(shí)系統(tǒng)回調(diào)方法中會返回這個(gè)值,用以區(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" />    <!--刪除按鍵長按時(shí)連續(xù)響應(yīng)-->    <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)為了使用方便,新建一個(gè)類: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);  }  //綁定一個(gè)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();    }  }}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android開發(fā)動畫技巧匯總》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大庆市| 上林县| 通许县| 自贡市| 乐都县| 林周县| 周口市| 营口市| 关岭| 双鸭山市| 建平县| 天峻县| 芮城县| 景东| 河源市| 彩票| 永川市| 丰县| 措美县| 舒城县| 林周县| 静海县| 灯塔市| 察雅县| 合水县| 乌拉特中旗| 延长县| 普兰店市| 保康县| 大悟县| 集贤县| 南和县| 乌鲁木齐市| 连城县| 庆元县| 昌都县| 舟曲县| 凤山市| 大宁县| 西安市| 南雄市|