How to add two edit text fields in an alert dialog

后端 未结 8 1009
野趣味
野趣味 2020-12-07 16:28

I am trying to use an alert dialog to prompt for a username and a password in android. I have found this code here:

  if (token.equals(\"Not Found\"))
    {
         


        
8条回答
  •  一个人的身影
    2020-12-07 17:14

     LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.text_entry, null);
    //text_entry is an Layout XML file containing two text field to display in alert dialog
    final EditText input1 = (EditText) textEntryView.findViewById(R.id.EditText1);
    final EditText input2 = (EditText) textEntryView.findViewById(R.id.EditText2);             
    input1.setText("DefaultValue", TextView.BufferType.EDITABLE);
    input2.setText("DefaultValue", TextView.BufferType.EDITABLE);
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
    alert.setIcon(R.drawable.icon)
         .setTitle("Enter the Text:")
         .setView(textEntryView)
         .setPositiveButton("Save", 
             new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int whichButton) {
                        Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
                        Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
                        /* User clicked OK so do some stuff */
                 }
             })
         .setNegativeButton("Cancel",
             new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog,
                        int whichButton) {
                 }
             });
    alert.show();
    

提交回复
热议问题