Подтвердить что ты не робот

Как изменить текст TextView, нажав кнопку

Я хочу изменить a TextView, нажав Button, но не понимаю, как это сделать правильно.

Это часть моего макета:

<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()?

4b9b3361

Ответ 1

  • Найти текстовое представление по id
  • Измените текст, вызвав yourTextView.setText("New text");

Обратитесь findViewById и setText методы.

Ответ 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);
            }
        });
    }
}

Надеюсь, это послужит вашей потребности.

Ответ 4

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

Edit: Вот ваш обработчик:

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

Вы можете сделать это с помощью метода setText ( "ничего" ).

Ответ 6

В onclick возьмите объект TextView и установите требуемый текст следующим образом:

tvOBJECT.setText("your text");

Ответ 7

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.