2

我目前正在编写一个小 Android 应用程序,并发现了一个关于多个按钮的奇怪问题。我有一个带有 4 个按钮的 Activity。当我一次按下多个按钮时,两个动作都会被执行。所以我可以按下所有 4 个按钮并启动所有 4 个以下活动。

这是我的 onButtonClick 方法

public void onButtonClick(View view) {
    Intent intent = new Intent(this, RunActivityConfirm.class);

    switch(view.getId()) {
        case R.id.btnRunAcceleration:
            intent.putExtra("DisciplineName", "Acceleration");
            startActivity(intent);
            break;
        case R.id.btnRunSkidPad:
            intent.putExtra("DisciplineName", "Skid Pad");
            startActivity(intent);
            break;
        case R.id.btnRunAutocross:
            intent.putExtra("DisciplineName", "Autocross");
            startActivity(intent);
            break;
        case R.id.btnRunEndurance:
            intent.putExtra("DisciplineName", "Endurance");
            startActivity(intent);
            break;
    }

}

首先我认为问题在于我总是在每个“案例”中调用 startActivity() 但即使我尝试以下所有 4 个活动同时开始

public void onButtonClick(View view){   
    Intent intent = new Intent(this, RunActivityConfirm.class);

    switch(view.getId()) {
        case R.id.btnRunAcceleration:
            intent.putExtra("DisciplineName", "Acceleration");
            break;
        case R.id.btnRunSkidPad:
            intent.putExtra("DisciplineName", "Skid Pad");
            break;
        case R.id.btnRunAutocross:
            intent.putExtra("DisciplineName", "Autocross");
            break;
        case R.id.btnRunEndurance:
            intent.putExtra("DisciplineName", "Endurance");
            break;
    }
    startActivity(intent);
}

这发生在我的应用程序中。无论我按下哪个按钮,每个关联的 Action 都会执行并且每个关联的 Activity 都会启动。

我能做些什么吗?

4

2 回答 2

2

当我一次按下多个按钮时,两个动作都会被执行。

This is expected behavior. Your code starts an activity when a button is pressed. Multiple buttons are pressed, so multiple activities are started.

If you want to change this so that only one button can be pressed, then you need to add code to do so. You could disable the other buttons in onButtonClick (and re-enable them when appropriate).

于 2013-01-22T20:46:01.023 回答
0

As this is the same Activity add

android:finishOnTaskLaunch="true" 

in your manifest under your activity tag.It will prevent to make the multiple instances of same activity.

于 2013-01-22T20:46:40.140 回答