How to make a edittext box in a dialog

后端 未结 8 1886
借酒劲吻你
借酒劲吻你 2020-11-28 01:34

I am trying to make a edittext box in a dialog box for entering a password. and when I am doing I am not able to do. I am a beginner in it. Please help me in this.



        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 01:54

    You can also create custom alert dialog by creating an xml file.

    dialoglayout.xml

       
    
        
      
       

    The Java Code:

    @Override//to popup alert dialog
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        showDialog(DIALOG_LOGIN);
    });
    
    
    @Override
    protected Dialog onCreateDialog(int id) {
        AlertDialog dialogDetails = null;
    
        switch (id) {
            case DIALOG_LOGIN:
                LayoutInflater inflater = LayoutInflater.from(this);
                View dialogview = inflater.inflate(R.layout.dialoglayout, null);
                AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
                dialogbuilder.setTitle("Title");
                dialogbuilder.setView(dialogview);
                dialogDetails = dialogbuilder.create();
                break;
        }
        return dialogDetails;
    }
    
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
            case DIALOG_LOGIN:
                 final AlertDialog alertDialog = (AlertDialog) dialog;
                 Button loginbutton = (Button) alertDialog
                    .findViewById(R.id.btn_login);
                 Button cancelbutton = (Button) alertDialog
                    .findViewById(R.id.btn_cancel);
                 userName = (EditText) alertDialog
                    .findViewById(R.id.dialog_txt_name);
                 loginbutton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          String name = userName.getText().toString();
                          Toast.makeText(Activity.this, name,Toast.LENGTH_SHORT).show();
                 });
                 cancelbutton.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                               alertDialog.dismiss();
                          }
                 });
                 break;
       }
    }
    

提交回复
热议问题