代码之家  ›  专栏  ›  技术社区  ›  Pointyhat

如何使嵌套开关工作?

  •  0
  • Pointyhat  · 技术社区  · 7 年前

    我对android开发完全陌生,我对嵌套开关有一些问题。对于首发,我想知道这是否可行。

    下面是我为篮球比赛编写的分数+统计计数器代码的一部分。 因此,为了向您解释我想做什么-用户将按下一个按钮(播放器),然后应用程序禁用屏幕上除统计按钮以外的所有按钮(例如+1点、+2点、+1辅助等) 问题是我无法让程序让我进入第二个切换。 我需要任何帮助, 非常感谢。

    @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;
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Roman Puchkovskiy    7 年前

    在两个开关中切换相同的表达式: view.getId() ,但 case 表达方式不同。

    那么,进入第二阶段 switch 案例 (嵌入第一个中), 看法getId() 必须等于 R.id.player1_team1 R.id.plus1p 同时我想这些值是不同的,所以不可能进入 案例 第二个的 转换 .

        2
  •  0
  •   letsCode    7 年前

    我看到你在这里使用按钮。。。如果你点击其中一个,它会禁用其他的。为什么不使用单选按钮实现一个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>
    

    如果添加10个单选按钮,如果选择一个,则无法选择其他9个单选按钮,等等。然后可以在onclick中为每个按钮执行单击。它将比你正在尝试的工作效果好10倍。