如何通过按下按钮更改 TextView 的文本 [英] How to change a TextView's text by pressing a Button

查看:17
本文介绍了如何通过按下按钮更改 TextView 的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过按下 Button 来更改 TextView 的属性,但不明白如何正确操作.

I want to change the of a TextView by pressing a Button, but don't understand how to do it properly.

这是我的布局的一部分:

This is part of my layout:

<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() 方法里面放什么?

What should I put inside onClick() method?

推荐答案

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

参考 findViewByIdsetText 方法.

Refer findViewById and setText methods.

这篇关于如何通过按下按钮更改 TextView 的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆