我有6个按钮,当我点击任何按钮时我试图改变他们的位置。 第一个shuffle(活动开始时)由以下各项完成: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>
答案 0 :(得分:1)
考虑一下:不要改变按钮,而是改变它们上面的数字,这样你就不必重新布局应用程序并移动按钮。
如果您设置了随机播放按钮,最简单的方法是删除它们并按新顺序添加它们。
要在单击按钮时获取事件,请注册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();
}
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
在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
}
这就是全部。它应该完全符合您的要求。
答案 3 :(得分:0)
尝试这样的事情
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。