我正在尝试将所有警报框放在我的代码中..但仍然无法运行...当editText
为空或null时。它将显示一个需要由用户填写的对话框。我已经尝试了所有步骤和所有对话框警告框。但它仍然有效。但在我的情况下......这涉及3个以上的edittext。只需要一个意见,我应该把错误空编辑文本的代码放在哪里,并且需要用户在按下按钮之前填写它。
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;
// url to create new product
private static String url_create_product = "http://192.168.0.102/android_connect/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
//private static final CharSequence TITLE_DIALOG = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
// Edit Text
inputName = (EditText) findViewById(R.id.inputName);
inputPrice = (EditText) findViewById(R.id.inputPrice);
inputDesc = (EditText) findViewById(R.id.inputDesc);
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
List<EditText> editTextList = new ArrayList<EditText>();
//editTextList.add(myEditText);
String name = inputName.getText().toString();
String price = inputPrice.getText().toString();
String description = inputDesc.getText().toString();
for(EditText inputName : editTextList)
{
if (name == null || inputName.getText().toString().length() == 0)
{
Context context = getApplicationContext();
CharSequence error = "Please enter a track name" + name;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, error, duration);
toast.show();
}
else {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
答案 0 :(得分:0)
我实际上会这样做有点不同......错误检查代码应该是Dialog onClick按钮的一部分。检查是否输入了所有值,如果不是,则输入用户需要填写该框的指示符,同时未完成对话框。以下是我的一个程序中的一个示例,在这种情况下,获取一个名称以添加到高分例程。
EditText input;
input=new EditText(this.getApplicationContext());
final int index=i;
new AlertDialog.Builder(this)
.setTitle("Update Status")
.setMessage(R.string.getName)
.setView(input)
.setPositiveButton(sayings_array[rnum], new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
String name=value.toString();
if (name!=null)
{
addToHighScore(name,level,score,index);
dialog.dismiss();
}
}
}).show();
答案 1 :(得分:0)
在开始执行AsyncTask
之前,检查Edittext是否为null。只需首先检查EditText为空,然后点击按钮,然后启动CreateNewProduct
AsyncTask
。将您的代码更改为:
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputName.getText().toString();
String price = inputPrice.getText().toString();
String description = inputDesc.getText().toString();
if (name != null || inputName.getText().toString().length() != 0)
{
if (price != null || price.getText().toString().length() != 0)
{
if (description != null || description.getText().toString().length() != 0)
{
// creating new product in background thread
new CreateNewProduct().execute();
}
else{
//show alert here
}
}else{
//show alert here
}
}else{
//show alert here
}
}
});