[英]Dialog box alert edittext empty and need to fill it
我正在尝试将所有警报框放在我的代码中。但是当editText
为空或null时,仍然无法运行。 它会显示一个对话框,需要用户填写。 我已经尝试了所有步骤和所有对话框警报框。 但是它仍然起作用。 但就我而言...这涉及3个以上的edittext。 只是需要一种意见,我应该将错误代码空的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();
}
}
}
实际上,我的做法会大不相同...错误检查代码应该是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();
在开始执行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
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.