Null Validation on EditText box in Alert Dialog - Android

后端 未结 6 1356
有刺的猬
有刺的猬 2021-01-02 23:15

I am trying to add some text validation to an edit text field located within an alert dialog box. It prompts a user to enter in a name.

I want to add some validatio

6条回答
  •  佛祖请我去吃肉
    2021-01-03 00:08

    Use This code for displaying Dialog.

     public void onClick(DialogInterface dialog, int whichButton) {
    
                String textSt`enter code here`ring = trackName.getText().toString(); // Converts the value of getText to a string.
                if (textString != null && textString.trim().length() ==0)
                {       
                    Context context = getApplicationContext();
                    CharSequence error = "Please enter a track name" + textString;
                    int duration = Toast.LENGTH_LONG;
    
                    Toast toast = Toast.makeText(context, error, duration);
                    toast.show();
    
                    new AlertDialog.Builder(this)
                    .setTitle("Message")
                    .setMessage("please enter valid field")
                    .setPositiveButton("OK", null).show();            
                }
    

    This will create a Dialog for you, editText is empty or what are conditions you wants.

提交回复
热议问题