简体   繁体   中英

I want my alert dialog box to trigger and stay showing an error message

I want my alert dialog box to trigger and stay showing an error message if the user leaves the name field empty and clicks ok but my dialogue box disappears even if the user does not fill anything and click ok.Here is my code. Please suggest me the corrections I need to make.

    save2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final dbmanager db= new  dbmanager(cgpa3.this);

                    final AlertDialog.Builder alert = new AlertDialog.Builder(cgpa3.this);

//                            alert.setTitle("Enter a name");
                alert.setMessage("Enter student Name");

// Set an EditText view to get user input
                final EditText input = new EditText(cgpa3.this);
                alert.setView(input);


                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            String value = input.getText().toString();
                            if(value.isEmpty()){
                                Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
                                input.startAnimation(shake);
                                input.setError("Please enter student name");

                            }
                            else
                            {db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);

                            }

                        }
                    });

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // Canceled.
                        }
                    });

                    alert.show();

            };

        });

use this one,

       final EditText editText;

            final AlertDialog.Builder alert = new AlertDialog.Builder(DemoActivity.this);
                       alert.setTitle("Enter a name");
             alert.setMessage("Enter student Name");
            alert.setCancelable(false);


            editText = new EditText(DemoActivity.this);
            alert.setView(editText);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });

            final AlertDialog dialogs  = alert.create();
            dialogs.show();

            dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String value = editText.getText().toString();
                    if (value.isEmpty()) {
                        editText.setError("Please enter student name");
                    }
                    else{
                        dialogs.dismiss();
                    }

                }
            });

            dialogs.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    dialogs.dismiss();
                }
            });
if(TextUtils.isEmpty(input.getText().toString().trim())){
 Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
                            input.startAnimation(shake);
                            input.setError("Please enter student name");
}
else{
db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);
}

if working plese aprove

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM