启动MainActivity
后,我会立即显示一个对话框。它会提示用户输入2个参数,一个表号和一个封面号。然后他们确认并且这些参数用于创建Order对象。
然而我遇到的问题是,无论他们是否输入参数,当他们点击确认按钮时该框被解除。我希望该框保持显示并显示一条概述错误的Toast
消息。
下面是我的代码,但是当点击按钮时,框会消失并显示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();
}
答案 0 :(得分: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 ;
}
答案 1 :(得分:0)
String tableNum="",coverNum="";
if(tableNum.gettext.toString() == "" && coverNum.gettext.toString() =="")
{
Toast.makeText(context, "Please Enter General Comments", 1).show();
}
else
{
dialoge.show();
}