【问题标题】:How to convert TextView to int?如何将 TextView 转换为 int?
【发布时间】:2014-07-01 07:14:03
【问题描述】:

我知道有很多这样的帖子,但我阅读了它们,但我的应用程序还没有工作。 我试图将 TextView 参数转换为 int。 我用这个:

int MyScore;
TextView score = (TextView) findViewById(R.id.highscore);
MyScore = Integer.parseInt(score.toString());

当我启动程序并丰富到这个地方时,这应该可以让我的程序崩溃,因为最后一行:MyScore = Integer.parseInt(score.toString());

我该如何解决这个问题?

【问题讨论】:

    标签: string textview int ivalueconverter parseint


    【解决方案1】:

    你的最后一行应该是

        MyScore = Integer.parseInt(score.getText().toString());
    

    score 对象上调用的toString() 方法描述了score 对象,它不返回输入到score 对象中的字符串。请参阅 Oracle Java 教程。通读其中的大部分内容是个好主意。

    【讨论】:

    • 使用 TextView.getText().toString() (就像@Floris 建议的那样)将返回 TextView 的文本。如果您需要,也适用于 EditText。
    【解决方案2】:

    由于您没有指定使用的语言,我无法完全回答,但我可以猜到问题所在。大多数输入字段都有一个 value 或 text 属性,尝试解析它。

    【讨论】:

      猜你喜欢
      • 2013-08-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2020-07-17
      • 2013-04-18
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      相关资源
      最近更新 更多