Android - Custom Dialog - Can't get text from EditText

前端 未结 6 782
清歌不尽
清歌不尽 2020-12-01 12:00

I have a problem with a custom dialog.
My dialog consists of a TextView, EditText and an \"Ok\" Button. After clicking \"Ok\", it should get t

6条回答
  •  青春惊慌失措
    2020-12-01 12:49

    If you want to inflate a xml file into dialog box for creating custom version you can use the following code which gets two input from user

    LayoutInflater linf = LayoutInflater.from(this);            
    final View inflator = linf.inflate(R.layout.twoinputs, null);
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    
    alert.setTitle("Tilte"); 
    alert.setMessage("Message"); 
    alert.setView(inflator); 
    
    final EditText et1 = (EditText) inflator.findViewById(R.id.editText1);
    final EditText et2 = (EditText) inflator.findViewById(R.id.editText2);
    
    alert.setPositiveButton("ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) 
       { 
              String s1=et1.getText().toString();
              String s2=et2.getText().toString();
              //do operations using s1 and s2 here...
       } 
    }); 
    
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
                dialog.cancel(); 
       } 
    }); 
    
    alert.show(); 
    

提交回复
热议问题