Null Validation on EditText box in Alert Dialog - Android

后端 未结 6 1340
有刺的猬
有刺的猬 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-02 23:55

    I think you should recreate the Dialog, as it seems the DialogInterface given as a parameter in onClick() doesn't give you an option to stop the closure of the Dialog.

    I also have a couple of tips for you:

    Try using Activity.onCreateDialog(), Activity.onPrepareDialog() and of course Activity.showDialog(). They make dialog usage much easier (atleast for me), also dialog usage looks more like menu usage. Using these methods, you will also be able to more easilty show the dialog again.

    I want to give you a tip. It's not an answer to your question, but doing this in an answer is much more readable.

    Instead of holding a reference to an AlertDialog.Builder() object, you can simply do:

    new AlertDialog.Builder(this)
    .setTitle("Record New Track")
    .setMessage("Please Name Your Track:")
    //and some more method calls
    .create();
    //or .show();
    

    Saves you a reference and a lot of typing ;). (almost?) All methods of AlertDialog.Builder return an AlertDialog.Builder object, which you can directly call a method on.

    The same goes for Toasts:

    Toast.makeText(this, "Please enter...", Toast.LENGTH_LONG).show();
    

提交回复
热议问题