如何通过按下按钮更改TextView的文本

11
我想通过按下一个按钮来改变TextView的文本,但不知道如何正确实现。这是我的布局的一部分:
<TextView android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text" />
<Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change text!" />

这是我的活动:

public class Click extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                // ???
            }
        });
    }
}

我应该在onClick()方法里放什么内容?

7个回答

15

12
  1. 通过id查找textview
  2. 通过调用yourTextView.setText("新文本");更改文本

请参阅findViewByIdsetText方法。


一个重要的事情:只有当你从onClick块中退出时,该字段才会更新,你不能两次更新同一字段。 - onizukaek

9
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Click extends Activity {
int i=0;
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        final TextView mTextView = (TextView) findViewById(R.id.counter) 
        mTextView.setText("hello "+i);

        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
              i=i+1;  
              mTextView.setText("hello "+i);
            }
        });
    }
}

希望这能满足您的需求。

我尝试了这个,并修改了像这样的内容 mTextView.setText(Integer.toString(i));,它起作用了。谢谢。+1 - InnocentKiller

3
TextView tv = (TextView) v;
tv.setText("My new text");

编辑:以下是您的处理程序:

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //TextView tv = (TextView) v; //code corrected
            TextView tv= (TextView) findViewById(R.id.counter);
            tv.setText("My new text");
        }
});

TextView view = (TextView) findViewById(R.id.counter);

可以翻译为:

TextView视图 =(TextView)findViewById(R.id.counter);


我希望你知道这是完全错误的。onClick() 方法中的 v 参数是被点击的视图。现在,如果他按下一个带有你的代码的 Button,那么发生的只是你的 Button 的文本更改。 - Octavian Helm
哦!按钮和TextView是不同的。是的,这是我的错误。我将删除我的答案。 - Vikas

0

仅限Java版本,不包括XML版本

为了更明确地表达:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity {
    private int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.i = 0;

        final TextView tv = new TextView(this);
        tv.setText(String.format("%d", this.i));

        final Button button = new Button(this);
        button.setText("click me");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Main.this.i++;
                tv.setText(String.format("%d", Main.this.i));
            }
        });

        final LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.addView(button);
        linearLayout.addView(tv);
        this.setContentView(linearLayout);
    }
}

已在Android 22上进行测试。


0

你可以使用setText("anything")方法来实现。


0
onclick 中,获取 TextView 对象并设置所需的文本,如下所示:
tvOBJECT.setText("your text");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接