在android中打开活动时如何防止键盘打开?

时间:2013-07-18 00:59:47

标签: android android-edittext

在我的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" />

2 个答案:

答案 0 :(得分:14)

只需在onCreate(...)方法

中添加此行代码即可
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

答案 1 :(得分:0)

对于大多数用户而言,EditText可能正在发生这种情况,您所要做的就是,只需转到布局文件并按如下所示设置布局的视图组属性:

 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;