最近、Nexus 7 スマートフォンでこの問題に対処しました。それ以外の場合、動作は他のテスト電話では発生しませんでした. トリックは、ソフトキーボードを閉じる前にフォーカスを変更することのようです。キーボードは、[完了] ボタンのクリック、編集テキスト ボックスの外側のクリック、[戻る] ボタンの 3 つのポイントで閉じられます。
まず、フォーカスを食い尽くす隠し要素を作成します
<MyEditText
android:id="@+id/editHidden"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/imageLogin"
/>
保存してたけどちょっと汚い…
@Override
protected void onResume() {
super.onResume();
Utils.focusable = findViewById(R.id.editHidden);
キーボードが閉じられたら、フォーカスを隠し要素に変更します。
public static void clearFocus() {
try {
if (focusable!=null)
focusable.requestFocus();
} catch (Exception e) {}
}
public static void hideSoftKeyboard(View view) {
clearFocus();
if (view!=null) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {}
}
}
次に、キーボードが閉じている場所で hideKeyboard 関数を実行します: EditText 戻るボタンが押されました:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
try {
Utils.clearFocus();
} catch (Exception e) {}
}
return super.onKeyPreIme(keyCode, event);
}
[完了] ボタンを押して、問題が発生している EditText ボックスにこれを添付します。
public static OnEditorActionListener getOnEditorActionListener() {
return new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
hideSoftKeyboard(v);
}
return false;
}
};
}
テキストボックスの外側をクリック - onCreate() でページ上のすべての要素にアタッチします。
public static void setupUI(View ビュー) {
try {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(v);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
} catch (Exception e) {}
}
かなり面倒ですが、問題は解決しましたが、もっと良い方法があることを願っています。