How to add two edit text fields in an alert dialog

后端 未结 8 995
野趣味
野趣味 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条回答
  •  萌比男神i
    2020-12-07 17:20

    Check the following code. It shows 2 edit text fields programmatically without any layout xml. Change 'this' to 'getActivity()' if you use it in a fragment.

    The tricky thing is we have to set the second text field's input type after creating alert dialog, otherwise, the second text field shows texts instead of dots.

        public void showInput() {
            OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() {
                @Override
                public void onFocusChange(final View v, boolean hasFocus) {
                    if (hasFocus) {
                        // Must use message queue to show keyboard
                        v.post(new Runnable() {
                            @Override
                            public void run() {
                                InputMethodManager inputMethodManager= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                                inputMethodManager.showSoftInput(v, 0);
                            }
                        });
                    }
                }
            };
    
            final EditText editTextName = new EditText(this);
            editTextName.setHint("Name");
            editTextName.setFocusable(true);
            editTextName.setClickable(true);
            editTextName.setFocusableInTouchMode(true);
            editTextName.setSelectAllOnFocus(true);
            editTextName.setSingleLine(true);
            editTextName.setImeOptions(EditorInfo.IME_ACTION_NEXT);
            editTextName.setOnFocusChangeListener(onFocusChangeListener);
    
            final EditText editTextPassword = new EditText(this);
            editTextPassword.setHint("Password");
            editTextPassword.setFocusable(true);
            editTextPassword.setClickable(true);
            editTextPassword.setFocusableInTouchMode(true);
            editTextPassword.setSelectAllOnFocus(true);
            editTextPassword.setSingleLine(true);
            editTextPassword.setImeOptions(EditorInfo.IME_ACTION_DONE);
            editTextPassword.setOnFocusChangeListener(onFocusChangeListener);
    
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.addView(editTextName);
            linearLayout.addView(editTextPassword);
    
            DialogInterface.OnClickListener alertDialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        // Done button clicked
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        // Cancel button clicked
                        break;
                    }
                }
            };
            final AlertDialog alertDialog = (new AlertDialog.Builder(this)).setMessage("Please enter name and password")
                    .setView(linearLayout)
                    .setPositiveButton("Done", alertDialogClickListener)
                    .setNegativeButton("Cancel", alertDialogClickListener)
                    .create();
    
            editTextName.setOnEditorActionListener(new OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    editTextPassword.requestFocus(); // Press Return to focus next one
                    return false;
                }
            });
            editTextPassword.setOnEditorActionListener(new OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    // Press Return to invoke positive button on alertDialog.
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
                    return false;
                }
            });
    
            // Must set password mode after creating alert dialog.
            editTextPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            editTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            alertDialog.show();
        }
    

提交回复
热议问题