Java 如何在Android Studio中为我的两个文本视图添加重置按钮
我在多个网站上搜索过,但仍然找不到答案。我还试着使用_uusettext(“”)来完成它 没有if()方法那么有效。我也尝试过意图方法,但似乎也没有效果 工作,和许多其他代码,请帮助我,因为我被困在这一点,我知道这是简单的,但Java 如何在Android Studio中为我的两个文本视图添加重置按钮,java,android,button,textview,android-studio-4.0,Java,Android,Button,Textview,Android Studio 4.0,我在多个网站上搜索过,但仍然找不到答案。我还试着使用_uusettext(“”)来完成它 没有if()方法那么有效。我也尝试过意图方法,但似乎也没有效果 工作,和许多其他代码,请帮助我,因为我被困在这一点,我知道这是简单的,但 我就是找不到。谢谢单击按钮one时,以下代码将文本视图得分设置为空白 第一种方法 TextView mTvScore1 = (TextView) findViewById(R.id.score) public void score1(View v) { mTvS
我就是找不到。谢谢单击按钮
one
时,以下代码将文本视图得分设置为空白
第一种方法
TextView mTvScore1 = (TextView) findViewById(R.id.score)
public void score1(View v) {
mTvScore1.setText("");
}
在XML中使用android:onClick=“score1”
将此添加到您的活动中
<?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>
第二种方法
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("");
}
});
我不知道你想在这里做什么?如果希望按钮在单击时执行操作,则需要在.java类中添加要执行的click函数的xml标记或添加onClickListener,您必须添加更多详细信息并澄清您的问题。你想要实现什么?