3
我有一個EditText,我想控制鍵盤。當EditText有焦點時,鍵盤應該出現,然後一旦我點擊其他視圖,我想讓鍵盤消失。我嘗試下面的代碼,但它的工作EditText失去焦點時關閉鍵盤
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
}
});
你還有問題嗎?讓我知道如果你仍然需要幫助。 –