как мне добавить кнопку сброса для моих двух текстовых представлений в Android Studio

#java #android #button #textview #android-studio-4.0

#java #Android #кнопка #textview #android-studio-4.0

Вопрос:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@ id/away"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="@string/away"
        android:gravity="center"
        android:textSize="20sp"/>

    <TextView
        android:id="@ id/score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="@string/_0"
        android:textSize="20sp" />

    <Button
        android:id="@ id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="50dp"
        android:onClick="score1"
        android:text="@string/score" />

    <TextView
        android:id="@ id/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:text="@string/home"
        android:textSize="20sp"
        android:gravity="center"/>

    <TextView
        android:id="@ id/score2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="50dp"
        android:text="@string/_0"
        android:textSize="20sp" />

    <Button
        android:id="@ id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="score2"
        android:text="@string/score" />

    <Button
        android:id="@ id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:textColor="#ff1100"/>

</LinearLayout>
  

Я искал его на нескольких веб-сайтах, но я все еще не могу ответить. Я также попытался использовать__setText («») это

не работает так же хорошо, как метод if (). Я также пробовал метод intent, который, похоже, не

работа и многие другие коды, пожалуйста, помогите мне, потому что я застрял на этом этапе, я знаю, что это просто, но

Я просто не могу ее найти. Спасибо

Комментарии:

1. Я не уверен, что вы пытаетесь здесь сделать? Если вы хотите, чтобы кнопка выполняла действие при нажатии, вам нужно либо добавить XML-тег функции click для выполнения, либо добавить к нему OnClickListener в вашем классе .java

2. Вы должны добавить больше деталей и7 прояснить вашу проблему. Чего вы хотите достичь?

Ответ №1:

Следующие коды установят текстовое score представление пустым при нажатии кнопки one

Первый подход

Чтобы использовать android:onClick="score1" в вашем XML

Добавьте это в свою активность

 TextView mTvScore1 = (TextView) findViewById(R.id.score)

public void score1(View v) {
    mTvScore1.setText("");
}
  

Второй подход

 Button mBtn = (Button) findViewById(R.id.one)
TextView mTvScore1 = (TextView) findViewById(R.id.score)

mBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mTvScore1.setText("");
    }
});
  

Код действия

 import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView mTvScore1 = (TextView) findViewById(R.id.score);
        Button mBtn = (Button) findViewById(R.id.one);

        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                score1(v);
            }
        });
    }

    public void score1(View v) {
        mTvScore1.setText("");
    }
}