gpt4 book ai didi

java - 如何在android java中通过按钮的ID使用OnClickListener属性

转载 作者:行者123 更新时间:2023-12-01 16:22:48 25 4
gpt4 key购买 nike

我试图使用 onClickListener 通过 id 为每个按钮实现相同的结果。这是原始代码的链接,其中 onClick 用于所有按钮,而没有创建 id 以供引用。我之所以打算为每个按钮创建 id ,与原始代码不同,是因为我觉得原始代码有点简化,但我想挑战自己,看看是否可以通过使用引用来实现相同的结果,尽管我知道它的复杂性。但我正在尝试以不同的方式做事,而不仅仅是盲目地复制粘贴代码:)

非常感谢任何帮助!

附:请仅考虑与按钮代码相关的代码,因为我在该部分中遇到了错误,并且我还没有超出原始代码。

[https://gist.github.com/udacityandroid/4edd7beac8465acc07ca][1]

附件是我使用按钮 id 的代码。`

 **activity_main.xml**

<?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/Team_Name_A"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Team A"
android:gravity="center_horizontal"
/>
<TextView
android:id="@+id/Scored_Points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:padding="20dp"
android:gravity="center_horizontal"
/>

<Button
android:id="@+id/Beyond_Three_Point_Line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="+3 Points"
android:onClick="addThreeForTeamA"/>
<Button
android:id="@+id/Within_Three_Point_Line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="+2 Points"
android:onClick="addTwoForTeamA"/>
<Button
android:id="@+id/Foul_Free_Throw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Free Throw!"
android:onClick="addOneForTeamA"/>
</Linear

**MainActivity.java**

package com.example.scorebasket;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

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

public void addThreeForTeamA (View view){
Button button1 = (Button) findViewById(R.id.Beyond_Three_Point_Line);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

}
});

public void addTwoForTeamA (View view){
Button button2 = (Button) findViewById(R.id.Within_Three_Point_Line);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
public void addOneForTeamA (View view){
Button button3 = (Button) findViewById(R.id.Foul_Free_Throw);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});

}

/**
* Displays the given score for Team A
*
*/

public void scoreEarnedByTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.Scored_Points);
scoreView.setText(String.valueOf(score));
}

}

最佳答案

package com.example.scorebasket;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override

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

}

public void addThreeForTeamA (View view){
Button button1 = (Button) findViewById(R.id.Beyond_Three_Point_Line);
button1.setOnClickListener(this);
}

public void addTwoForTeamA (View view){
Button button2 = (Button) findViewById(R.id.Within_Three_Point_Line);
button2.setOnClickListener(this);
}

public void addOneForTeamA (View view){
Button button3 = (Button) findViewById(R.id.Foul_Free_Throw);
button3.setOnClickListener(this);

}

@Override
public void onClick(View view){
if(view.getId() == R.id.Beyond_Three_Point_Line){
// handle on click of R.id.Beyond_Three_Point_Line
}else if(view.getId() == R.id.Within_Three_Point_Line){
// handle on click of R.id.Within_Three_Point_Line
}else if(view.getId() == R.id.Foul_Free_Throw){

}
}

/**
* Displays the given score for Team A
*
*/

public void scoreEarnedByTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.Scored_Points);
scoreView.setText(String.valueOf(score));
}

}

你是这个意思吗?喜欢根据 id 在一种方法中处理 onClick 吗?

关于java - 如何在android java中通过按钮的ID使用OnClickListener属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62227559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com