Button 活动上的多个按钮
我对Java还不熟悉,正在试图弄清楚如何使用它 在活动上有四个按钮,用于向其他活动添加要启动的意图。当我运行代码时,它会在仿真器到达此页面时停止应用程序 任何帮助都将不胜感激。这是我的密码:Button 活动上的多个按钮,button,android-intent,Button,Android Intent,我对Java还不熟悉,正在试图弄清楚如何使用它 在活动上有四个按钮,用于向其他活动添加要启动的意图。当我运行代码时,它会在仿真器到达此页面时停止应用程序 任何帮助都将不胜感激。这是我的密码: package com.helpfinder.app; import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.view.View; import androi
package com.helpfinder.app;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class LakeOptions extends Activity implements View.OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lake);
Button imageButton5= (Button) findViewById(R.id.imageButton5);
Button imageButton6= (Button) findViewById(R.id.imageButton6);
Button imageButton7= (Button) findViewById(R.id.imageButton7);
Button imageButton8= (Button) findViewById(R.id.imageButton8);
imageButton5.setOnClickListener(new View.OnClickListener() {
public void onClick(View imageButton5) {
Intent goToLakeFood = new Intent(LakeOptions.this,LakeFood.class);
startActivity(goToLakeFood);
}
});
imageButton6.setOnClickListener(new View.OnClickListener() {
public void onClick(View imageButton6) {
Intent goToLakeHousing = new Intent(LakeOptions.this,LakeHousing.class);
startActivity(goToLakeHousing);
}
});
imageButton7.setOnClickListener(new View.OnClickListener() {
public void onClick(View imageButton7) {
Intent goToLakeAssistance = new Intent(LakeOptions.this,LakeAssistance.class);
startActivity(goToLakeAssistance);
}
});
imageButton8.setOnClickListener(new View.OnClickListener() {
public void onClick(View imageButton8) {
Intent goToLakeOtherServices = new Intent(LakeOptions.this, LakeOtherServices.class);
startActivity(goToLakeOtherServices);
}
});
}public void onClick(View v) {
}
}
在xml中的按钮上,我有
android:onClick="onClick"
在这里尝试了这个建议之后,我的代码现在看起来是这样的:但是这个应用甚至不会运行第一个页面。我按照您的建议取出了最后一段代码,但在android studio中实现我的onClick侦听器时,它会将代码添加到后面
public class LakeOptions extends Activity implements View.OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lake);
Button imageButton5= (Button) findViewById(R.id.imageButton5);
Button imageButton6= (Button) findViewById(R.id.imageButton6);
Button imageButton7= (Button) findViewById(R.id.imageButton7);
Button imageButton8= (Button) findViewById(R.id.imageButton8);
imageButton5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent goToLakeFood = new Intent(LakeOptions.this,LakeFood.class);
startActivity(goToLakeFood);
}
});
imageButton6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent goToLakeHousing = new Intent(LakeOptions.this,LakeHousing.class);
startActivity(goToLakeHousing);
}
});
imageButton7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent goToLakeAssistance = new Intent(LakeOptions.this,LakeAssistance.class);
startActivity(goToLakeAssistance);
}
});
imageButton8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent goToLakeOtherServices = new Intent(LakeOptions.this, LakeOtherServices.class);
startActivity(goToLakeOtherServices);
}
});
}
@Override
public void onClick(View view) {
}
}在XML文件中,要删除:
android:onClick="onClick"
因为您正在java代码中设置按钮处理程序
此外,您还希望删除最后一个onClick函数,因为您没有使用它。(滚动到代码的底部)
最后,替换onClick处理程序签名:
public void onClick(View imageButton[5-8])
与
我按照你的建议尝试了RichS,但该应用程序甚至不会运行第一个活动。当我对函数进行注释时,它是有效的。我将编辑上面的新代码。谢谢。android studio会让你保留这个功能吗?我更熟悉Eclipse(我没有使用android studio),当我对它进行注释时,onClickListener会变成红色,并且没有实现。它说了一些关于使其成为一个抽象类的内容,我也尝试过,但结果相同。如果LakeOptions是您希望启动的活动,您需要在活动标签中指定它(设置android:name=com.helpfinder.app.LakeOptions)。我已经这样做了。我为所有活动设置了活动标签。我也需要意向过滤器吗?
public void onClick(View v)