EditText
親の下部に合わせてボタンを配置しています。
テキストを入力してボタンを押してデータを保存すると、仮想キーボードが消えません。
キーボードを非表示にする方法を教えてもらえますか?
EditText
親の下部に合わせてボタンを配置しています。
テキストを入力してボタンを押してデータを保存すると、仮想キーボードが消えません。
キーボードを非表示にする方法を教えてもらえますか?
EditText 内で imeOptions を定義することもできます。このように、[完了] を押すと、キーボードが消えます。
<EditText
android:id="@+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
これは機能するはずです。
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
this.getCurrentFocus()がnullを返さないことを確認してください。これは、フォーカスがない場合に返されます。
mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do something, e.g. set your TextView here via .setText()
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
そしてxmlで
android:imeOptions="actionDone"
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
この方法を使用している人を見たことがありません。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (focused)
keyboard.showSoftInput(editText, 0);
else
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
次に、editText にフォーカスをリクエストします。
editText.requestFocus();
EditText アクションリスナーに含まれるソリューション:
public void onCreate(Bundle savedInstanceState) {
...
...
edittext = (EditText) findViewById(R.id.EditText01);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
...
...
}
I found this because my EditText wasn't automatically getting dismissed on enter.
This was my original code.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {
// Do stuff when user presses enter
return true;
}
return false;
}
});
I solved it by removing the line
return true;
after doing stuff when user presses enter.
Hope this helps someone.
マークされた回答が上部に表示されます。しかし、私は使用getDialog().getCurrentFocus()
してうまく機能しています。"this"
oncreatedialog に入力できないため、この回答を投稿します。
これが私の答えです。マークされた回答を試してもうまくいかなかった場合は、次のことを試してみてください。
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
過去数日間これに苦労しており、非常にうまく機能する解決策を見つけました。EditText 以外の場所でタッチすると、ソフト キーボードは非表示になります。
ここに投稿されたコード: Android でクリック時にデフォルトのキーボードを非表示にする
int klavStat = 1; // for keyboard soft/hide button
int inType; // to remeber your default keybort Type
エディター - EditText フィールドです
/// metod for onclick button ///
public void keyboard(View view) {
if (klavStat == 1) {
klavStat = 0;
inType = editor.getInputType();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
editor.setInputType(InputType.TYPE_NULL);
editor.setTextIsSelectable(true);
} else {
klavStat = 1;
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
editor.setInputType(inType);
}
}
別の EditText フィールドがある場合は、フォーカスの変更を監視する必要があります。
このメソッドを使用して、編集テキストからキーボードを削除します。
public static void hideKeyboard(Activity activity, IBinder binder) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (binder != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
inputManager.showSoftInputFromInputMethod(binder, 0);
}
}
}
そして、アクティビティからキーボードを削除するこの方法 (場合によっては機能しません。たとえば、edittext がキーボードにバインドされている場合、フォーカスを失った場合は機能しません。しかし、他の状況ではうまく機能し、あなたは持っていませんキーボードを保持する要素を気にする)。
public static void hideKeyboard(Activity activity) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
editText.setInputType(InputType.TYPE_NULL);