How to change a TextView's text by pressing a

2019-01-12 04:00发布

问题:

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?

回答1:

  1. Find the textview by id
  2. Change the text by invoking yourTextView.setText("New text");

Refer findViewById and setText methods.



回答2:

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

TextView view = (TextView) findViewById(R.id.counter);
view.setText("Do whatever");


回答3:

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

Hope this serve your need



回答4:

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:

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



回答6:

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

tvOBJECT.setText("your text");


回答7:

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.