有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java指向同一活动的两个按钮

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

尽管两个按钮都应该调用不同的活动,但它们将引导到同一个活动(Statistics.class)

以下是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        安卓:layout_width="0dp"
        安卓:layout_height="0dp"
        安卓:background="@color/colorPrimary"
        安卓: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
            安卓:layout_width="match_parent"
            安卓:layout_height="match_parent"
            安卓:orientation="vertical"
            安卓:gravity="center">

            <安卓x.appcompat.widget.AppCompatImageView
                安卓:id="@+id/tum_logo"
                安卓:layout_width="match_parent"
                安卓:layout_height="100dp"
                安卓:src="@drawable/tum_logo">
            </安卓x.appcompat.widget.AppCompatImageView>

            <TextView
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:layout_gravity="center"
                安卓:layout_marginTop="10dp"
                安卓:text="das Quiz"
                安卓:textColor="@安卓:color/white"
                安卓:textSize="42dp"
                安卓:textStyle="bold" />

        </LinearLayout>
    </FrameLayout>

    <Button
        安卓:id="@+id/startBtn"
        安卓:layout_width="150dp"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="32dp"
        安卓:background="@drawable/btn_rounded_corners"
        安卓:backgroundTint="@color/colorPrimary"
        安卓:text="Quiz starten"
        安卓:textColor="@安卓:color/white"
        安卓:textStyle="bold"
        安卓:onClick="onClick"
        app:layout_constraintBottom_toTopOf="@+id/statisticsBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        安卓:id="@+id/statisticsBtn"
        安卓:layout_width="150dp"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="64dp"
        安卓:text="Statistiken"
        安卓:textStyle="bold"
        安卓:background="@drawable/btn_rounded_corners"
        安卓:backgroundTint="#8A8A8A"
        安卓:textColor="@安卓:color/white"
        安卓:onClick="onClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</安卓x.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();
        }
    }
}

祝酒词表明,在点击开始按钮之后,也会点击统计按钮,但我不明白为什么


共 (1) 个答案

  1. # 1 楼答案

    您忘记在案例结束后添加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;
            }