将EditText与TextView进行比较

时间:2014-05-28 07:39:40

标签: android

我有TextView随机值,我有EditText输入值和按钮,我wand禁用按钮,直到输入正确的值 到我的EditText ...

这是我的代码到目前为止,但它不起作用..请你帮忙。

 @Override
protected Dialog onCreateDialog(int id) {

    // TODO Auto-generated method stub

    Dialog dialog = null;

    switch (id) {

    case CUSTOM_DIALOG_ID:

        dialog = new Dialog(RegisterActivity.this);

        dialog.setContentView(R.layout.customlayout);

        dialog.setTitle("Update");

        customDialog_TextView1 = (TextView) dialog
                .findViewById(R.id.dialogtextview1);
        final Random myRandom = new Random();
        customDialog_TextView1
                .setText(String.valueOf(myRandom.nextInt(100)));

        customDialog_EditText = (EditText) dialog
                .findViewById(R.id.dialogedittext);
        customDialog_EditText.setGravity(Gravity.CENTER);

        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(2);
        customDialog_EditText.setFilters(FilterArray);

        final String srt = customDialog_EditText.getText().toString();
        final String srt1 = customDialog_TextView1.getText().toString();

        customDialog_EditText.addTextChangedListener(new TextWatcher() {
              @Override
              public void afterTextChanged(Editable arg0) {
                 enableSubmitIfReady();
              }

              private void enableSubmitIfReady() {
                // TODO Auto-generated method stub

                  if (srt.equals(srt1)) {
                        customDialog_Update.setEnabled(true);
                   } else {
                       customDialog_Update.setEnabled(false);
                    }

            }

            @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
              }
            });


        customDialog_Update = (Button) dialog
                .findViewById(R.id.dialogupdate);


        customDialog_Dismiss = (Button) dialog
                .findViewById(R.id.dialogdismiss);

        customDialog_Update
                .setOnClickListener(customDialog_UpdateOnClickListener);

        customDialog_Dismiss
                .setOnClickListener(customDialog_DismissOnClickListener);

        break;

    }

    return dialog;

}

感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

我认为问题是将字符串声明为final。试试这个:

        customDialog_Update = (Button) dialog
                .findViewById(R.id.dialogupdate);


        customDialog_Dismiss = (Button) dialog
                .findViewById(R.id.dialogdismiss);

        customDialog_Update
                .setOnClickListener(customDialog_UpdateOnClickListener);

        customDialog_Dismiss
                .setOnClickListener(customDialog_DismissOnClickListener);

        customDialog_EditText.addTextChangedListener(new TextWatcher() {
                  @Override
                  public void afterTextChanged(Editable arg0) {
                     enableSubmitIfReady();
                  }

                  private void enableSubmitIfReady() {
                    // TODO Auto-generated method stub

                      if (customDialog_EditText.getText().toString().equals(customDialog_TextView1.getText().toString())) {
                            customDialog_Update.setEnabled(true);
                       } else {
                           customDialog_Update.setEnabled(false);
                        }

                }

                @Override
                  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                  }

                  @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) {
                  }
          });