简体   繁体   English

Dialog Fragment with和EditText在加载后自动显示键盘

[英]Dialog Fragment with and EditText shows keyboard automatically after load

I have a DialogFragment that has a view with an EditText in it. 我有一个DialogFragment,其中包含一个带有EditText的视图。 Every time I show the dialog the edittext has focus and the keyboard comes up. 每次显示对话框时,edittext都会聚焦并且键盘会出现。

How can i stop it from coming up automatically? 如何阻止它自动启动?

I tried putting android:windowSoftInputMode="stateHidden" in my manifest for the activity that creates the fragment but that had no effect 我尝试在我的清单android:windowSoftInputMode="stateHidden"放入创建片段但没有效果的活动中

this is my dialog 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();
}

} }

this is my layout 这是我的布局

<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>

Use this code to hide it programmatically 使用此代码以编程方式隐藏它

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

I had to give focus to a view above the edit text using 我必须将焦点放在编辑文本上方的视图中

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

in the view 在视图中

Try this: 试试这个:

your_edit_text.clearFocus();

How about if you changed in the manifest to: 如果你在清单中更改为:

android:configChanges="keyboardHidden"

That helped me with some keyboard issues i had :-) 这帮我解决了一些键盘问题:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在对话框中自动获取EditText的键盘(Android) - Automatically get keyboard for an EditText in a Dialog (Android) 对话框解雇后未显示android edittext键盘 - android edittext keyboard not showing after dialog dismissal 使用ViewPager可见Fragment时,EditText会自动打开软键盘 - EditText automatically opens soft keyboard when Fragment is visible with ViewPager EditText TimePicker 显示键盘 - EditText TimePicker shows KeyBoard 自动加载自定义键盘,而无需在Android中触摸EditText - Load Custom KeyBoard Automatically without touching EditText in Android 单击外部EditText后如何隐藏android上的软键盘? 在片段中 - How to hide soft keyboard on android after clicking outside EditText? in fragment 仅在软键盘消失后才显示EditText文本 - EditText text shows only after soft keyboard is gone 键盘正在使用底部工作表对话框片段打开,甚至在 Activity 或 Fragment 中没有 EditText - Keyboard is opening with bottom sheet dialog fragment without even having EditText in Activity or Fragment 对话框后请求EditText时不显示键盘 - Doesn't show Keyboard when requesting EditText after Dialog 第一次单击EditText Android Studio后出现“显示片段对话框” - Getting a Fragment Dialog to appear after first click on EditText Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM