Android:检查编辑文本isempty并提交问题

时间:2013-04-03 06:42:05

标签: android android-edittext

在我的应用程序中,我使用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;
            }
            }); 

但是即使编辑文本是空的,也提交哪个不应该发生。我正在检查条件如下但是它不起作用

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 个答案:

答案 0 :(得分:0)

使用comment.trim().equals("")代替comment.equalsIgnoreCase("")

或检查注释字符串的长度,使其超出最小字符数。

答案 1 :(得分:0)

使用

if(comment != null && !comment.isEmpty())

答案 2 :(得分:0)

你可以用它。

if(comment != null && !comment.isEmpty() && comment.length()!=0){
    //your task
}else{
  //give some message
}

答案 3 :(得分: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();
                         }