对话框没有出现,数据仍然插入到DB中,而它的空...所有关于对话框的代码我已经使用但仍然可以运行。只是想在我当前页面中出现一个警告框,表示edittext为空,需要用户填写..
if (name == null || inputName.getText().toString().length() == 0)
{
if (price == null || inputPrice.getText().toString().length() == 0)
{
if (description == null || inputDesc.getText().toString().length() == 0)
{
// creating new product in background thread
new CreateNewProduct().execute();
}
else
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this) ;
// set the message to display
alertbox.setMessage("This is the alertbox!");
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener()
{
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
// show it
alertbox.show();
}
}
else{
AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this);
// set the message to display
alertbox.setMessage("This is the alertbox!");
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
// show it
alertbox.show();
}
}else{
AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this);
// set the message to display
alertbox.setMessage("This is the alertbox!");
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
// show it
alertbox.show();
}
}
});
答案 0 :(得分:0)
为什么不使用这种方法?
if (name == null || inputName.getText().toString().equals("")
|| price == null || inputPrice.getText().toString().equals("")
|| description == null || inputDesc.getText().toString().equals("")){
AlertDialog.Builder alertbox = new AlertDialog.Builder(NewProductActivity.this);
// set the message to display
alertbox.setMessage("This is the alertbox!");
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
// show it
alertbox.show();
}
else
{
//Insert into database
}
答案 1 :(得分:0)
比较字符串使用:
YourString.equals("")
你可以使用
name.equals("") == true
而不是
name== null
你的if语句中的
答案 2 :(得分:0)
但是当EditText不为空时,您显示警告,因此更改您的代码,以便在EditText为空时显示警告:
if (!name.equals("") || inputName.getText().toString().length() != 0){
if (!price.equals("") inputPrice.getText().toString().length() != 0)
{
if (!description.equals("") || inputDesc.getText().toString().length() != 0)
{
// creating new product in background thread
new CreateNewProduct().execute();
}
else{
// your code here
}
});