随机位置Android的随机播放按钮

时间:2013-07-29 05:46:01

标签: android

我的xml布局中有十个按钮,我想随机更换10个按钮的位置。我尝试了两种方法来实现它。

方法1:我更改了按钮的背景资源,但只更改了图像。不是布局上的按钮位置。这是我对方法1的工作。

objects = new ArrayList<Integer>();
objects.add(R.drawable.num_one);
objects.add(R.drawable.num_eight);
objects.add(R.drawable.num_six);
objects.add(R.drawable.num_five);
objects.add(R.drawable.num_nine);
objects.add(R.drawable.num_four);
objects.add(R.drawable.num_two);
objects.add(R.drawable.num_three);
objects.add(R.drawable.num_zero);
objects.add(R.drawable.num_seven);

// Shuffle the collection
Collections.shuffle(objects);

List<Button> buttons = new ArrayList<Button>();
buttons.add((Button)findViewById(R.id.name));
buttons.add((Button)findViewById(R.id.boutton_eight));
buttons.add((Button)findViewById(R.id.boutton_six));
buttons.add((Button)findViewById(R.id.boutton_five));
buttons.add((Button)findViewById(R.id.time));
buttons.add((Button)findViewById(R.id.boutton_four));
buttons.add((Button)findViewById(R.id.boutton_two));
buttons.add((Button)findViewById(R.id.search_box));
buttons.add((Button)findViewById(R.id.boutton_zero));
buttons.add((Button)findViewById(R.id.boutton_seven));

for (int i = 0; i < objects.size(); i++) {
    buttons.get(i).setBackgroundResource(objects.get(i));
}

在方法二中:我直接拖拽了按钮,但因为小部件已经在xml布局中设置了。 AddView()给出一个错误消息,表明视图已经设置。这是我为第二个解决方案工作的。

        ll =  (LinearLayout)findViewById(R.id.llShuffleBox);

        //create another two linear layouts which will host 5 buttons horizontally
        top_compte =  (LinearLayout)findViewById(R.id.top_compte);
        bottom_calculator=  (LinearLayout)findViewById(R.id.bottom_calculator);

    buttons.add((Button)findViewById(R.id.name));
buttons.add((Button)findViewById(R.id.boutton_eight));
buttons.add((Button)findViewById(R.id.boutton_six));
buttons.add((Button)findViewById(R.id.boutton_five));
buttons.add((Button)findViewById(R.id.time));
buttons.add((Button)findViewById(R.id.boutton_four));
buttons.add((Button)findViewById(R.id.boutton_two));
buttons.add((Button)findViewById(R.id.search_box));
buttons.add((Button)findViewById(R.id.boutton_zero));
buttons.add((Button)findViewById(R.id.boutton_seven));
Collections.shuffle(buttons);
  for (int i=0;i<5;i++) {
  top_compte.addView(buttons.get(i));
        }

  //add remaining 5 to second layout
        for (int i=5;i<10;i++){
        bottom_calculator.addView(buttons.get(i));
        }
      //  ll.addView(top_compte);
        ll.addView(bottom_calculator);

我的问题是如何在布局上交换按钮的位置。注意按钮0到4是水平线性布局,下面是包含按钮5到9的第二个水平线性布局。另外ll是包含两个水平布局的主LinearLayout。

2 个答案:

答案 0 :(得分:3)

您可以尝试动态创建Buttons(在代码中)。因此,您内部会有LinearLayout(vertical)和两个LinearLayouts(horizontal)。将您的xml文件保存为:

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

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

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

</LinearLayout>

现在,对于活动代码:

ll =  (LinearLayout)findViewById(R.id.llShuffleBox);

//create another two linear layouts which will host 5 buttons horizontally
top_compte =  (LinearLayout)findViewById(R.id.top_compte);
bottom_calculator=  (LinearLayout)findViewById(R.id.bottom_calculator);

// Create an ArrayList to hold the Button objects that we will create    
ArrayList<Button> buttonList = new ArrayList<Button>();

// Create the Buttons, set their text as numeral value of the index variable    
for (int i = 0; i < 10; i++) {
    Button b = new Button(this);
    b.setText("" + (i+1));
    b.setGravity(Gravity.CENTER_HORIZONTAL);

    buttonList.add(b);
}

// Shuffle    
Collections.shuffle(buttonList);


for (int i = 0; i < 10; i++) {

    // Add the first five Buttons to top_compte
    // Add the last five Buttons to bottom_calculator
    if (i < 5) {
        top_compte.addView(buttonList.get(i));
    } else {
        bottom_calculator.addView(buttonList.get(i));
    }
}

修改1:

声明并初始化此全局变量:

// Global variable
int id = 1;

将以下方法添加到您的活动中。它将生成一个在视图树中未使用的ID。

// Generates and returns a valid id that's not in use
public int generateUniqueId(){  
    View v = findViewById(id);  
    while (v != null){  
        v = findViewById(++id);  
    }  
    return id++;  
}

更改代码以将ID设置为按钮:

ll =  (LinearLayout)findViewById(R.id.llShuffleBox);

//create another two linear layouts which will host 5 buttons horizontally
top_compte =  (LinearLayout)findViewById(R.id.top_compte);
bottom_calculator=  (LinearLayout)findViewById(R.id.bottom_calculator);

// Create an ArrayList to hold the Button objects that we will create    
ArrayList<Button> buttonList = new ArrayList<Button>();

// Create the Buttons, set their text as numeral value of the index variable    
for (int i = 0; i < 10; i++) {
    Button b = new Button(this);
    b.setText("" + (i+1));
    b.setGravity(Gravity.CENTER_HORIZONTAL);
    b.setId(generateUniqueId());                    // Set an id to Button

    buttonList.add(b);
}

// Shuffle    
Collections.shuffle(buttonList);


for (int i = 0; i < 10; i++) {

    // Add the first five Buttons to top_compte
    // Add the last five Buttons to bottom_calculator
    if (i < 5) {
        top_compte.addView(buttonList.get(i));
    } else {
        bottom_calculator.addView(buttonList.get(i));
    }
}

您可以使用getId()方法检索ID。如果您不想使用generateUniqueId()方法,可以尝试将行b.setId(generateUniqueId());替换为b.setId(i + 1);。这会将按钮ID设置为1到10.但是,如果视图层次结构中的某些视图具有与任何其他视图相同的ID,则可能会出现问题。

编辑2:

以下是如何将OnClickListeners设置为动态创建的按钮:

for (int i = 0; i < 10; i++) {
    final Button b = new Button(this);
    b.setText("" + (i+1));
    b.setGravity(Gravity.CENTER_HORIZONTAL);

    b.setId(i + 1);

    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dealWithButtonClick(b);
        }            
    });

    bList.add(b);

}

这是dealWithButtonClick(Button)方法的一种可能实现。您可以根据自己的需要进行更改:

public void dealWithButtonClick(Button b) {
    switch(b.getId()) {
      case 1: 
        b.setBackground(getResources().getDrawable(R.drawable.some_drawable_1));
        break;
    case 2:
        b.setBackground(getResources().getDrawable(R.drawable.some_drawable_2));
        break;

    ........

    ........

    ........

    case 10:
        b.setBackground(getResources().getDrawable(R.drawable.some_drawable_10));
        break;
    default:
        b.setBackground(getResources().getDrawable(R.drawable.some_drawable_for_when_no_cases_were_matched));
            break;        }
}

答案 1 :(得分:0)

我有同样的问题(随机播放按钮),我想出了一个简单的解决方案 我只使用了一组整数个按钮id和简单的shufle。在我的情况下就足够了。它很简单,你也得到相同的结果。

Integer[] a =  { 0x7f090049, 0x7f09004b, 0x7f09004c, 0x7f090048};
Collections.shuffle(Arrays.asList(a));

button1 = (Button)findViewById(a[0]);
button2 = (Button)findViewById(a[1]);
button3 = (Button)findViewById(a[2]);
button4 = (Button)findViewById(a[3]);

我希望这会有所帮助。 问候 !! (为我破碎的英语而烦恼)