android - 带有 EditText 的对话框 fragment 在加载后自动显示键盘

标签 android android-edittext android-softkeyboard android-dialogfragment

我有一个 DialogFragment,它有一个包含 EditText 的 View 。每次我显示对话框时,编辑文本都有焦点并且键盘出现。

如何阻止它自动出现?

我尝试将 android:windowSoftInputMode="stateHidden" 放入创建 fragment 的 Activity list 中,但没有效果

这是我的对话 fragment

public class RegDialog extends DialogFragment {

OnRegComplete mRegComplete;

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
        mRegComplete = (OnRegComplete)activity;
    }catch(ClassCastException e){
        throw new ClassCastException(activity.toString() + " must implement OnRegComplete");
    }
}

public Dialog onCreateDialog(Bundle state){
    Dialog d = new Dialog(getActivity());
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.registration);
    d.setCanceledOnTouchOutside(false);

    return d;
}

public interface OnRegComplete{
    void onRegComplete();
}

这是我的布局

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"
    android:layout_marginLeft="20dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:textSize="30sp"
    android:text="Registration" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="16dp"
    android:text="State"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_marginTop="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/reg_title" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView4"
    android:layout_toRightOf="@+id/textView4"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_alignLeft="@+id/editText1"
     />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="40dp"
    android:text="Station"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="40dp"
    android:text="County"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_alignLeft="@+id/editText1" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView6"
    android:layout_toRightOf="@+id/textView6"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_marginLeft="20dp"
    android:ems="10"
    android:inputType="textPassword" />

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView6"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_alignLeft="@+id/editText1" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="36dp"
    android:text="@string/reg_btn" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView5"
    android:layout_below="@+id/textView5"
    android:layout_marginTop="42dp"
    android:text="Password"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="15dp"
    android:src="@drawable/mobile_mapr_logo" />

</RelativeLayout>

最佳答案

使用此代码以编程方式隐藏它

Dialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

关于android - 带有 EditText 的对话框 fragment 在加载后自动显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146128/

相关文章:

android - Kotlin:如何获取字符串中 "@"之后的字符?

java - 使用 Android Studio 将对象或数组列表从一个 fragment 传递到另一个 fragment

android - 避免 EditText TextWatcher 在每次字符更改时触发事件

Android: AutoCompleteTextView 隐藏软键盘

android - 如何使用 Firestore 填充引用字段

android - 如何将我的按钮设置为背景的随机颜色。安卓

android - EditText setError() 带有图标但没有弹出消息

android - 单击按钮将 android 键盘语言更改为印地语

Android 自定义键盘从 Activity 更改