在Android開發中怎么實現一個自定義輸入法功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
1.前面兩個輸入框使用了自定義的輸入法:

2.第三個輸入框沒有進行任何設置,因此將使用默認的輸入法:

(二)代碼簡介:
1.主頁面布局,由3個輸入框加上一個android.inputmethodservice.KeyboardView組成。android.inputmethodservice.KeyboardView是一個系統自帶的繼承自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目錄,然后創建文件keys_layout.xml,即輸入法面板的內容。每個row表示一行,Keyboad的屬性keyWidth和keyHeight表示每個按鍵的大小,25%p表示占父組件的25%. Key的屬性codes表示該按鍵的編號(點擊時系統回調方法中會返回這個值,用以區分不同的按鍵),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" />
<!--刪除按鍵長按時連續響應-->
<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();
}
}
}看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。