【问题标题】:Android shuffle button position onClickAndroid shuffle 按钮位置 onClick
【发布时间】:2015-08-13 16:51:07
【问题描述】:

我有 6 个按钮,我试图在单击任何按钮时改变它们的位置。 第一次 shuffle(在 Activity 启动时)由 Collections.shuffle(buttonList);

完成

单击任何按钮时如何随机播放?

爪哇:

public class Main extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        LinearLayout ll = (LinearLayout) findViewById(R.id.llShuffleBox);
        LinearLayout top_layout = (LinearLayout) findViewById(R.id.top_layout);
        LinearLayout middle_layout = (LinearLayout) findViewById(R.id.middle_layout);
        final ArrayList<Button> buttonList = new ArrayList<Button>();

        for(int i=0; i<6; i++) {
            final Button b = new Button(this);
            b.setText("" + (i + 1));
            b.setGravity(Gravity.CENTER_HORIZONTAL);
            b.setId(i + 1);
            buttonList.add(b);
        }

        //First shuffle
        Collections.shuffle(buttonList);

        for (int i = 0; i<6; i++) {
            if (i <3) {
                top_layout.addView(buttonList.get(i));
            } else {
                middle_layout.addView(buttonList.get(i));
            }
        }
    }

} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".Main">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/llShuffleBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_marginTop="50dp">

        <LinearLayout
            android:id="@+id/top_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_horizontal">
        </LinearLayout>

        <LinearLayout
            android:id="@+id/middle_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_horizontal">
        </LinearLayout>


    </LinearLayout>
</RelativeLayout>

【问题讨论】:

    标签: java android xml shuffle


    【解决方案1】:

    首先将按钮的初始设置移至 onCreate 方法。

    private LinearLayout top_layout;
    private LinearLayout middle_layout;
    private final ArrayList<Button> buttonList = new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    
      top_layout = (LinearLayout) findViewById(R.id.top_layout);
      middle_layout = (LinearLayout) findViewById(R.id.middle_layout);
    
      for(int i=0; i<6; i++) {
        final Button b = new Button(this);
        b.setText("" + (i + 1));
        b.setGravity(Gravity.CENTER_HORIZONTAL);
        b.setId(i + 1);
        b.setOnClickListener(clickListener);
        buttonList.add(b);
      }
    
      shuffleButtons();
    }
    

    然后把删除按钮视图的代码,打乱它们,然后重新添加一个方法。

    private void shuffleButtons() {
      top_layout.removeAllViews();
      middle_layout.removeAllViews();
      Collections.shuffle(buttonList);
    
      for (int i = 0; i<6; i++) {
        if (i <3) {
          top_layout.addView(buttonList.get(i))
        } else {
          middle_layout.addView(buttonList.get(i));
        }
      }
    }
    

    还为每个将调用 shuffle 方法的按钮附加一个点击侦听器。

    private View.OnClickListener clickListener = new View.OnClickListener() {
      public void onClick(View v) {
        shuffleButtons();
      }
    }
    

    【讨论】:

    • 我试过你的方法,但我得到:“变量 buttonList 可能没有被初始化”。我错过了什么吗?
    • 应该已经在声明中初始化了,我现在已经对我的答案进行了更改。
    • 如果我在声明中初始化,我会收到 NullPointerException:致命异常:主要 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.sample/com.example.sample.MainActivity }: java.lang.NullPointerException.
    • 进行了更多应该解决 NPE 的编辑。但是,您应该将其作为指南阅读,并能够弄清楚如何自己解决这些细节,而不仅仅是复制和粘贴代码。
    • 谢谢!我是编程的初学者,我仍在努力解决问题。
    【解决方案2】:

    请考虑:无需重新排列按钮,而是更改按钮上的数字,这样您就不必重新布局应用程序并四处移动按钮。

    如果您打算对按钮进行洗牌,最简单的方法是删除它们并以新顺序重新添加它们。

    要在单击按钮时获取事件,您需要注册一个 onClickListener,其代码可能如下所示:

    class YourClass implements OnClickListener
    
            final Button b = new Button(this);
            b.setText("" + (i + 1));
            b.setGravity(Gravity.CENTER_HORIZONTAL);
            b.setOnClickListener (this)
    
            public void onClick(View view) {
                shuffle();
            }
    

    【讨论】:

      【解决方案3】:

      试试这样的

      public class Main extends AppCompatActivity implements OnClickListener{
      
      private Button b1, b2, b3, b4, b5, b6;
      
      private ArrayList<Button> buttonList = new ArrayList<Button>();
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          for(int i=0; i<6; i++) {
              final Button b = new Button(this);
              b.setText("" + (i + 1));
              b.setGravity(Gravity.CENTER_HORIZONTAL);
              b.setId(i + 1);
              buttonList.add(b);
          }
      
          b1 = (Button) findViewById(...);
          b2 = (Button) findViewById(...);
          b3 = (Button) findViewById(...);
          b4 = (Button) findViewById(...);
          b5 = (Button) findViewById(...);
          b6 = (Button) findViewById(...);
      
          b1.setOnClickListener(this);
          b2.setOnClickListener(this);
          b3.setOnClickListener(this);
          b4.setOnClickListener(this);
          b5.setOnClickListener(this);
          b6.setOnClickListener(this);
      }
      
      @Override
      protected void onStart() {
          super.onStart();
          LinearLayout ll = (LinearLayout) findViewById(R.id.llShuffleBox);
          LinearLayout top_layout = (LinearLayout) findViewById(R.id.top_layout);
          LinearLayout middle_layout = (LinearLayout) findViewById(R.id.middle_layout);
      
          //First shuffle
          Collections.shuffle(buttonList);
      
          for (int i = 0; i<6; i++) {
              if (i <3) {
                  top_layout.addView(buttonList.get(i));
              } else {
                  middle_layout.addView(buttonList.get(i));
              }
          }
      }
      
      @Override
      public void onClick(View v) {
      
          Collections.shuffle(buttonList);
      }
      

      }

      将 ... 替换为它们各自的布局 ID。

      【讨论】:

      • 您忘记了:您已经有一个列表,您不需要再次跟踪按钮,您没有移动屏幕上按钮的位置。考虑更新你的答案:)
      • 这是一项勇敢的努力。将setOnClickListener() 放入创建每个按钮的 for 循环中可能会更容易,而不是使用 findViewById 来处理大量按钮。此外,当单击按钮时,您会随机播放列表,但按钮将保留在屏幕上的位置。
      【解决方案4】:

      就在 b.setId(i+1); 之后添加此代码 -

      b.setId(i + 1); //this line you already have add these after
      b.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Collections.shuffle(buttonList);
              for (int i=0; i<6; i++){
                   //removing the buttons from the view
                   ViewGroup layout = (ViewGroup) buttonList.get(i).getParent();
                   if (null != layout) //for safety only  as you are doing onClick
                          layout.removeView(buttonList.get(i));
              }
              for (int i = 0; i<6; i++) {
                  //adding the buttons again
                  if (i <3) {
                      top_layout.addView(buttonList.get(i));
                  } else {
                      middle_layout.addView(buttonList.get(i));
                  }
              }
          }
      });//end of onClickListener; this is all which you have to add;
      buttonList.add(b);//this line you already have
      

      }

      这就是全部。它现在应该完全符合您的要求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多