我最近在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();頁面上的所有元素。
公共靜態無效setupUI(查看視圖){
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) {}
}
這是相當混亂,但沒有解決問題,希望有雖然是更好的辦法。
如果將它放在scrollView中會發生什麼?那麼它也發生? –
我已將我的RelativeLayout放入ScrollView中:不會更改任何內容,對不起 –
整個'EditText'是隱藏的還是隻是字符的起始位置? – codeMagic