隐藏软键盘问题

3
我有一个包含编辑框的gridview,在activity上方,下面有一个自定义键盘,如下面的代码所示,我添加了按钮来输入数字。但问题是每当我点击gridview中的edittexts时,软键盘就会弹出。
我尝试将android:windowSoftInputMode="stateHidden"放入清单文件中,但在我的情况下它甚至不隐藏软键盘。我参考了很多来源,包括stackoverflow,没有一个帖子离开。我认为我的main.xml文件存在问题。我们不能在gridview的子级中隐藏它吗(我知道这似乎很愚蠢,但这是我经过尝试许多事情后最终得到的疑问)。请问有人能建议我哪里出错了吗?如果您使用您的答案测试以下代码,然后建议我是否有效,我将不胜感激。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <FrameLayout
                android:id="@+id/fLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:background="@drawable/background3">
        <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/gridview"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:columnWidth="18dp"
                android:numColumns="6"
                android:verticalSpacing="0dp"
                android:horizontalSpacing="1dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:listSelector="@null"/>
        </FrameLayout>

        <FrameLayout
                android:id="@+id/fLayout2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="4"
                android:background="@drawable/background2">

        <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/keypad"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:stretchColumns="*">

        <TableRow>
        <Button android:id="@+id/keypad_1"
        android:text="1"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_2"
        android:text="2"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_3"
        android:text="3"
        android:background="@drawable/custombutton">
        </Button>

        <Button android:id="@+id/keypad_4"   
        android:text="4"
        android:background="@drawable/custombutton">
        </Button>

        <Button android:id="@+id/keypad_5"
        android:text="5"
        android:background="@drawable/custombutton">
        </Button>

        </TableRow>
        <TableRow>



        <Button android:id="@+id/keypad_6"
        android:text="6"
        android:background="@drawable/custombutton">
        </Button>

        <Button android:id="@+id/keypad_7"
        android:text="7"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_8"
        android:text="8"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_9"
        android:text="9"
        android:background="@drawable/custombutton"> 
        </Button>

        <Button android:id="@+id/keypad_10"
        android:text="C"
        android:background="@drawable/custombutton">
        </Button>

        </TableRow>
        <TableRow>
        <Button android:id="@+id/submit"
        android:text="submit"
        android:layout_span="5"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/custombutton">

        </Button>
        </TableRow>
        </TableLayout>

        </FrameLayout>
</LinearLayout>

@Shrikant,你可以查看我的问题,如果给出的解决方案甚至没有为我的问题提供提示,我该如何接受答案。 - Kanth
如果你没有得到合适的答案,那么请检查一下你是否清楚地解释了问题。我相信你可能已经阅读过了,但我仍然建议在发布问题之前始终阅读常见问题解答。 - Shrikant Ballal
这对我来说似乎是一个参数。如果您对我的问题有任何疑问,现在可以问我,我会很高兴回答。这个问题也不例外。当然,我总是表达感激之情,因为至少他们正在尝试帮助解决我的问题。 - Kanth
1个回答

1
你尝试在onCreate()方法中添加这行代码了吗?

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

或者

尝试在清单文件中使用之前的方法,但是将 stateHidden 替换为 stateAlwaysHidden

或者

你是否尝试在 EditText 上放置一个 onTouch 监听器?如果我没记错的话,它会“消耗”触摸事件,键盘就不会出现了。类似这样:

editText_input_field.setOnTouchListener(otl);

private OnTouchListener otl = new OnTouchListener() {
    public boolean onTouch (View v, MotionEvent event) {
            return true; // the listener has consumed the event
    }
 };

这里获取

如果有任何问题,请告诉我:)。如果不行,我会想出其他解决方案...


谢谢您的回复。我之前也尝试过这些方法,但都没有成功。最终使用了editText.setFocusable(false)方法,虽然它不能在单击时高亮显示编辑框,光标也不可见。因此,我无法使用它。我甚至尝试了requestFocus方法,但也没有起作用。 - Kanth
我之前也尝试过这个方法:editText.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { editText.setFocusable(true); editText.setFocusableInTouchMode(true); return false; } }); 但是没有起作用。你的方法也一样,但是也没有起作用。现在它完全禁用了编辑框。 - Kanth
非常感谢您的时间。为此点赞。如果您找到其他解决方案,请告诉我。 - Kanth

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