简体   繁体   English

三个按钮,每个按钮以不同的Int启动相同的Activity。 但是,当选择任何按钮时,它将加载所有3个活动

[英]Three buttons, each start the same Activity with a different Int. But when selecting any button, it loads all 3 Activities

I'm programming an assignment, and I've got the core functionality operating but I'm quite confused with this. 我正在编写作业,并且已经运行了核心功能,但是对此我感到很困惑。 I have 3 buttons on this Activity/Fragment, and each one sends an Intent to a new Activity with a different integer to set the speed of the main game. 我在此“活动/片段”上有3个按钮,每个按钮都向一个新的“活动”发送一个“意图”,并使用不同的整数来设置主游戏的速度。

Button 1 sets the speed 10, button 2 sets the speed 30, and button 3 sets the speed 50. However, whichever one I click, it starts the Activity with speed 50 first, then when that Activity is finished, it is started with speed 30, and then again after with speed 10. It is essentially cycling and starting all possible Activities, but I only want the selected one starting. 按钮1将速度设置为10,按钮2将速度设置为30,按钮3将速度设置为50。但是,无论我单击哪个按钮,它都首先以速度50启动活动,然后在该活动结束时以速度启动30,然后再以速度10再启动。它实际上是在循环并开始所有可能的活动,但是我只希望所选的一项开始。

应用

This is the code, as far as I can understand it's somewhere in here. 就我所知,这就是代码。

public class LevelsFragment extends Fragment implements OnClickListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{    
    View rootView = inflater.inflate(R.layout.fragment_levels, container, false);

    Button b1 = (Button)rootView.findViewById(R.id.button1);
    Button b2 = (Button)rootView.findViewById(R.id.button2);
    Button b3 = (Button)rootView.findViewById(R.id.button3);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);

    return rootView;
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.button1:
            Intent i1 = new Intent(getActivity(), GameActivity.class);
            i1.putExtra("s", 10);
            startActivity(i1);
        case R.id.button2:
            Intent i2 = new Intent(getActivity(), GameActivity.class);
            i2.putExtra("s", 30);
            startActivity(i2);
        case R.id.button3:
            Intent i3 = new Intent(getActivity(), GameActivity.class);
            i3.putExtra("s", 50);
            startActivity(i3);
    }       
}
}

You forgot to put a break in your switch statements. 您忘记在switch语句中放个break

switch (v.getId())
    {
        case R.id.button1:
            Intent i1 = new Intent(getActivity(), GameActivity.class);
            i1.putExtra("s", 10);
            startActivity(i1);
            break;
        case R.id.button2:
            Intent i2 = new Intent(getActivity(), GameActivity.class);
            i2.putExtra("s", 30);
            startActivity(i2);
            break;
        case R.id.button3:
            Intent i3 = new Intent(getActivity(), GameActivity.class);
            i3.putExtra("s", 50);
            startActivity(i3);
            break;
    }       

This one's a classic, you forgot to put a break; 这是经典,你忘了break; between the different cases. 在不同情况之间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个按钮,每个按钮选择一个不同的活动 - Multiple buttons w/ each selecting a different Activity 具有三个按钮的Java程序,每个按钮显示一个不同的文本,按下该按钮将在文本框中显示按钮上的文本 - java program that has three buttons, each displaying a different text that when pressed will display the text on the button in a text box 在不同的活动之间开始活动的结果不起作用 - Start activity for result not work, between different activities 通过关闭所有堆叠的活动来开始新活动 - start a new activity by closing all the stacked activities 单击来自两个不同活动的两个单选按钮时,如何在新活动中显示文本? - How to display text in a new activity when two radio buttons from two different activities are clicked? 一个活动2个按钮,可以进行不同的活动 - One activity 2 buttons which can go different activities 有什么方法可以让 Activity 在单击时对每个按钮做出不同的反应? - Is there any way to make an Activity react differently for each button when clicked? Android:不同按钮的活动相同,但动作不同 - Android: Same activity for different buttons, but different actions 为具有不同资源的不同按钮启动相同的活动 - Launch the same activity for different buttons with different resource 从Android中的两个不同活动打开时的不同活动行为 - Different Behaviors of Activity when Opened from two Different Activities in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM