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

标签 java android onclick onclicklistener

我试图使用 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/

相关文章:

javascript - 移动设备上的 OnClick 事件处理程序?

javascript - 如何动态创建一个 onClick 到 JavaScript 中的输入按钮?

javascript - 如何通过多个 EJS 数据字段的 onClick 填充多个表单字段

java - 如何使用 Jackson 将 JSONArray-to-List<MyObject> 中不同类型的对象映射?

java - SDK8 和 Tomcat 8 上的 sc.getRealPath(.) 和 File.seperator

android - 通过添加像素在android中合并两个图像一个在另一个下面

java - Android POST 到服务器总是返回错误的响应代码

java - 如何优化基于正则表达式的大日志文件解析?

java - Android Samck 消息超时

java - 我无法从 Activity 中填充 fragment 中的回收 View