アクションバーにカスタムActionViewを追加しました。これは、ユーザーが数値を入力して何かを検索できるようにすることを目的としています。ただし、レイアウトに使用したEditTextは、imeType of numberを指定した場合でも、常に完全な英数字キーボードを表示します。英数字キーボードは必要ありません。ちなみに、+/-タイプのオプションすら必要ありません。ちょうど0-9と「完了」。検索用のEditTextでカスタムキーパッドを使用するにはどうすればよいですか?ドロップダウンキーボードを使用する方法はありますか?そのフィールド専用のカスタムIMEを作成したいと思っていましたが、許可されていないようです。
メニューのXML:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_keypad"
android:icon="@drawable/ms_btn_keypad_sel"
android:title="Keypad"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="@layout/keypad_actionview"/>
</menu>
アクションレイアウトのXML:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypad_actionview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/White"
android:drawableLeft="@drawable/icon_channel_keypad"
android:drawablePadding="5dp"
android:inputType="number"
android:imeOptions="flagNoExtractUi"
/>
アクションバーメニューを設定するためのJava:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.number_menu, menu);
keypadMenuItem = menu.findItem(R.id.menu_keypad);
final EditText keypadText = (EditText) keypadMenuItem.getActionView();
keypadText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView textView, final int i, final KeyEvent keyEvent) {
keypadMenuItem.collapseActionView();
changeDisplayedNumber(Integer.valueOf(textView.getText().toString()), true);
textView.setText("");
return true;
}
});
keypadMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
startAutoHidePlayerControlsRunner();
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
stopAutoHidePlayerControlsRunner();
keypadText.setText("");
keypadText.requestFocus();
/*keypadText.setInputType(InputType.TYPE_CLASS_NUMBER);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}*/
return true; // Return true to expand action view
}
});
return true;
}
Javaの最後にあるコメントアウトされたコードは、キーボードをすぐに表示するように強制することでしたが、それは奇妙な動作を引き起こしていました-数値以外のキーボードがポップアップします。