在不调用解雇的情况下解除警报对话框

时间:2013-12-17 06:52:31

标签: android dialog android-widget alertdialog

在我的程序中,我正在为 空编辑文本 显示Toast,我的对话框中的edittext 以及上传按钮 < / strong>,现在关于我的程序和问题

像:

  1. PersonName 字段不能留空

  2. 如果 PersonName字段为空,则显示Toast (只要用户在提醒对话框中 点击上传按钮

  3. 没有调用解雇 关闭我的AlertDialog

  4. 在我的AlertDialog中,我有 4 EditTexts 3个按钮和一些 其他小部件

    UploadActivity.java: -

    private boolean SaveData() {                        
    
     final AlertDialog.Builder ad = new AlertDialog.Builder(UploadActivity.this);                    
      ad.setTitle("StreamReader");                        
      ad.setIcon(android.R.drawable.btn_star_big_on);
      ad.setPositiveButton("Close", null);      
      if(editPersonName.getText().length() == 0)
      {
      Toast.makeText(getApplicationContext(), "Person Name field cannot be left blank", Toast.LENGTH_LONG).show();
      return false ;
      }
      String 
      url = "http://domain.tld/sendData.jsp";       
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new BasicNameValuePair("sImageName", editImageName.getText().toString()));
    
      String resultServer  = getHttpPost(url,params);
      Log.d("Entire string::", " " + resultServer);
      /*** Default Value ***/
      String strStatusID = "0";
      String strError = "Data Stored to Server";
      JSONObject c;
      try {
        c = new JSONObject(resultServer);
        strStatusID = c.getString("StatusID");
        strError = c.getString("Message");
           } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
         }
        // prepare save data
        if(strStatusID.equals("0"))
        {
          Toast.makeText(getApplicationContext(), strError.toString(), Toast.LENGTH_LONG).show();
        }
        else
        {
        Toast.makeText(getApplicationContext(), "Data Uploaded Successfully!", Toast.LENGTH_LONG).show();                           
        }
         return true;
        }
    

2 个答案:

答案 0 :(得分:2)

在调用SaveData方法之前验证您的编辑文本。并从SaveData中删除验证码:

所以处理这样的点击事件:

uploadButton.setOnClickListener(new View.OnClickListener(){
            @Override
                    public void onClick(View v) {

                if(editPersonName.getText().length() == 0)
              {
                  Toast.makeText(getApplicationContext(), "Person Name field cannot be left blank", Toast.LENGTH_LONG).show();

              }
                else
                {
                    SaveData();
                }
            }

现在您的SaveData将如下所示:

private void SaveData() {    

      String
      url = "http://domain.tld/sendData.jsp";      
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new BasicNameValuePair("sImageName", editImageName.getText().toString()));

      String resultServer  = getHttpPost(url,params);
      Log.d("Entire string::", " " + resultServer);
      /*** Default Value ***/
      String strStatusID = "0";
      String strError = "Data Stored to Server";
      JSONObject c;
      try {
            c = new JSONObject(resultServer);
            strStatusID = c.getString("StatusID");
           strError = c.getString("Message");
       } catch (JSONException e) {
       // TODO Auto-generated catch block
          e.printStackTrace();
       }

      // prepare save data
      if(strStatusID.equals("0"))
        {
             Toast.makeText(getApplicationContext(), strError.toString(),   Toast.LENGTH_LONG).show();
       }
        else
        {
              Toast.makeText(getApplicationContext(), "Data Uploaded Successfully!", Toast.LENGTH_LONG).show();                          
        }
   }

答案 1 :(得分:0)

private boolean SaveData() {                        

     final AlertDialog.Builder ad = new AlertDialog.Builder(UploadActivity.this);                    
      ad.setTitle("StreamReader");                        
      ad.setIcon(android.R.drawable.btn_star_big_on);
      ad.setPositiveButton("Close", null);   
      return validateAndShare(editPersonName.getText().toString());

}

private boolean validateAndShare(String editedText){
    if(editedText!= null && !editedText.trim().equals(""))
      {

      String 
      url = "http://domain.tld/sendData.jsp";       
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new BasicNameValuePair("sImageName", editImageName.getText().toString()));

      String resultServer  = getHttpPost(url,params);
      Log.d("Entire string::", " " + resultServer);
      /*** Default Value ***/
      String strStatusID = "0";
      String strError = "Data Stored to Server";
      JSONObject c;
      try {
        c = new JSONObject(resultServer);
        strStatusID = c.getString("StatusID");
        strError = c.getString("Message");
           } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
         }
        // prepare save data
        if(strStatusID.equals("0"))
        {
          Toast.makeText(getApplicationContext(), strError.toString(), Toast.LENGTH_LONG).show();
        }
        else
        {
        Toast.makeText(getApplicationContext(), "Data Uploaded Successfully!", Toast.LENGTH_LONG).show();                           
        }
         return true;
        }else{

            return false;
             Toast.makeText(getApplicationContext(), "Person Name field cannot be left blank", Toast.LENGTH_LONG).show();
             //Again open the dialog 
             SaveData();

        }

我不知道这会起作用。这是我的假设。用你的代码检查一下。如果它工作正常,那么请回答我的答案。 :)