1

xml レイアウトに 10 個のボタンがあり、10 個のボタンの位置をランダムに交換したいと考えています。私はそれを達成するために2つの方法を試しました。

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

方法 2: ボタンを直接シャッフルしましたが、ウィジェットは既に xml レイアウトに設定されているためです。AddView () は、ビューが既に設定されているというエラー メッセージを表示します。これが私の2番目の解決策です。

        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 を含む 2 番目の水平線形レイアウトがあります。また、ll は 2 つの水平レイアウトを含む Main LinearLayout です。

4

2 に答える 2

0

私は同じ問題(シャッフルボタン)を抱えていましたが、ボタンIDの整数の配列と単純なシャッフルを使用した簡単な解決策を思いつきました。私の場合はそれで十分でした。それは簡単で、同じ結果が得られます。

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

これが役立つことを願っています。よろしく!! (私の壊れた英語でごめんなさい)

于 2014-12-08T17:54:48.413 に答える