Displaying soft keyboard whenever AlertDialog.Builder object is opened

后端 未结 14 1803
半阙折子戏
半阙折子戏 2020-12-08 19:11

My code for opening an input dialog reads as follows:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);  
alert.setTitle(\"Dialog Title\");  
         


        
14条回答
  •  佛祖请我去吃肉
    2020-12-08 19:57

    This is in response to miannelle.

    The following method is called when a menu option is selected:

    private void addNote() {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.textentryalertdialog);
        dialog.setTitle("Add note");
        TextView msgText = (TextView) dialog.findViewById(R.id.messagetext);
        msgText.setText("Whatever prompt you want");
        final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext);
        Button okButton = (Button) dialog.findViewById(R.id.OKButton);
        okButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                dialog.dismiss();
                // app specific code
            }           
        });
        Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton);
        cancelButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                dialog.dismiss();
            }           
        });
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog.show();
    }
    

    The textentryalertdialog.xml file defines a linear layout containing

    TextView android:id="@+id/messagetext" ...

    EditText android:id="@+id/my_edittext" ...

    Button android:id="@+id/OKButton" ...

    Button android:id="@+id/CancelButton" ...

    I hope this helps.

提交回复
热议问题