gpt4 book ai didi

java - 按钮 OnClick 开关不起作用

转载 作者:行者123 更新时间:2023-11-30 01:13:19 30 4
gpt4 key购买 nike

我的应用程序中的一个 Activity 中有三个按钮,我使用一个开关来对它们进行编码。我在我的应用程序中多次使用了几乎相同的代码,但这个特定的代码不起作用。当我导航到此屏幕并单击三个按钮中的任何一个时,没有任何反应。

这是不起作用的代码:

public void buttonOnClick(View view){
switch(view.getId()){
case R.id.generalPrefabButton:
Intent generalPrefabScreen = new Intent();
generalPrefabScreen.setClass(this, General_Prefab_Order.class);
startActivity(generalPrefabScreen);
break;
case R.id.conduitBendButton:
Intent conduitBendScreen = new Intent();
conduitBendScreen.setClass(this, Conduit_Bend_Order.class);
startActivity(conduitBendScreen);
break;
case R.id.safetyReportButton:
Intent safetyReportScreen = new Intent();
safetyReportScreen.setClass(this, Safety_Report.class);
startActivity(safetyReportScreen);
}
}

最佳答案

实现此目的的一种方法是让您的类实现 OnClickListener,然后像这样将其添加到您的按钮中:

例子:

//make your class implement OnClickListener     
public class MyClass implements OnClickListener{ ... //Create your buttons and set their onClickListener to "this"

Button generalPrefabButton = (Button) findViewById(R.id.buttonplay);
generalPrefabButton.setOnClickListener(this);

Button conduitBendButton = (Button) findViewById(R.id.buttonstop);
conduitBendButton.setOnClickListener(this); ...

//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {

case R.id.generalPrefabButton:
Intent generalPrefabScreen = new Intent();
generalPrefabScreen.setClass(this, General_Prefab_Order.class);
startActivity(generalPrefabScreen);
break;

case R.id.conduitBendButton:
Intent conduitBendScreen = new Intent();
conduitBendScreen.setClass(this, Conduit_Bend_Order.class);
startActivity(conduitBendScreen);
break;

case R.id.safetyReportButton:
Intent safetyReportScreen = new Intent();
safetyReportScreen.setClass(this, Safety_Report.class);
startActivity(safetyReportScreen);
break;
}

}

关于java - 按钮 OnClick 开关不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38212504/

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