【问题标题】:How to stop keyboard popping up in EditText when activity gets opened? [duplicate]打开活动时如何停止在EditText中弹出键盘? [复制]
【发布时间】:2019-12-04 05:56:14
【问题描述】:

我有MainActivity,有EditTextPinView 用于用户名和密码。当我打开MainActivity时,键盘会自动弹出。

如何预防?

activit_main.xml

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/edtEmailLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/lblLogin">

                    <EditText
                        android:id="@+id/editText_mobile"
                        style="@style/lato_regular"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/toolbarHeight"
                        android:background="@drawable/edit_bottom_line"
                        android:drawablePadding="8dp"
                        android:gravity="center_vertical"
                        android:hint="@string/mobileNumber"
                        android:inputType="number"
                        android:maxLength="10"
                        android:padding="10dp"
                        android:paddingStart="@dimen/space_with_4dp"
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textColorHint="@color/text_hint_color"
                        android:textSize="16sp"
                        />
                </android.support.design.widget.TextInputLayout>

AndroidManifest.xml

        <activity
        android:name=".module.activity.signin.MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateAlwaysHidden">

        <!-- First activity -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

我也试过了:

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText_mobile.getWindowToken(), 0);

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

【问题讨论】:

标签: android android-edittext


【解决方案1】:

我正在使用它,它工作得很好。在我点击它之前它不会显示关键字.. 即使你应该尝试..

<com.google.android.material.textfield.TextInputLayout
      android:id="@+id/text1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="16dp">

      <EditText
          android:id="@+id/username"
          style="@style/TextAppearance.AppCompat.Small"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="20dp"
          android:background="@android:color/transparent"
          android:drawableStart="@drawable/email_login"
          android:drawableLeft="@drawable/email_login"
          android:drawablePadding="12dp"
          android:hint="@string/usernameedit"
          android:imeOptions="actionNext"
          android:inputType="text"
          android:maxLength="32"
          android:maxLines="1"
          android:padding="13dp"
          android:textColor="@android:color/black"
          android:textColorHighlight="@android:color/tertiary_text_dark"
          android:textColorHint="@android:color/white" />

     <View
          android:layout_width="match_parent"
          android:layout_height="2dp"
          android:layout_below="@+id/username"
          android:layout_marginLeft="4dp"
          android:layout_marginRight="4dp"
          android:background="#f9d7db" />
</com.google.android.material.textfield.TextInputLayout>

【讨论】:

    【解决方案2】:

    在您的 XML 布局的 Root View 中添加这两行

    根视图可以是 ContraintLayout 或任何其他布局。

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" 
    

    【讨论】:

      【解决方案3】:

      将这两行添加到EditText的父级

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

      你也可以使用

      windowSoftInputMode="stateAlwaysHidden" 
      

      在活动清单@Makarand

      【讨论】:

        猜你喜欢
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        相关资源
        最近更新 更多