사실,이보기가 동일한 창에서 나올 때까지는 상관 없습니다. 문서로 는 View.getWindowToken() 말했다 :
이보기에 를 부착 창을 식별하는 고유 한 토큰을 검색합니다.
모든 활동보기가 동일한 창에 연결됩니다. 따라서 모든보기에서 동일한 토큰을 얻을 수 있습니다.
그러나 키보드가 보이고 대화 상자가 표시되는 경우 이 대화 상자는 일부 사용자 지정보기를해야합니다 말 :
dialog_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tryHideKeyboard"
android:text="Try hide keyboard"/>
</LinearLayout>
TestDialogFragment.java을
public class TestDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_layout, null);
view.findViewById(R.id.tryHideKeyboard).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
// Use the Builder class for convenient dialog construction
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test dialog")
.setView(view)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
// nothing to do
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
그런 다음 활동에서 연 대화 (에 Try hide keyboard
버튼을 눌러 예를 들어 주 레이아웃의 EditText 및 키보드 표시) 대화 상자에는 자체 창이 있기 때문에 키보드 숨김이 닫히지 않습니다.
그래서 다른 창 (예 : 팝업 대화 상자)에 연결된보기에서 키보드를 닫으려고해도 문제가되지 않습니다.
함께 사용해 주셔서 감사합니다. –