5

好吧,经过几个小时寻找解决方案,我来到了这里。

我认为这可能是Android的问题。

尝试创建这个简单的布局。打开键盘,隐藏它,然后再次打开它,EditText 被隐藏。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="300dp"
        android:gravity="center"
        android:inputType="phone"
        android:maxLength="14"
        android:text="some text"
        android:textColor="#666666"
        android:textSize="22sp" />

</RelativeLayout>

现在,删除android:gravity="center",一切正常!在你问我之前,是的,我已经添加了android:windowSoftInputMode="adjustPan"

任何解决方案将不胜感激!谢谢

4

1 回答 1

2

我最近在 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) {}

}

这很混乱,但确实解决了问题,但希望有更好的方法。

于 2013-10-02T08:15:14.080 回答