我有一個RelativeLayout嵌套在另一個RelativeLayout中。默認情況下,它是隱藏的。當用戶點擊一個按鈕時,嵌套的RelativeLayout變得可見。該佈局包含一個EditText。當用戶完成打字,我想隱藏鍵盤:當試圖隱藏鍵盤時,getCurrentFocus爲null
<RelativeLayout
...
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cameraEmoji"
android:contentDescription="@string/camera_emoji"
android:src="@mipmap/ic_image_black_24dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/cameraText"
android:layout_marginStart="57dp" />
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/emojiTextView"
android:background="#80444444">
<EditText
android:id="@+id/actionEmojiText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#FFF"
android:imeActionId="@+id/finishText"
android:imeActionLabel="@string/action_complete_emoji_text"
android:imeOptions="actionDone"
android:maxLines="1"
android:singleLine="true"/>
</RelativeLayout>
...
</RelativeLayout>
活動:
cameraText = (ImageButton)findViewById(R.id.cameraText);
cameraText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
emojiTextView = (RelativeLayout) findViewById(R.id.emojiTextView);
emojiTextView.setVisibility(View.VISIBLE);
final EditText actionEmojiText = (EditText)findViewById(R.id.actionEmojiText);
actionEmojiText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == EditorInfo.IME_ACTION_UNSPECIFIED) {
emojiTextView.setVisibility(View.INVISIBLE);
View view = MainActivity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
dropTextIn(actionEmojiText);
}
return false;
}
});
}
});
getCurrentFocus
返回空但鍵盤仍然可見。如何在輸入EditText字段後隱藏鍵盤?
我解決了這個問題。檢查我的答案。 – Donato