当android:gravity="center"时,EditText被键盘遮挡 - Android

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"。 非常感谢任何解决方案!谢谢。

如果将它放在scrollView中会发生什么?那样也会发生吗? - Shobhit Puri
我已经将我的RelativeLayout放在了ScrollView中:这并没有改变任何东西,抱歉。 - Romain Pellerin
整个 EditText 是隐藏的还是只有字符开始的地方隐藏了? - codeMagic
你应该尝试查看一下,但是是的,因为我设置了android:layout_marginTop="300dp",整个编辑框都被隐藏了。 - Romain Pellerin
对于这个例子,只需将alignParentBottom设置为true:不会改变任何东西。 - Romain Pellerin
显示剩余4条评论
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()中将点击文本框外部的事件附加到页面上的所有元素;

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) {}

这样做虽然有些混乱,但解决了问题,希望能找到更好的方法。


1
哇,谢谢你,但这对我来说太棘手了。我通过删除android:gravity="center"来绕过这个问题。 - Romain Pellerin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接