如何为我的两个TextView在Android Studio中添加一个重置按钮。

huangapple go评论77阅读模式
英文:

how do I add a reset button for my two TextViews in Android Studio

问题

<?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>
英文:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/away&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;100dp&quot;
android:text=&quot;@string/away&quot;
android:gravity=&quot;center&quot;
android:textSize=&quot;20sp&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/score&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;50dp&quot;
android:layout_marginTop=&quot;50dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;@string/_0&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/one&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginBottom=&quot;50dp&quot;
android:onClick=&quot;score1&quot;
android:text=&quot;@string/score&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/home&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;50dp&quot;
android:layout_marginTop=&quot;50dp&quot;
android:text=&quot;@string/home&quot;
android:textSize=&quot;20sp&quot;
android:gravity=&quot;center&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/score2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center&quot;
android:layout_marginBottom=&quot;50dp&quot;
android:text=&quot;@string/_0&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/two&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:onClick=&quot;score2&quot;
android:text=&quot;@string/score&quot; /&gt;
&lt;Button
android:id=&quot;@+id/reset&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/reset&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;50dp&quot;
android:textColor=&quot;#ff1100&quot;/&gt;
&lt;/LinearLayout&gt;

I searched it on multiple websites but I still can't the answer. I also tried using the__setText("")it

didn't work as well as the the if() method. I have also tried the intent method also doesn't seem to

work, and many other codes , please help me because I am stuck at this bit , I know that it is simple but

I just can't find it. Thank you

答案1

得分: 0

以下代码将在单击按钮one时将TextView score设置为空白。

第一种方法

在你的XML中使用android:onClick="score1"

将以下内容添加到你的活动中

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("");
    }
}
英文:

The following codes will set the TextView score to blank when Button one is clicked

First approach

To use the android:onClick=&quot;score1&quot; in your XML

Add this to your activity

TextView mTvScore1 = (TextView) findViewById(R.id.score)
public void score1(View v) {
mTvScore1.setText(&quot;&quot;);
}

Second approach

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(&quot;&quot;);
}
});

Activity code

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(&quot;&quot;);
}
}

huangapple
  • 本文由 发表于 2020年10月19日 21:55:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64428847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定