0

Random.nextInt()でViewFLipperを使用しています。クリック(ボタン)でレイアウトを変更します。これで、3つのxmlレイアウトができました。1_view.xml2_view.xml3_view.xml。1_view.xmlから始めて、そこにボタンがあります。ボタンをクリックすると、ランダムなレイアウトが表示されます。できます。しかし、問題は今、同じレイアウト(1_view.xml)を取得することがあります。ユーザーが(1_view.xml)のボタンをクリックすると、私が残したレイアウト(2_view.xmlおよび3_view.xml)に移動するようにします。

コード

Main.xml

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <TextView
            android:id="@+id/TVLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="left|top"
            android:text="0"
            android:textColor="#4CB848"
            android:textSize="40sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TVRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/TVLeft"
            android:gravity="right"
            android:text="0"
            android:textColor="#FF0000"
            android:textSize="40sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/1_View"
            layout="@layout/1_view" />

        <include
            android:id="@+id/2_View"
            layout="@layout/2_view" />

        <include
            android:id="@+id/3_View"
            layout="@layout/3_view" />
    </ViewFlipper>
</LinearLayout>

Main.java
        // FlipperView
        MyViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

        TVRight = (TextView) findViewById(R.id.TVRight);
        TVLeft = (TextView) findViewById(R.id.TVLeft);

        // 1_view.xml
        Q1_btn1 = (Button) findViewById(R.id.btn_1);
        Q1_btn2 = (Button) findViewById(R.id.btn_2);
        Q1_btn3 = (Button) findViewById(R.id.btn_3);
        Q1_btn4 = (Button) findViewById(R.id.btn_4);

        Q1_btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }

        });

        Q1_btn2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Rightnum++;
                RightResult.setText(Integer.toString(Rightnum));

                Random RandomView = new Random();
                MyViewFlipper.setDisplayedChild(RandomView.nextInt(3));

            }
        });
        Q1_btn3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }
        });
        Q1_btn4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }
        });
4

1 に答える 1

0

あなたはあなたのコードでこれを行うことができます:

 Random RandomView = new Random();
 int nextViewIndex = RandomView.nextInt(3);
 while (nextViewIndex == MyViewFlipper.getDisplayedChild()) {
     nextViewIndex = RandomView.nextInt(3);
 }
 MyViewFlipper.setDisplayedChild(nextViewIndex);

基本的には、現在のviewFlipperのインデックスと一致しなくなるまで、Random.nextInt()を呼び出すだけです。

viewFlipperをランダムに1回だけ表示するには:

// Intialize this once
List<Integer> vfIndices = new ArrayList<Integer>();
for (int i = 0; i < MyViewFlipper.getChildCount(); i++) {
   vfIndices.add(i);
}

// when button is clicked
if (vfIndices.size() == 0) {
   return; // no more view flipper to show!
}
int viewIndex = RandomView.nextInt(vfIndices.size());
MyViewFlipper.setDisplayedChild(vfIndices.get(viewIndex));
vfIndicies.remove(viewIndex);

アイデアは、ArrayListを使用して、表示されていないviewFlipperインデックスを追跡することです。ボタンがクリックされたら、この配列からランダムにインデックスを選択し、viewFlipperをに設定します。次に、そのインデックスをArrayListから削除します。次にボタンをクリックすると、残りのフリッパーの1つが表示されます。

于 2012-07-11T22:39:36.157 に答える