[英]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.