在我的应用程序中,自定义对话框位于BaseExpandableListAdapter类中。 在对话框中我有两个编辑文本。首先是名称及其强制性。第二个是解决它的选择问题。两个按钮确定并取消。当Dialog显示我想显示带有请求焦点的键盘以编辑文本名称。单击确定按钮后,软键盘应该隐藏。
答案 0 :(得分:13)
final Dialog dialog = new Dialog(_context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.prompts);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
final EditText name = (EditText) dialog.findViewById(R.id.name);
final EditText add = (EditText) dialog.findViewById(R.id.add);
Button btnok = (Button) dialog.findViewById(R.id.btn_ok);
Button btncancel = (Button) dialog.findViewById(R.id.btn_cancel);
btnAddExpList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
答案 1 :(得分:10)
单击“确定”按钮,编写以下代码: -
final Dialog dialog=new Dialog(this);
dialog.setContentView(R.layout.dialog);
final EditText text = (EditText) dialog.findViewById(R.id.nameField);
Button mOkBtn=(Button)dialog.findViewById(R.id.okBtn);
// if button is clicked, close the custom dialog
mOkBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
}
});
dialog.show();
将上下文定义为Context context = this。
答案 2 :(得分:2)
使用以下代码隐藏键盘
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
使用以下代码显示键盘
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
答案 3 :(得分:1)
在您的活动中使用此功能
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
答案 4 :(得分:0)
当您显示KeyBoard
时显示Dialog
,并按下KeyBoard
按钮关闭OK
,如下所示...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
alertDialogBuilder.setTitle(getString(R.string.app_error) + ":" + errorCode);
alertDialogBuilder.setMessage("Message");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); }
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
答案 5 :(得分:0)
这很容易,因为你自己回答了它。在显示对话框之前,通过xml或<requestfocus/>
通过代码将editText.requestFocus();
添加到EditText。
这可以通过两种方式实现,具体取决于您单击“确定”按钮时的操作。
一个。如果您要开始新的活动 - 在清单中向此活动添加android:windowSoftInputMode="stateHidden"
,那么每次活动开始时键盘都将被隐藏,除非您调用它。
湾如果您在同一页面上 - 请拨打以下方法。
private void hideSoftKeyBoard() {
try {
// hides the soft keyboard when the drawer opens
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
e.printStackTrace();
}
}
如果 getCurrentFocus()。getWindowToken()给出错误然后将任何View传递给它(你可以通过try catch块跟踪它),其中View可以是任何东西,Button,EditText等您的活动(myButton..getWindowToken()
)。
答案 6 :(得分:0)
使用此功能:
public void hideKeyboard() {
if (getDialog().getCurrentFocus() != null) {
InputMethodManager inputManager = (InputMethodManager) Objects.requireNonNull(getActivity()).getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
我希望它会有用
答案 7 :(得分:0)
这是一个解决方案:
private fun showCustomDialog() {
// Normal dialog stuff
// -----
val builder = AlertDialog.Builder(activity as Context)
val customLayout = View.inflate(context, R.layout.dialog_layout, null)
val editText: EditText = customLayout.findViewById(R.id.edit_text)
// -----
// Get a hold of the inpoutMethodManager here
val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
builder.setTitle(getText(R.string.dialog_title))
builder.setView(customLayout)
builder.setPositiveButton(getText(R.string.action_confirm)) { _, _ ->
// Hide the soft keyboard here after the positive button onclick
imm.hideSoftInputFromWindow(editText.windowToken, 0)
/*
* Do your logic here for the positive click
* ....
*/
}
builder.setNegativeButton(getText(R.string.action_cancel)) { dialog, _ ->
// Also hide the soft keyboard if the user clicked negative button
imm.hideSoftInputFromWindow(editText.windowToken, 0)
/*
* Do your logic here for the negative click
* ....
*/
dialog.cancel()
}
// added not cancelable for the dialog since it might mess with the keyboard hiding
builder.setCancelable(false)
builder.show()
// make sure you call this to request focus, or else the hiding might not work...
editText.requestFocus()
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
答案 8 :(得分:-1)
使用它。
protected void hideKeyboardDialog(Dialog dialog){
View view = dialog.getView();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}