【发布时间】:2016-06-25 05:34:57
【问题描述】:
我是 android 的新手,正在开发警报对话框的演示,一旦单击警报中的一个按钮,我想关闭软键盘。我已经尝试过编程但键盘仍然打开,请你帮忙我这个问题, 代码
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();
}
【问题讨论】:
标签: android android-alertdialog android-softkeyboard android-input-method