简体   繁体   English

我希望我的警报对话框触发并保持显示错误消息

[英]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如果工作请批准

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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