简体   繁体   中英

Android shuffle button position onClick

I have 6 buttons and I'm trying to shuffle their position when any button is clicked. The first shuffle (when the activity is started) is done by: Collections.shuffle(buttonList);

How do I shuffle when any of the buttons is clicked?

例 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>

Consider: Instead of shuffling the buttons change the numbers on them, that way you don't have to re-layout your app and move the buttons around.

If you are set on shuffling the buttons the easiest way is to remove them and add them back in the new order.

To get the event when you click on a button you register an onClickListener, the code for that could look like this:

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();
        }

First move the initial setup of the buttons to the onCreate method.

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();
}

Then put the code to remove button views, shuffle them, then re-add in a method.

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));
    }
  }
}

Also attach a click listener to each of the buttons that will call through to the shuffle method.

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

Just after b.setId(i+1); add this code -

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

}

This is all. It should do exactly what you want now.

Try something like this

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);
}

}

Replace the ... for their respective layout ids.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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