【问题标题】:Cannot resolve method setText() - Android Studio Development无法解析方法 setText() - Android Studio 开发
【发布时间】:2016-10-20 02:55:54
【问题描述】:

我对 java 编程完全是个菜鸟,并且像往常一样陷入了困境(我似乎这样得到了更好的结果)。

我正在使用 Android Studio 创建一个 Android 应用,但在我的 Java 代码中遇到了错误。

我已经搜索了一个答案,但答案都是针对实际代码的,所以我自己找不到任何答案。

我的问题出现在我的 MainActivity.java 中,我使用 TextWatcher 作为 NumberTextWatcher,它将任何输入转换并设置为十进制货币(因为用户将输入价格)来自 Stack Overflow 上的 this answer

问题是我得到一个错误:

无法解析方法 setText()

这里是错误所在的代码(我已经用 //

   public class NumberTextWatcher implements TextWatcher {

    private final DecimalFormat df;
    private final DecimalFormat dfnd;
    private final EditText et;
    private boolean hasFractionalPart;
    private int trailingZeroCount;

    NumberTextWatcher(EditText inputPricePerOz, String pattern) {
        df = new DecimalFormat(pattern);
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("#,###.00");
        this.et = inputPricePerOz;
        hasFractionalPart = false;
    }

    @Override
    public void afterTextChanged(Editable s) {
        et.removeTextChangedListener();

        if (s != null && !s.toString().isEmpty()) {
            try {
                int inilen, endlen;
                inilen = et.getText().length();
                String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "").replace("$","");
                Number n = df.parse(v);
                int cp = et.getSelectionStart();
                if (hasFractionalPart) {
                    StringBuilder trailingZeros = new StringBuilder();
                    while (trailingZeroCount-- > 0)
                        trailingZeros.append('0');
                    et.setText();  //<- Here it throws an error.
                } else {
                    et.setText();  //<- Here it throws an error.
                }
                et.setText();  //<- Here it throws an error.
                endlen = et.getText().length();
                int sel = (cp + (endlen - inilen));
                if (sel > 0 && sel < et.getText().length()) {
                    et.setSelection(sel);
                } else if (trailingZeroCount > -1) {
                    et.setSelection(et.getText().length() - 3);
                } else {
                    et.setSelection(et.getText().length());
                }
            } catch (NumberFormatException | ParseException e) {
                e.printStackTrace();
            }
        }

        et.addTextChangedListener(this);
    }

由于我对 Java 知之甚少,我不知道为什么会发生此错误,尽管我认为使用 setText() 方法设置文本会很简单。 任何帮助将不胜感激

【问题讨论】:

  • 那些方法带参数...阅读文档。方法可以解决。无法应用它的参数(无)。
  • @cricket_007 嘿,感谢您的快速评论,所以它要我给 setText() 方法添加一个参数,例如 setText("@string/hello_world") 吗?我不明白“不能应用它的论点。”。抱歉,我说我是 Java 新手,就像我昨天刚开始学习一样!
  • 好吧,setText(R.string.hello_world) 会给你带来这个价值(如果你想要的)。您写的内容将按字面意思写入 EditText。但是是的,您需要提供字符串文字或字符串资源
  • 链接的问题在这些方法中确实有参数。为什么要删除它们?
  • @cricket_007 看看我的回答...

标签: java android android-studio


【解决方案1】:

语法错误:应该像下面的任何一个

        et.setText("Text to set in editext");
OR
        et.setText(R.string.app_name);
OR
        et.setText("Text to set in editext", BufferType.EDITABLE);

参考 API 文档: https://developer.android.com/reference/android/widget/EditText.html#setText(java.lang.CharSequence, android.widget.TextView.BufferType)
& https://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

【讨论】:

    【解决方案2】:

    如果您查看 EditText 的文档,您会发现该方法采用字符串和缓冲区类型。因此,您需要添加要更改的文本和缓冲区类型。你可能想要 TextView.BufferType.EDITABLE。

    例如

    et.setText("Example text", TextView.BufferType.EDITABLE);
    

    在此处查看更多信息https://developer.android.com/reference/android/widget/EditText.html

    【讨论】:

    • 您缺少从TextView 继承的方法。缓冲区类型不是必须的
    【解决方案3】:

    所以我弄清楚了为什么会抛出错误,这是一个我无法解释的简单错误,但是当我复制并粘贴代码时,它以某种方式错过了复制某些部分,或者未能让 android studio/gradle 更改我的代码,这是固定代码:

            @Override
        public void afterTextChanged(Editable s) {
            et.removeTextChangedListener(this);  //<- also fixed error an error here
    
            if (s != null && !s.toString().isEmpty()) {
                try {
                    int inilen, endlen;
                    inilen = et.getText().length();
                    String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "").replace("$","");
                    Number n = df.parse(v);
                    int cp = et.getSelectionStart();
                    if (hasFractionalPart) {
                        StringBuilder trailingZeros = new StringBuilder();
                        while (trailingZeroCount-- > 0)
                            trailingZeros.append('0');
                        et.setText(df.format(n) + trailingZeros.toString());  //<- Fixed error
                    } else {
                        et.setText(dfnd.format(n));  //<- Fixed error
                    }
                    et.setText("$".concat(et.getText().toString()));  //<- Fixed error
                    endlen = et.getText().length();
                    int sel = (cp + (endlen - inilen));
                    if (sel > 0 && sel < et.getText().length()) {
                        et.setSelection(sel);
                    } else if (trailingZeroCount > -1) {
                        et.setSelection(et.getText().length() - 3);
                    } else {
                        et.setSelection(et.getText().length());
                    }
                } catch (NumberFormatException | ParseException e) {
                    e.printStackTrace();
                }
            }
    
            et.addTextChangedListener(this);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多