[在Android中尝试使用两个不同的按钮调用两个不同的活动时遇到问题。
即使两个按钮应调用不同的活动,它们也会导致指向相同的活动(Statistics.class)。
这是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimary"
android:layout_marginBottom="128dp"
app:layout_constraintBottom_toTopOf="@+id/startBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/tum_logo"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/tum_logo">
</androidx.appcompat.widget.AppCompatImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="das Quiz"
android:textColor="@android:color/white"
android:textSize="42dp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<Button
android:id="@+id/startBtn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:background="@drawable/btn_rounded_corners"
android:backgroundTint="@color/colorPrimary"
android:text="Quiz starten"
android:textColor="@android:color/white"
android:textStyle="bold"
android:onClick="onClick"
app:layout_constraintBottom_toTopOf="@+id/statisticsBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/statisticsBtn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="64dp"
android:text="Statistiken"
android:textStyle="bold"
android:background="@drawable/btn_rounded_corners"
android:backgroundTint="#8A8A8A"
android:textColor="@android:color/white"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是相应的Java代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button startBtn, statisticsBtn;
Intent intent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn = (Button) findViewById(R.id.startBtn);
statisticsBtn = (Button) findViewById(R.id.statisticsBtn);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
System.exit(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startBtn:
intent = new Intent(this, Semester.class);
startActivity(intent);
Toast.makeText(this, "startBtn", Toast.LENGTH_SHORT).show();
this.finish();
case R.id.statisticsBtn:
intent = new Intent(this, Statistics.class);
startActivity(intent);
Toast.makeText(this, "statisticsBtn", Toast.LENGTH_SHORT).show();
this.finish();
}
}
}
吐司表示,在单击startBtn之后也单击了statisticsBtn,但我不明白为什么。
您忘记在案例结束后添加break;
。
switch (v.getId()){
case R.id.startBtn:
intent = new Intent(this, Semester.class);
startActivity(intent);
Toast.makeText(this, "startBtn", Toast.LENGTH_SHORT).show();
this.finish();
break;
case R.id.statisticsBtn:
intent = new Intent(this, Statistics.class);
startActivity(intent);
Toast.makeText(this, "statisticsBtn", Toast.LENGTH_SHORT).show();
this.finish();
break;
}