【问题标题】:'equals()' between objects of inconvertible types 'int' and 'TextWatcher' error不可转换类型“int”和“TextWatcher”的对象之间的“equals()”错误
【发布时间】:2016-01-10 15:59:33
【问题描述】:

我有一个我无法解决的奇怪错误。错误是:不可转换类型“int”和“TextWatcher”的对象之间的“equals()”。这是我的代码:

if( textWatcher_ans.equals(R.string.editText_7)){
    Toast.makeText(getApplication().getBaseContext(),
                   (R.string.Good),Toast.LENGTH_SHORT).show();
}

            private final TextWatcher textWatcher_ans = new TextWatcher(){
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textView_name.setVisibility(View.VISIBLE);
            }
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    textView_name.setVisibility(View.GONE);
                } else {
                    textView_name.setText("You have entered : " + editText_surname.getText());
                }
            }

我很困,我不知道该怎么办。 有人可以帮帮我吗?

【问题讨论】:

  • 这里是@Erik-JanWestendorp
  • 你在这里的目的是什么?现在您正在将 TextWatcher 对象与一个整数(映射到字符串资源的整数)进行比较,我很确定这不是您想要的。
  • 我想要的是用户输入的内容等于我的字符串。 @asadmshah

标签: java android


【解决方案1】:

如果您的目标是在editText_surname 中的文本与R.string.editText_7 匹配时显示R.string.Good 消息,请尝试以下操作:

首先将TextWatcher附加到editText_surname

editText_surname.addTextChangedListener(textWatcher_ans);

在你的afterTextChangedMethod

public void afterTextChanged(Editable s) {
    if (s.length() == 0) {
        textView_name.setVisibility(View.GONE);
     } else {
        textView_name.setText("You have entered : " + s.toString());
     }

    if (s.toString().equals(context.getString(R.string.editText_7)) {
        Toast.makeText(getApplication().getBaseContext(),
               (R.string.Good),Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

  • 您好,感谢您的帮助!但是当我将这段代码输入到文件中时,我得到了这个错误:无法解析符号'上下文'。
  • 哦等等,我没有声明上下文。非常感谢!!!!对此,我真的非常感激!! @asadmshah
  • @JeroenBoot 没问题。如果此答案解决了您的问题,请考虑通过单击位于否决按钮下方的复选标记来接受它。
  • 完成!再次感谢! @asadmshah
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2021-03-02
  • 1970-01-01
  • 2022-12-06
  • 2019-06-23
相关资源
最近更新 更多