2

尝试做一个计算器,这里是带有 * 的行运行时的按钮方法,它出现错误“operator + cannot be applied to java.lang.charsequence”。如果有人知道解决此问题的方法,请提供帮助。

    public void Button_Click(View view) {
    TextView result = (TextView) findViewById(R.id.result);
    Button b = (Button)view;
    if ((result.getText() == "0")||(operation_pressed))
        result.setText("");
    operation_pressed = false;

    if (b.getText() == ".") {
        result.getText() = result.getText() + b.getText()); ***
    }
}    

谢谢

4

1 回答 1

0

为了将字符序列转换为字符串,请将 .toString() 应用于 CharSequence 对象,您应该可以使用 + 运算符:

[ https://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html][1]

此外,您似乎有语法错误:

   result.getText() = result.getText() + b.getText()); *** the last ) is too much

此外,您不能写入 getText() .. Getter(由 getXXX() 表示)应该用于获取值,而 Setter(由 setXXX() 表示)应该用于将值存储到对象中。

result.setText(result.getText().toString() + b.getText().toString())

于 2014-11-11T08:16:26.377 回答