アクション バー内にオート コンプリート テキスト ビューを実装するアプリがあります。私は Action Bar Sherlock を使用しており、このオートコンプリートはタグで表示されます
android:actionLayout="@layout/field_search"
以下に示すように、xml メニューからのアイテムの。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/autocomplete_menu_item"
android:showAsAction="always|collapseActionView"
android:actionLayout="@layout/field_search"
android:icon="@drawable/ic_search"
/>
</menu>
検索アイコンをクリックすると、キーボードがユーザーに表示されます。ユーザーがアイテムを選択すると、選択したアイテム名のオートコンプリート テキスト ビューのコンテンツが変更され、キーボードが非表示になります。さて、この部分はうまく機能しています。
機能していないのは、オートコンプリート クリック イベントでのみキーボードが非表示になっていることです。そのため、検索項目をクリックしてオート コンプリート テキスト ビューの項目を選択せずに、アクション バーのホーム ボタンをクリックして (現在のアクティビティを閉じるため)、キーボードが閉じられません。開いたままです。
以下の方法でホームボタン(アクションバーの)をクリックして設定しています:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Action bar back button.
case android.R.id.home:
onBackPressed();
return true;
case R.id.autocomplete_menu_item:
initializeAutoComplete();
return true;
// Default.
default:
return super.onOptionsItemSelected(item);
}
}
そのため、onBackPressed() メソッドが呼び出されています。しかし、キーボードは閉じられていません。
次に、以下に示すように、onPause() メソッドでキーボードを閉じようとしました。
@Override
protected void onPause() {
super.onPause();
// Closes keyboard before exit.
if (mKeyboardShown)
hideKeyboard(mAutoComplete);
}
繰り返しますが、キーボードは閉じられていません。
キーボードを開閉するメソッドを以下に示します。
/**
* Shows the keyboard.
*
* @param view
*/
public void showKeyboard(View view) {
Context context = view.getContext();
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
/**
* Hides the keyboard.
*
* @param view
*/
public void hideKeyboard(View view) {
Context context = view.getContext();
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
オートコンプリートを管理するメソッドを以下に示します。
/**
* Calls the auto complete text view.
*/
public boolean initializeAutoComplete() {
if (mAutoComplete == null) return false;
// Cleans text.
mAutoComplete.setText("");
// Invoke virtual keyboard.
mAutoComplete.requestFocus();
showKeyboard(mAutoComplete);
mKeyboardShown = true;
// Creates an array adapter to display the school units from the auto complete text view.
final AutoCompleteAdapter adapter = new AutoCompleteAdapter(this, mList);
// Sets the click listener of the auto complete text view, to show the keyboard when the auto complete
// text view has shown the keyboard and this keyboard was closed (so the auto complete text view is been
// shown, but the keyboard don't, so we need to show it).
mAutoComplete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showKeyboard(mAutoComplete);
mKeyboardShown = true;
}
});
// Sets the click listener of the auto complete text view, to set the adapter.
mAutoComplete.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// Populate list with our static array of titles.
mAutoComplete.setAdapter(adapter);
return false;
}
});
// Sets the item click listener of the auto complete text view, to set the auto complete text view name,
// hide the keyboard, and hide the auto complete drop down.
mAutoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get id of the unit from the adapter tag.
Integer nameId = (Integer)view.getTag();
// Get clicked id of the auto complete text view.
Data data = getDataById(nameId);
if (data != null) mAutoComplete.setText(data.getName());
else mAutoComplete.setText("Data not found.");
// Hide keyboard and hide auto complete drop down.
hideKeyboard(mAutoComplete);
mKeyboardShown = false;
mAutoComplete.setDropDownHeight(0);
}
});
return true;
}
そのため、showKeyboard() を呼び出すたびにキーボードが表示されますが、オート コンプリート テキスト ビューのクリック イベントで hideKeyboard() を呼び出したときにのみ非表示になります。
クリックイベントが呼び出されたときに hideKeyboard() が「ウィンドウトークン」のみを取得するようです。しかし、クリックイベントだけでなく、必要なときにキーボードを閉じる必要があります。
このアプリには、MainActivity と AutoCompleteActivity の 2 つのアクティビティがあります。以下に、6 つのフレーム (左から右/上から下) に分割された 1 つの画像を示し、次のアクションを示します。
- AutoCompleteActivity を入力した後。
- 検索アイコンをクリックした後(ここではキーボードが呼び出されます)。
- テキストを入力すると、アダプタによって検出されたものがオートコンプリートに表示されます。
- オートコンプリートによって表示されたアイテムをクリックすると、オートコンプリートのテキスト コンテンツが、選択したアイテムの名前 (この場合は白色の「Apple」) で更新されます。
- アクション バーの [戻る] ボタンをクリックすると、オート コンプリート テキスト ビューは非表示になりますが、キーボードは非表示になり、onBackPressed() を呼び出しても (onOptionsItemSelected() が呼び出された場合)。
- アクション バーの戻るボタンをもう一度押すと、MainActivity に戻ります。ただし、AutoCompleteActivity の onPaused() が呼び出されても、キーボードは画面に残ります。これはどのように起こりますか?
この問題を解決するにはどうすればよいですか?