android - Using ViewFlipper with onclick to switch views. -
I'm totally new to Android, with so many people using ViewFLipper with Random.nextInt () Click to change the layouts (button) Now I have 3 XML layouts starting from 1_view.xml 2_view.xml 3_view.xml 1_view.xml I have a button when the button is clicked then I should get a random layout . It works. But the problem is now, sometimes I get the same layout (1_view.xml). When a user clicks on a button on (1_view.xml), I want to go to layout (2_view.xml and 3_view.xml). can u help me. Thank you. (Soory for my english)
Codes
Main.xml & LT; RelativeLayout Android: Layout_width = "Fill_parent" Android: Layout_height = "Wrap_content" android: orientation = "vertical" android: padding = "10 DP" & gt; & Lt; 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: Text Size = "40 SP" Android: Textstyle = "bold" /> & Lt; 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: Text Collar =" # FF 20000 "Android: Text Size =" 40 SP "Android: Text Style =" Bold "/> & Lt; / RelativeLayout & gt; & Lt; ViewFlipper Android: id = "@ + id / viewFlipper1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" & gt; & Lt; Include Android: id = "@ + id / 1_View" layout = "@ layout / 1_view" /> & Lt; Include Android: id = "@ + id / 2_View" layout = "@ layout / 2_view" /> & Lt; Include Android: id = "@ + id / 3_View" layout = "@ layout / 3_view" /> & Lt; / ViewFlipper & gt; & Lt; / LinearLayout & gt; Men.Java // FlipReview MyFilePilter = (Viewflipper) Find VVBID (R.D.Flipper1); TVRight = (TextView) Find ViewById (R.ID.TVRight); TVLeft = (TextView) findViewById (R.id.TVLeft); // 1_view.xml Q1_btn1 = (button) Find ViewById (R.id.btn_1); Q1_btn2 = (button) findViewById (R.id.btn_2); Q1_btn3 = (button) Find ViewById (R.id.btn_3); Q1_btn4 = (Button) Find ViewById (R.id.btn_4); Ql_btnlksetOnClickListener (new OnClickListener () {public on zero (View v) {Wrongnum ++; WrongResult.setText (Integer.toString (Wrongnum))}}}); Q1_btn2.setOnClickListener (New OnClickListener () {public void See onClick (v) {Rightnum ++; RightResult.setText (Integer.toString (Rightnum)); Random RandomView = new Random (); MyViewFlipper.setDisplayedChild (RandomView.nextInt (3 ));}}); Q1_btn3.setOnClickListener (New OnClickListener) {Public Zero onClick (see v) {Wrongnum ++; WrongResult.setText (Integer.toString (Wrongnum))}}); Q1_btn4.setOnClickListener (New OnClickListener () {see public void onClick (v) {Wrongnum ++; WrongResult.setText (Integer.toString (Wrongnum));}});
You can do this in your code:
< Code> random RandomView = new random (); Int nextViewIndex = RandomView.nextInt (3); While (nextViewIndex == MyViewFlipper.getDisplayedChild ()) {nextViewIndex = RandomView.nextInt (3); } MyViewFlipper.setDisplayedChild (nextViewIndex); Basically just call random.nextInt () until the current view matches the flapper's index.
To show only one view flyer randomly:
// List it once & lt; Integer & gt; VfIndices = New Arrestist & lt; Integer & gt; (); For (int i = 0; i This idea uses an Arrayist to track the view flopper index that has not been shown. When the button is clicked, select a random indicator from this array and set the viewFlipper to it. Then delete that index from the hello next time the button is clicked, it will show the remaining flippers.
Comments
Post a Comment