1

在我的应用程序中,我有带有edittext和提交按钮的dailog,如果edittext为空,我给了一个祝酒词“请输入一些东西”..我的代码如下

Button reviewPost = (Button) dialog.findViewById(R.id.submit);
        reviewPost.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setEnabled(false);
                    EditText ratingComment = (EditText) dialog
                            .findViewById(R.id.reviewcomment);
                    String comment = ratingComment.getText().toString();



                    String postStatus = "Y";
                    if (spinner.getSelectedItemPosition() > 0) {
                        postStatus = "N";
                    }
                    RadioGroup rating = (RadioGroup) dialog
                            .findViewById(R.id.customrating);
                    int radioButtonID = rating.getCheckedRadioButtonId();
                    View radioButton = rating.findViewById(radioButtonID);
                    int ratingNo = rating.indexOfChild(radioButton);

                    String ratingValue = (ratingNo + 1) + "";

                    if (comment != null && !comment.equalsIgnoreCase("")){

                    new PostReviewMessage().execute(activity, comment,
                            ratingValue, postStatus); 
                    activity.finish();
                    }else{
                         Toast.makeText(activity, "Please enter your comment", Toast.LENGTH_SHORT).show();

                     }  
                    Constants.rivewDialog = new ProgressDialog(activity);
                    Constants.rivewDialog.setCanceledOnTouchOutside(false);
                    Constants.rivewDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    Constants.rivewDialog.setMessage(activity.getString(R.string.postingrivew));
                    Constants.rivewDialog.setOnCancelListener(new OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface arg0) {

                        }
                    });
                    Constants.rivewDialog.show();
                    dialog.dismiss();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                }
                return false;
            }
            }); 

但即使edittext为空,它也会提交不应该发生的情况。我正在检查条件如下但它不起作用

if (comment != null && !comment.equalsIgnoreCase("")){

                    new PostReviewMessage().execute(activity, comment,
                            ratingValue, postStatus); 
                    activity.finish();
                    }else{
                         Toast.makeText(activity, "Please enter your comment", Toast.LENGTH_SHORT).show();

                     }
4

4 回答 4

0

使用comment.trim().equals("")而不是comment.equalsIgnoreCase("")

或检查注释字符串长度是否应超过最小字符。

于 2013-04-03T06:47:04.880 回答
0

利用

if(comment != null && !comment.isEmpty())
于 2013-04-03T06:51:39.320 回答
0

你可以用这个。

if(comment != null && !comment.isEmpty() && comment.length()!=0){
    //your task
}else{
  //give some message
}
于 2013-04-03T06:53:53.860 回答
0

用下面的代码解决了..问题出在 v.setEnabled(false);...我把它移到了 if 条件

if (comment != null && !comment.equalsIgnoreCase("")){
                            v.setEnabled(false);
                            new PostReviewMessage().execute(activity, comment,
                                    ratingValue, postStatus); 
                            Constants.rivewDialog = new ProgressDialog(activity);
                            Constants.rivewDialog.setCanceledOnTouchOutside(false);
                            Constants.rivewDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                            Constants.rivewDialog.setMessage(activity.getString(R.string.postingrivew));


                            Constants.rivewDialog.setOnCancelListener(new OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface arg0) {

                                }
                            });
                            Constants.rivewDialog.show();
                            dialog.dismiss();
                         }else{

                             Toast.makeText(activity, "Please enter your comment", Toast.LENGTH_SHORT).show();
                         }  
于 2013-04-03T07:39:36.367 回答