How to change a TextView's text by pressing a

2019-01-12 03:33发布

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!" />

And this is my activity:

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
                // ???
            }
        });
    }
}

What should I put inside onClick() method?

7条回答
Evening l夕情丶
2楼-- · 2019-01-12 03:50
  1. Find the textview by id
  2. Change the text by invoking yourTextView.setText("New text");

Refer findViewById and setText methods.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-12 03:50

You can do it with the setText("anything") method.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-12 03:55
TextView tv = (TextView) v;
tv.setText("My new text");

Edit: Here is your handler:

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);

查看更多
狗以群分
5楼-- · 2019-01-12 03:56

According to: http://developer.android.com/reference/android/widget/TextView.html

TextView view = (TextView) findViewById(R.id.counter);
view.setText("Do whatever");
查看更多
Viruses.
6楼-- · 2019-01-12 03:57

Java only no XML version

To make things more explicit:

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);
    }
}

Tested on Android 22.

查看更多
\"骚年 ilove
7楼-- · 2019-01-12 04:04

In onclick, take the object of the TextView and set the desired text like so:

tvOBJECT.setText("your text");
查看更多
登录 后发表回答