android - android :gravity ="center" - Android时键盘隐藏的EditText

标签 android xml layout keyboard android-edittext

好吧,在花了几个小时寻找解决方案之后,我来到了这里。

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

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

最佳答案

我最近在 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 - android :gravity ="center" - Android时键盘隐藏的EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18295290/

相关文章:

android - 使用 MvvmCross 构建的 Xamarin Android 应用程序在 3-4 天后从后台恢复时卡在启动画面上

android - 一次加载 10 个项目 ListView Android

android - GPS Mock - 我只能模拟一次位置

java - JAXB 验证错误 - cvc-complex-type.2.4.a : Invalid content was found starting with element 'codeSystem' . 需要 '{codeSystem}' 之一

html - 调整 div 容器大小时,如何使内联元素的水平列表垂直堆叠?

android - 覆盖 Intent 额外

python - xml.etree.ElementTree.ParseError -- 异常处理未捕获错误

xml - 将整个 XML 作为属性输出

java - 两个Relativelayouts垂直显示

html - css中的简单布局