[英]Hide Keyboard from editText within dialog
我知道如果類是Activity的話,該如何隱藏鍵盤,但是當它擴展DialogFragment時,我從Activity中隱藏鍵盤的代碼是行不通的。
到目前為止,這是我的代碼:
public class PersonalData extends DialogFragment
LinearLayout activity_personaldata;
//Oncreate:
activity_personaldata = (LinearLayout) view.findViewById(R.id._activity_personaldata_);
activity_personaldata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
});
而我的XML是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/_activity_personaldata_"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
</ScrollView>
</LinearLayout>
我在XML的linearlayout中設置了ID,並使其具有焦點。 但它仍然無法正常工作。 請幫助我,在此先感謝:)
在onCreateView()
嘗試此代碼。 getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
如果要在用戶單擊EditText
之外時隱藏鍵盤,則可以為該EditText
實現focusListner
。
youreditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.