问题 1:我有 2 个活动。我想知道如何优化它。我可以使用多个侦听器创建 2 个活动。或者为每个按钮创建多个 java 文件(onclick 监听器) 问题 2:我尝试在一个 java 中创建多个监听器,但只能让一个按钮工作。一个java文件中多个监听器的语法是什么?这是我的*更新代码:现在的问题是无论单击什么按钮都会导致相同的页面。我认为问题在这些括号 [-{}-] activity1.java 中的文本中指出
package install.fineline;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Activity1 extends Activity {
Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;
[-{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fineline);
addListenerOnButton();
}
}-]
public void addListenerOnButton() {
final Context context = this;
Button1 = (Button) findViewById(R.id.autobody);
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
Button2 = (Button) findViewById(R.id.glass);
Button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
Button3 = (Button) findViewById(R.id.wheels);
Button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
Button4 = (Button) findViewById(R.id.speedy);
Button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
Button5 = (Button) findViewById(R.id.sevan);
Button5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
Button6 = (Button) findViewById(R.id.towing);
Button6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
}}
activity2.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button button1;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autobody);
}
Button button2;
public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
Button button3;
public void onCreate3(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wheels);
}
Button button4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedy);
}
Button button5;
public void onCreate5(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sevan);
}
Button button6;
public void onCreate6(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.towing);
}}