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

前端 未结 6 758
清歌不尽
清歌不尽 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:29

    Like @krishna but in kotlin

      val builder = AlertDialog.Builder(context)
        val inflater = requireActivity().layoutInflater
    
        val dialogView = inflater.inflate(R.layout.fav_dialog, null)
        val name = dialogView.findViewById<EditText>(R.id.username)
    
        builder.setView(dialogView)
            .setPositiveButton("Add") { dialog, id ->
                val foxName = name.text.toString()
                Log.v("miapp", "Myfox $foxName")
            }
            .setNegativeButton("Cancel") { dialog, id ->
                dialog.cancel()
            }
    
        builder.setTitle("Add to favs")
        builder.setCancelable(false).create().show()
    
    0 讨论(0)
  • 2020-12-01 12:30
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    

    http://developer.android.com/training/basics/firstapp/starting-activity.html

    0 讨论(0)
  • 2020-12-01 12:43

    An alternative to matsjoe and to krishna (both work):

    builder.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
        //@Override
        public void onClick(DialogInterface dialog, int id) {
          // sign in the user ...
    
          Dialog dialogObj =Dialog.class.cast(dialog);
          EditText etUsr=(EditText) dialogObj.findViewById(R.id.username_id_value);
          userStr = etUsr.getText().toString();      
        }
      }
      );
    

    Kf

    0 讨论(0)
  • 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(); 
    
    0 讨论(0)
  • 2020-12-01 12:50

    Hope it will help you.

    private void inputFromDialog() {
    
        final Dialog dialog = new Dialog(SplashActivity.this);
        dialog.setContentView(R.layout.view_dialog);
        dialog.setTitle("Title");
        dialog.setCancelable(false);
        dialog.show();
    
        Button button = (Button) dialog.findViewById(R.id.continue_BTN);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    
                EditText inputET=(EditText)dialog.findViewById(R.id.input_ET);
    
    
                if (inputET.getText().toString().equals("")){
    
                    inputET.setError("This field is required");
    
                }else {
    
                    mssid = inputET.getText().toString();
                    dialog.cancel();
                    Toast.makeText(SplashActivity.this, mssid, Toast.LENGTH_SHORT).show();
                }
    
            }
        });
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-01 12:52

    You are inflating a layout where it is not needed. I fixed your code as you see I removed your line where it inflates and changed the line where you try to find the EditText view.

    final Dialog dialog = new Dialog(MyActivity.this);
     dialog.setContentView(R.layout.custom_dialog);
     dialog.setTitle("Title");
    
     Button button = (Button) dialog.findViewById(R.id.dialog_ok);
     button.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
    
                EditText edit=(EditText)dialog.findViewById(R.id.dialog_edit);
                String text=edit.getText().toString();
    
                dialog.dismiss();
                name=text;
    
         }
     });   
    
    
    dialog.show();
    
    0 讨论(0)
提交回复
热议问题