软键盘不会在 android 中以编程方式隐藏
Soft keyboard doesn't get hide programmatically in android
我是 android 的新手,正在开发警报对话框的演示,我想在警报按钮之一 clicked.I 以编程方式尝试过后关闭软键盘,但是键盘保持打开状态,你能帮我解决这个问题吗,
代码
public void Show_Dialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
SwipeActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.add_albom_dialog, null);
alertDialog.setView(layout);
final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
alertDialog.setPositiveButton("Create",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EditText txts = (EditText) layout
.findViewById(R.id.addAblum_edit);
hideSoftKeyboardDialogDismiss(SwipeActivity.this);
if(txts.getText().toString().trim().length() > 0) {
Add_album(txts.getText().toString());
} else {
AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Name can't be emtpy");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
}
});
alertDialog.show();
}
dialog.cancel(); // Your custom code
}
});
/* When negative (No/cancel) button is clicked */
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
hideSoftKeyboardDialogDismiss(SwipeActivity.this);
dialog.cancel();
// finish();
}
});
alertDialog.show();
}
试试下面的方法
final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
final AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Name can't be emtpy");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
inputManager.hideSoftInputFromInputMethod(alertDialog.getCurrentFocus().getWindowToken(), 0);
dialog.dismiss();
}
});
alertDialog.show();
使用你的 alertDailog
当前焦点而不是你的 activity
实际上肯定有延迟所以使用此代码
public static void hideSoftKeyboardDialogDismiss(final Activity activity) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus()) {
inputMethodManager.hideSoftInputFromWindow(activity
.getCurrentFocus().getWindowToken(), 0);
}
}
});
}
}, 1);
}
dialog.setOnDissmissListener(){
void onDismiss(){
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
}
}
dialog.dismiss();
试试这个:
protected void hideSoftKeyboard(EditText mSearchView) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
}
我是 android 的新手,正在开发警报对话框的演示,我想在警报按钮之一 clicked.I 以编程方式尝试过后关闭软键盘,但是键盘保持打开状态,你能帮我解决这个问题吗, 代码
public void Show_Dialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
SwipeActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.add_albom_dialog, null);
alertDialog.setView(layout);
final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
alertDialog.setPositiveButton("Create",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EditText txts = (EditText) layout
.findViewById(R.id.addAblum_edit);
hideSoftKeyboardDialogDismiss(SwipeActivity.this);
if(txts.getText().toString().trim().length() > 0) {
Add_album(txts.getText().toString());
} else {
AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Name can't be emtpy");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
}
});
alertDialog.show();
}
dialog.cancel(); // Your custom code
}
});
/* When negative (No/cancel) button is clicked */
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
hideSoftKeyboardDialogDismiss(SwipeActivity.this);
dialog.cancel();
// finish();
}
});
alertDialog.show();
}
试试下面的方法
final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
final AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Name can't be emtpy");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
inputManager.hideSoftInputFromInputMethod(alertDialog.getCurrentFocus().getWindowToken(), 0);
dialog.dismiss();
}
});
alertDialog.show();
使用你的 alertDailog
当前焦点而不是你的 activity
实际上肯定有延迟所以使用此代码
public static void hideSoftKeyboardDialogDismiss(final Activity activity) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus()) {
inputMethodManager.hideSoftInputFromWindow(activity
.getCurrentFocus().getWindowToken(), 0);
}
}
});
}
}, 1);
}
dialog.setOnDissmissListener(){
void onDismiss(){
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
}
}
dialog.dismiss();
试试这个:
protected void hideSoftKeyboard(EditText mSearchView) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
}