使用ViewFlipper和onclick切换视图

问题描述 投票:0回答:1

我正在使用带有Random.nextInt()的ViewFLipper。更改布局onClick(按钮)。现在我有3个xml布局。 1_view.xml 2_view.xml 3_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));

            }
        });
android viewflipper buttonclick
1个回答
0
投票

您可以在代码中执行此操作:

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

基本上只调用Random.nextInt(),直到它与当前的viewFlipper索引不匹配。

要只随机显示一次viewFlipper:

// 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中删除该索引。下次单击按钮时,它将显示剩余的鳍状肢之一。

© www.soinside.com 2019 - 2024. All rights reserved.