简体   繁体   English

如何使嵌套开关工作?

[英]How to make a nested switch work?

Im completely new to android development and i have some problems with a nested switch. 我对android开发来说是全新的,嵌套开关有一些问题。 For starts i wonder if that is possible. 首先,我想知道这是否可能。

Below is part of my code for a score + stats counter for a basketball game. 以下是我的篮球比赛得分+统计计数器代码的一部分。 So to explain you what i want to do - the user will press a button (player) and then the app disables all the buttons in the screen except of the stats buttons (such as +1point, +2points, +1assist etc) The problem is that i cant get the program to get me in the second switch. 因此,向您解释我想做什么-用户将按下一个按钮(播放器),然后该应用会禁用屏幕中除统计按钮(例如+1点,+ 2点,+ 1辅助等)以外的所有按钮是我无法获取让我进入第二个开关的程序。 I could use any help, Thank you 我可以帮忙,谢谢

@Override
public void onClick(View view){

    switch (view.getId()){
        case R.id.player1_team1:
            ((Button) findViewById(R.id.player2_team1)).setEnabled(false);
            ((Button) findViewById(R.id.player3_team1)).setEnabled(false);
            ((Button) findViewById(R.id.player4_team1)).setEnabled(false);
            ((Button) findViewById(R.id.player5_team1)).setEnabled(false);
            ((Button) findViewById(R.id.player1_team2)).setEnabled(false);
            ((Button) findViewById(R.id.player2_team2)).setEnabled(false);
            ((Button) findViewById(R.id.player3_team2)).setEnabled(false);
            ((Button) findViewById(R.id.player4_team2)).setEnabled(false);
            ((Button) findViewById(R.id.player5_team2)).setEnabled(false);

            switch (view.getId()){
              case R.id.plus1p:

              thisGame.setName(getStrings(player1_team1));
              thisGame.setOnePointer(1);
              scoreTeam1 += 1;
              scoreCount1(1);
              Toast.makeText(view.getContext(), "+1 point " + getStrings(player1_team1), Toast.LENGTH_SHORT).show();
            break;

You switch on the same expression in your both switches: view.getId() , but the case expression is different. 您在两个开关中都打开了相同的表达式: view.getId() ,但case表达式不同。

So, to get into the second switch 's case (which is embedded in the first one), view.getId() would have to be equal to R.id.player1_team1 and R.id.plus1p at the same time. 因此,要进入第二个switchcase (嵌入在第一个switch ), view.getId()必须同时等于R.id.player1_team1R.id.plus1p I guess these values are different, so it's not possible to get into the case of the second switch . 我猜这些值是不同的,因此不可能进入第二个switchcase

I see that you are using buttons here... and it looks like if you click on one, it disables others. 我看到您在这里使用按钮...看起来好像您单击其中一个按钮,则会禁用其他按钮。 Why not implement a RadioGroup with RadioButtons instead? 为什么不使用RadioButtons实现RadioGroup?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/playerGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/playerOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <RadioButton
            android:id="@+id/playerTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />

</LinearLayout>

If you add 10 RadioButtons, if you select one, then you can't select the other 9, etc. You can then implement clicks within the onclick for each button. 如果添加10个RadioButton,则选择一个,则不能选择其他9个,依此类推。然后可以在onclick中为每个按钮实现单击。 It will work 10x better than what you are trying to do. 它将比您尝试做的工作好10倍。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM