【问题标题】:TextView error issuesTextView 错误问题
【发布时间】:2013-06-14 00:57:01
【问题描述】:

我的问题是:我无法让按钮更改 TextView 中的文本,并且 TextView 没有注册。错误是无法解析TextView。

   final Button button = (Button) findViewById(R.id.button1);
   button.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
      textView.setText("@string/htmll1");
   }

【问题讨论】:

    标签: java android textview onclicklistener android-button


    【解决方案1】:

    要检索 TextView,您应该使用 findViewById,就像在按钮上一样。要检索 string.xml 上的字符串,您应该使用:

    getString(R.string.htmll1)
    

    代码:

    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       textView.setText(getString(R.string.htmll1));
    } 
    

    【讨论】:

    • 在最后一行的开头仍然有 textView 错误,但感谢您的字符串帮助
    • 那么,您可以发布整个代码(.java 和 xml)吗?然后我们可以检查发生了什么......
    • @TylerLund,您需要使用您提出问题时使用的同一帐户添加新代码,否则它将被版主拒绝(我刚刚看到您的编辑被拒绝)。
    【解决方案2】:

    我不确定您是否已经这样做了,但是您需要创建该 textView 的实例。基本上,给它一个变量,就像你为按钮所做的那样。

    添加类似:

    TextView textview = (TextView) findViewById(R.id.yourtextviewname);

    【讨论】:

      【解决方案3】:

      试试这个:

      textView.setText(getResources().getString(R.string.htmll1));
      

      【讨论】:

        猜你喜欢
        • 2016-03-09
        • 1970-01-01
        • 2014-10-19
        • 2016-08-07
        • 1970-01-01
        • 1970-01-01
        • 2017-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多