0

一旦启动,我就会显示一个对话框MainActivity。它提示用户输入 2 个参数,一个表号和一个封面号。然后他们确认并使用这些参数创建一个 Order 对象。

但是我遇到的问题是,无论他们是否输入参数,当他们单击确认按钮时,该框都会被关闭。我希望该框保持显示并显示Toast概述错误的消息。

下面是我的有效代码,但是当单击按钮时,该框会消失并且吐司也会显示出来。

非常感谢。

public Dialog createDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(SectionsActivity.this);

    View v = getLayoutInflater().inflate(R.layout.order_dialog, null);
    builder.setView(v);

    tableNum = (EditText)v.findViewById(R.id.numberEntry);
    coverNum = (EditText)v.findViewById(R.id.coversEntry);

    builder.setMessage("Order Information");
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

            if(tableNum.getText().length() != 0 && coverNum.length() != 0){
            //Create order object in here
            table = Integer.parseInt(tableNum.getText().toString());
            covers = Integer.parseInt(coverNum.getText().toString());
            Order.getInstance().setTableNumber(table);
            Order.getInstance().setCoverNumber(covers);
            created = true;
            }else{
                Toast.makeText(getApplicationContext(), "Must enter a table and cover number", Toast.LENGTH_SHORT).show();
            }
        }
    });

    return builder.create();
}

编辑:

if(!Order.getInstance().getCreated()){
    Dialog d = createDialog();
    d.show();
    }
4

2 回答 2

1

试试这个

 AlertDialog alertDialog = builder.create();

 builder.setOnShowListener(new DialogInterface.OnShowListener() {

                @Override
                public void onShow(DialogInterface dialog) {
                     Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                     b.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String strComments = youredittext.getText().toString().trim();   // get your Textbox value here
                            if(strComments==null || strComments.length()==0)
                            {
                                Toast.makeText(context, "Please Enter General Comments", 1).show();
                            }
                            else
                            {
                                alertDialog.dismiss();
                                // do stuff if success

                            }
                        }
                    });

                }
            });

我们正在做的是,只是覆盖 AlertDialog Positive 按钮,以避免在未在字段中输入值的情况下关闭按钮

编辑:

public Dialog createDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(SectionsActivity.this);

    View v = getLayoutInflater().inflate(R.layout.order_dialog, null);
    builder.setView(v);

    tableNum = (EditText)v.findViewById(R.id.numberEntry);
    coverNum = (EditText)v.findViewById(R.id.coversEntry);

    builder.setMessage("Order Information");
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

            if(tableNum.getText().length() != 0 && coverNum.length() != 0){
            //Create order object in here
            table = Integer.parseInt(tableNum.getText().toString());
            covers = Integer.parseInt(coverNum.getText().toString());
            Order.getInstance().setTableNumber(table);
            Order.getInstance().setCoverNumber(covers);
            created = true;
            }else{
                Toast.makeText(getApplicationContext(), "Must enter a table and cover number", Toast.LENGTH_SHORT).show();
            }
        }
    });

 AlertDialog alertDialog = builder.create();

builder.setOnShowListener(new DialogInterface.OnShowListener() {

                @Override
                public void onShow(DialogInterface dialog) {
                     Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                     b.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String strComments = youredittext.getText().toString().trim();   // get your Textbox value here
                            if(strComments==null || strComments.length()==0)
                            {
                                Toast.makeText(context, "Please Enter General Comments", 1).show();
                            }
                            else
                            {
                                alertDialog.dismiss();
                                // do stuff if success

                            }
                        }
                    });

                }
            });

    return alertDialog ;
}
于 2013-05-09T13:13:41.977 回答
0
String tableNum="",coverNum="";
if(tableNum.gettext.toString() == "" && coverNum.gettext.toString() =="")
{
    Toast.makeText(context, "Please Enter General Comments", 1).show();
}
else 
{
    dialoge.show();
}
于 2013-05-09T13:28:04.280 回答