如何在对话框中显示/隐藏Android软键盘?

use*_*407 10 android

在我的应用程序中,自定义对话框位于BaseExpandableListAdapter类中.在对话框中我有两个编辑文本.首先是名称及其强制性.第二个是解决它的选择问题.两个按钮确定并取消.当Dialog显示我想显示带有请求焦点的键盘以编辑文本名称.单击确定按钮后,软键盘应该隐藏.

use*_*407 15

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);  
   }
 }
Run Code Online (Sandbox Code Playgroud)


swa*_*ati 13

点击确定按钮写下面的代码: -

  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();
Run Code Online (Sandbox Code Playgroud)

将上下文定义为Context context = this.


Sys*_*ech 5

使用以下代码隐藏键盘

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

使用以下代码显示键盘

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)