Android 随机播放按钮位置 onClick

Android shuffle button position onClick

我有 6 个按钮,我试图在单击任何按钮时调整它们的位置。 第一次随机播放(当 activity 开始时)由以下方式完成:Collections.shuffle(buttonList);

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

Java:

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>

就在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

}

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

考虑一下:与其打乱按钮,不如更改按钮上的数字,这样您就不必重新布局您的应用程序并四处移动按钮。

如果您打算随机排列按钮,最简单的方法是将它们移除并按新顺序重新添加。

要在您点击按钮时获取事件,您需要注册一个 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();
        }

尝试这样的事情

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。

首先将按钮的初始设置移至 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();
  }
}