【问题标题】:EditText hidden by keyboard when android:gravity="center" - Android当 android:gravity="center" - Android 时,EditText 被键盘隐藏
【发布时间】:2013-08-20 03:46:27
【问题描述】:

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

我认为这可能是 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"

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

【问题讨论】:

  • 如果你把它放在scrollView里面会发生什么?那么它也会发生吗?
  • 我已经将我的 RelativeLayout 放在一个 ScrollView 中:这并没有改变任何东西,抱歉
  • 是整个 EditText 隐藏还是只是字符开始的地方?
  • 你应该试试看,但是是的,因为我设置了 android:layout_marginTop="300dp",整个edittext被隐藏了
  • 我的意思是边距将编辑文本放在屏幕底部附近,所以是的,它完全隐藏了。边距与我的问题无关

标签: android xml layout keyboard android-edittext


【解决方案1】:

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

}

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

【讨论】:

  • 哇,谢谢,但这对我来说太棘手了。我绕过了删除 android:gravity="center" 的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 2014-12-13
相关资源
最近更新 更多