[英]How to hide the soft keyboard on OK on an EditText and show it again
我在fragment
中有一個數字EditText
,當我選擇EditText
時,它會正常顯示鍵盤。 我想在輸入OK時隱藏鍵盤。 所以我使用了hide_keyboard()函數 ,它正常工作。
我遇到的問題是當我重新選擇EditText
,軟鍵盤不再出現了。 我嘗試過很多東西,但都沒有用。
有任何想法嗎?
這是我的EditText:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="7"
android:id="@+id/kip_time"
android:hint="Reflexion time"
android:layout_below="@+id/chronometer_kipling"
android:layout_alignStart="@+id/chronometer_kipling"
android:layout_marginTop="10dp"
/>
和我的hide_keyboard()函數:
private void hide_keyboard(Context context, View view) {
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
}
最后我的onclicklistener方法:
kip_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
reflexion_time = Integer.parseInt(kip_time.getText().toString());
reflexion_time = reflexion_time * 1000;
hide_keyboard(context, view);
}
});
如果您需要在輸入值后隱藏鍵盤,那么只需使用
機器人:imeOptions = “actionDone”
它在軟鍵盤上提供了一個“完成”按鈕,用戶可以在輸入值時單擊。 將其添加到EditText聲明並刪除hide_keyboard()函數。 更新布局xml如下。
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="7"
android:id="@+id/kip_time"
android:hint="Reflexion time"
android:layout_below="@+id/chronometer_kipling"
android:layout_alignStart="@+id/chronometer_kipling"
android:imeOptions="actionDone"
android:layout_marginTop="10dp"
/>
*處理完成按鈕點擊事件使用如下的聽眾*
kip_time.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// you codes gose here
//reflexion_time = Integer.parseInt(kip_time.getText().toString());
//reflexion_time = reflexion_time * 1000;
return true;
}
return false;
}
});
我通過這樣做解決了我的問題:
kip_time.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("OK", "Enter pressed");
reflexion_time = Integer.parseInt(kip_time.getText().toString());
reflexion_time = reflexion_time * 1000;
hide_keyboard();
}
return false;
}
});
使用隱藏鍵盤:
private void hide_keyboard() {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(kip_time.getWindowToken(), 0);
}
不需要show_keyboard()
使用以下方法隱藏鍵盤,並檢查單擊editText
時是否可以再次顯示鍵盤:
private void hideKeypad() {
View view = context.getCurrentFocus();
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (view instanceof EditText) {
inputManager.hideSoftInputFromWindow(context
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
如果它不是多行輸入,只需將其添加到EditText即可
android:singleLine="true"
用戶只需在軟鍵上選擇Enter / OK即可...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.