我正在尝试进行一些数据验证。简而言之,当按下添加按钮时,如果某些字段未填写,那么我想显示一个消息框并从进一步处理中返回。
这是我的代码流程,没有 messageBox 代码:
Button add = (Button) findViewById(R.id.addButton);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//local vars
String access;
String gender;
String avail;
String availCode;
// getting values from selected editItems
String name = textName.getText().toString();
String street = textStreet.getText().toString();
String city = textCity.getText().toString();
String state = textState.getText().toString();
String postal = textPostal.getText().toString();
String country = textCountry.getText().toString();
String directions = textDirections.getText().toString();
String comments = textComments.getText().toString();
//verify miniminal data
if((name.equals("")) || (street.equals(""))|| (city.equals("")) || (state.equals("")) || (postal.equals("")) || (country.equals("")))
{
}
我尝试粘贴此代码:
//verify miniminal data
if((name.equals("")) || (street.equals(""))|| (city.equals("")) || (state.equals("")) || (postal.equals("")) || (country.equals("")))
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setTitle("Title");
builder.setInverseBackgroundForced(true);
builder.setMessage("Must enter minimal data.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
});
AlertDialog alert = builder.create();
alert.show();
}
但是......我无法建立这条线:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
Eclipse 说上下文不能解析为变量。
我对该怎么做感到困惑。有人可以帮忙吗?