簡體   English   中英

對話框警報edittext為空,需要填寫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM