簡體   English   中英

Dialog Fragment with和EditText在加載后自動顯示鍵盤

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

我有一個DialogFragment,其中包含一個帶有EditText的視圖。 每次顯示對話框時,edittext都會聚焦並且鍵盤會出現。

如何阻止它自動啟動?

我嘗試在我的清單android:windowSoftInputMode="stateHidden"放入創建片段但沒有效果的活動中

這是我的對話框片段

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:focusable="true"
android:focusableInTouchMode="true"

在視圖中

試試這個:

your_edit_text.clearFocus();

如果你在清單中更改為:

android:configChanges="keyboardHidden"

這幫我解決了一些鍵盤問題:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM