How to add two edit text fields in an alert dialog

后端 未结 8 984
野趣味
野趣味 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 16:57

    Have a look at the AlertDialog docs. As it states, to add a custom view to your alert dialog you need to find the frameLayout and add your view to that like so:

    FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
    fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
    

    Most likely you are going to want to create a layout xml file for your view, and inflate it:

    LayoutInflater inflater = getLayoutInflater();
    View twoEdits = inflater.inflate(R.layout.my_layout, f1, false);
    
    0 讨论(0)
  • 2020-12-07 16:58

    Use these lines in the code, because the textEntryView is the parent of username edittext and password edittext.

        final EditText input1 = (EditText) textEntryView .findViewById(R.id.username); 
        final EditText input2 = (EditText) textEntryView .findViewById(R.id.password); 
    
    0 讨论(0)
  • 2020-12-07 17:03

    I found another set of examples for customizing an AlertDialog from a guy named Mossila. I think they're better than Google's examples. To quickly see Google's API demos, you must import their demo jar(s) into your project, which you probably don't want.

    But Mossila's example code is fully self-contained. It can be directly cut-and-pasted into your project. It just works! Then you only need to tweak it to your needs. See here

    0 讨论(0)
  • 2020-12-07 17:07

    The API Demos in the Android SDK have an example that does just that.

    It's under DIALOG_TEXT_ENTRY. They have a layout, inflate it with a LayoutInflater, and use that as the View.

    EDIT: What I had linked to in my original answer is stale. Here is a mirror.

    0 讨论(0)
  • 2020-12-07 17:11
                   /* Didn't test it but this should work "out of the box" */
    
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    //you should edit this to fit your needs
                    builder.setTitle("Double Edit Text");
    
                    final EditText one = new EditText(this);
                    from.setHint("one");//optional
                    final EditText two = new EditText(this);
                    to.setHint("two");//optional
    
                    //in my example i use TYPE_CLASS_NUMBER for input only numbers
                    from.setInputType(InputType.TYPE_CLASS_NUMBER);
                    to.setInputType(InputType.TYPE_CLASS_NUMBER);
    
                    LinearLayout lay = new LinearLayout(this);
                    lay.setOrientation(LinearLayout.VERTICAL);
                    lay.addView(one);
                    lay.addView(two);
                    builder.setView(lay);
    
                    // Set up the buttons
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                           //get the two inputs
                           int i = Integer.parseInt(one.getText().toString());
                           int j = Integer.parseInt(two.getText().toString());
                      }
                    });
    
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                           dialog.cancel();
                    }
                  });
                  builder.show();
    
    0 讨论(0)
  • 2020-12-07 17:12

    Check this code in alert box have edit textview when click OK it displays on screen using toast.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString().trim();
                Toast.makeText(getApplicationContext(), value, 
                    Toast.LENGTH_SHORT).show();
            }
        });
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });
        alert.show();               
    }
    
    0 讨论(0)
提交回复
热议问题