答案 0 :(得分:1)
这是一个基本的例子:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(this,MainActivity.class);
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(this,Main2Activity.class);
}
});
答案 1 :(得分:1)
How to start new activity on button click
例如,在onCreate()中写道:
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Activity1.class);
view.getContext().startActivity(intent);}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Activity2.class);
view.getContext().startActivity(intent);}
});
答案 2 :(得分:1)
你也可以通过xml实现这个目标
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="@dimen/height"
android:layout_margin="@dimen/fields_margin_bottom"
android:text="Activity1"
android:textColor="@color/defaultTextColor"
android:textAllCaps="true"
android:textSize="@dimen/size"
android:onClick="mainActivty"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="@dimen/height"
android:layout_margin="@dimen/fields_margin_bottom"
android:text="Activity2"
android:textColor="@color/defaultTextColor"
android:textAllCaps="true"
android:textSize="@dimen/size"
android:onClick="main2Activty"/>
然后在您的主页活动中实施这些方法
public void mainActivty(View v) {
startActivity(new Intent(this,MainActivity.class);
}
public void main2Activty(View v) {
startActivity(new Intent(this,Main2Activity.class);
}