如何在Android中打开Activity时防止键盘弹出?

9
在我的Android应用中,当我启动“个人资料编辑”页面时,第一个EditText字段会自动聚焦(光标闪烁),并弹出键盘。
如何保持该字段仍然自动聚焦(光标闪烁),但防止键盘弹出?如果这不可能,则不聚焦于该EditText。
谢谢。
        <EditText
            android:id="@+id/textinput_firstname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="test" />

可能是重复的 https://dev59.com/cHE95IYBdhLWcg3wE52C - DynamicMind
这个问题是关于显示键盘,而这个问题是关于隐藏键盘,你为什么认为这两个问题可能会重复? - Salman Khakwani
2个回答

18

只需在您的onCreate(...)方法中添加此行代码即可

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

1
我必须等待5分钟才能...该网站不允许我。 - omega
这将破坏通过 android:windowSoftInputMode="adjustResize" 调整大小的 Activity 功能集 - 将像 adjustPan 一样行为并停止调整大小... - snachmsm

2

对于大多数用户来说,EditText 可能会出现这种情况。您需要做的就是打开 layout 文件,并将布局视图组的属性设置为以下内容:

 android:focusable="true"
 android:focusableInTouchMode="true"

例如。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:focusable="true"
    android:focusableInTouchMode="true"> 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="41dp"
      />
</LinearLayout>

注意:无论第一个视图是什么,您都必须为其设置属性。这将将默认焦点设置为false;

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