两个按钮导致相同的活动



我在Android中尝试用两个不同的按钮调用两个不同活动时遇到了一个问题。

即使这两个按钮都应该调用不同的Activity,它们也会将引向同一个(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();
}
}
}

toast表示,在单击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;
}

相关内容

  • 没有找到相关文章

最新更新