从对话框中的 editText 隐藏键盘
Hide Keyboard from editText within dialog
如果 class 扩展 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 的线性布局中创建了 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);
}
}
});
如果 class 扩展 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 的线性布局中创建了 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);
}
}
});