Android Applet-Fun learning Idiom Games (iv)

Source: Internet
Author: User
Tags gettext

Target effect:

The game page has several small games, because the time reason only did the first guessing idiom, is uses the choice question method, 10 questions within the correct six questions to calculate the success.


1. New Gameactivity.java page and Activity_game.xml page, activity_game.xml as the game Directory page:

Activity_game.xml page:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "android:backg round= "@drawable/bg_ling" android:orientation= "vertical" tools:context= ".        Gameactivity "> <textview android:id=" @+id/tvgameguess "android:layout_width=" Wrap_content "        android:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_margintop= "50DP" Android:clickable= "true" android:onclick= "GamePlay" android:text= "@string/tvgameguess" Android:textco Lor= "#000000" android:textsize= "23sp"/> <textview android:id= "@+id/tvgamewrite" Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center" Android : layout_margintop= "30DP" android:clickable= "true" android:onclick= "GamePlay"        android:text= "@string/tvgamewrite" android:textcolor= "#000000" android:textsize= "23sp"/> < TextView android:id= "@+id/tvgameconn" android:layout_width= "wrap_content" android:layout_height= "wrap        _content "android:layout_gravity=" center "android:layout_margintop=" 30DP "android:clickable=" true " android:onclick= "GamePlay" android:text= "@string/tvgameconn" android:textcolor= "#000000" Android : textsize= "23SP"/></linearlayout>


2. New Gameguessactivity.java page and Activity_game_guess.xml page, activity_game_guess.xml the page to show the rules of guessing the idiom game. Activity_game_guess.xml page:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @drawable/b G_animal "android:orientation=" vertical "> <textview android:id=" @+id/tvguesshelp "android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:l ayout_margintop= "60DP" android:text= "@string/tvguesshelp" android:textcolor= "#000000" Android:textsiz E= "25sp"/> <!--android:background= "#00ffffff" Remove edge--<imagebutton android:id= "@+id/ibstartgame "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_marginleft=        "150DP" android:layout_margintop= "300DP" android:background= "#00ffffff" android:onclick= "Startgame" android:src= "@drawable/btnstaRT "/></linearlayout> 


3. New Playguessactivity.java page and Activity_play_guess.xml page, Activity_play_guess.xml page as a guessing idiom game interface. Activity_play_guess.xml page:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @drawable/b G_animal "android:orientation=" vertical "> <!--always make TextView appear in the middle---<textview android:id=" @+i D/tvexplain "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_        gravity= "center" android:layout_margintop= "50DP" android:text= "TextView" android:textcolor= "#000000" Android:textsize= "22SP"/> <textview android:id= "@+id/tvrightorwrong" android:layout_width= " Wrap_content "android:layout_height=" wrap_content "android:layout_gravity=" center "Android:layout_mar gintop= "70DP" android:text= "android:textcolor=" #08c50d "android:textsize=" 22sp "/> <linea Rlayout Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_marginbottom= "60DP" Android:orien tation= "vertical" > <radiogroup android:layout_width= "wrap_content" Android:layout_heig ht= "wrap_content" android:orientation= "vertical" > <radiobutton android:id= "@+i                D/rbphraseone "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_marginleft= "120DP" android:textcolor= "#000000" android:textsize= "2 0sp "/> <radiobutton android:id=" @+id/rbphrasetwo "android:layout_width=" WR                Ap_content "android:layout_height=" wrap_content "android:layout_marginleft=" 120DP " Android:textcolor= "#000000" android:textsize= "20sp"/> <radiobutton A Ndroid:id= "@+id/rbphraseThree "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content " android:layout_marginleft= "120DP" android:textcolor= "#000000" android:textsize= "20SP"/&G            T                <radiobutton android:id= "@+id/rbphrasefour" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_marginleft= "120DP" Android:textcol Or= "#000000" android:textsize= "20sp"/> </RadioGroup> </LinearLayout> <linea Rlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravi            ty= "center" android:layout_marginbottom= "30DP" android:orientation= "Horizontal" > <imagebutton Android:id= "@+id/ibsubmit" android:layout_width= "wrap_content" android:layout_height= "Wra P_content "            Android:background= "#00ffffff" android:onclick= "Answersubmit" android:src= "@drawable/btn            Submit "/> <imagebutton android:id=" @+id/ibnext "android:layout_marginleft=" 70DP " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:background= "# 00ffffff "android:onclick=" Answersubmit "android:src=" @drawable/btnnext "/> </linearlayout& Gt;</linearlayout>


4.gameactivity.java page jumps to each game page. Gameactivity.java page:
Package Cn.edu.bztc.happyidion.activity;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.view;import Android.widget.TextView;public Class Gameactivity extends Activity {private Intent Intent; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_game);} public void GamePlay (view view) {switch (View.getid ()) {Case R.id.tvgameguess:intent=new intent (this, Gameguessactivity.class); startactivity (intent); Break;case r.id.tvgameconn:break;default:break;}}}


5.gameguessactivity.java Page Click Write to start the game click event. Gameguessactivity.java page:
Package Cn.edu.bztc.happyidion.activity;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.view;public class GameGuessActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_game_guess);} public void Startgame (view view) {switch (View.getid ()) {case R.id.ibstartgame:intent intent=new Intent (This, Playguessactivity.class); startactivity (intent); finish (); break;default:break;}}}


The 6.gamedao.java page defines the query method for the idiom table in the game. Gamedao.java page:
Package Cn.edu.bztc.happyidiom.dao;import Java.util.arraylist;import Java.util.list;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.util.log;import Cn.edu.bztc.happyidiom.db.DBOpenHelper; Import Cn.edu.bztc.happyidiom.db.mydatabasehelper;import Cn.edu.bztc.happyidiom.entity.animal;public class GameDao {private static Gamedao gamedao;private sqlitedatabase db;public Gamedao (context context) {Dbopenhelper dbhelper=new Dbopenhelper (context);d b=dbhelper.opendatabase ();} Public synchronized static Gamedao getinstance (context context) {if (gamedao==null) {gamedao=new Gamedao (context);} return Gamedao;} /* Get idiom information based on ID */public Animal getphrase (String id) {Animal Animal = null; Cursor cursor=db.query ("Animal", NULL, "_id=?", new String[]{id},null,null,null), if (Cursor.movetofirst ()) {animal= New Animal (); Animal.setid (Cursor.getint (Cursor.getcolumnindex ("_id")); Animal.setname (Cursor.getstring ( Cursor.geTcolumnindex ("name"))); Animal.setpronounce (Cursor.getstring (Cursor.getcolumnindex ("pronounce"))); Animal.setantonym (Cursor.getstring (Cursor.getcolumnindex ("antonym")); Animal.sethomoionym (Cursor.getString ( Cursor.getcolumnindex ("homoionym")); Animal.setderivation (Cursor.getstring (Cursor.getcolumnindex ("derivation") )); Animal.setexamples (Cursor.getstring (Cursor.getcolumnindex ("examples")); Animal.setexplain (Cursor.getstring ( Cursor.getcolumnindex ("explain")));} Cursor.close (); return animal;}}


The 7.playguessactivity.java page is the game interface. Playguessactivity.java page:
Package Cn.edu.bztc.happyidion.activity;import Java.util.arraylist;import Java.util.hashmap;import java.util.List; Import Java.util.random;import Cn.edu.bztc.happyidiom.dao.animaldao;import Cn.edu.bztc.happyidiom.dao.GameDao; Import Cn.edu.bztc.happyidiom.entity.animal;import Android. R.color;import Android.os.bundle;import Android.os.ibinder;import Android.app.activity;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.graphics.Color; Import Android.util.log;import android.view.menu;import Android.view.view;import Android.widget.imagebutton;import Android.widget.radiobutton;import Android.widget.textview;public class Playguessactivity extends Activity {int score= 0;//Total int number=1;//number of topics private TextView tvrightorwrong;private ImageButton ibnext,ibsubmit;private Random random;/ /random number private Animaldao animaldao;private Gamedao gamedao;private list<animal> animallist;private Animal Animal, Animaltwo, Animalthree, animalfour;//randomly generatedIdiom Object Private sharedpreferences pref;private Editor editor;private int[] array = {5, 5, 5, 5};//shaped array, used to generate random order display options private String answer;//Select the answer private TextView tvexplain;private RadioButton rbphraseone, Rbphrasetwo, Rbphrasethree, Rbphrasefour; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_play_guess); tvexplain = (TextView) Findviewbyid (r.id.tvexplain); RbPhraseOne = ( RadioButton) Findviewbyid (r.id.rbphraseone); rbphrasetwo = (RadioButton) Findviewbyid (r.id.rbphrasetwo); Rbphrasethree = (RadioButton) Findviewbyid (r.id.rbphrasethree); rbphrasefour = (RadioButton) Findviewbyid ( R.id.rbphrasefour); Tvrightorwrong = (TextView) Findviewbyid (R.id.tvrightorwrong); ibsubmit= (ImageButton) Findviewbyid (R.id.ibsubmit), ibnext= (ImageButton) Findviewbyid (r.id.ibnext);p ref = Getsharedpreferences (" Fourphrase ", mode_private);//Save the correct idiom and randomly generated three idioms to the document editor = Pref.edit ();//Get the Sharedpreferences.editor object/* Get the random value corresponding to the idiom interpretation */getnumber ();/* shows explanations to TextView */showphrase ();/* gets three random idioms and an option for the correct idiom */getfourphrase ();/* Display to RadioButton */showradibutton ();} /* get random values corresponding to the idiom */private void GetNumber () {animallist = new arraylist<animal> (); Animaldao = Animaldao.getinstance ( this); animallist = Animaldao.getallanimals (); random = new Random (); String id = integer.tostring (random.nextint (Animallist.size ())); Gamedao = Gamedao.getinstance (this); animal = Gamedao.getphrase (ID); animallist = new arraylist<animal> (); Animaldao = Animaldao.getinstance (this); animalList = Animaldao.getallanimals ();} /* Display interpreted to page */private void Showphrase () {Tvexplain.settext (Animal.getexplain ());} /* Get four idiom options */private void Getfourphrase () {random = new random (); String, three = null, four = Null;two = Integer.tostring (Random.nextint (Animallist.size ())); three = Integer.tos Tring (Random.nextint (Animallist.size ())); four = Integer.tostring (Random.nextint (Animallist.size ())); Gamedao = Gamedao.getinstance (this); animaltwo = Gamedao.getphrase (both); Get Random HealthThe value corresponding idiom Animalthree = Gamedao.getphrase (three); animalfour = Gamedao.getphrase (four); Editor.putstring ("0", Animal.getname ());//The four idioms are deposited into document editor.putstring ("1", Animaltwo.getname ()) editor.putstring ("2", Animalthree.getname ()); Editor.putstring ("3", Animalfour.getname ()); Editor.commit (); int no;for (int i = 0; i < Array.Length; i++) {//randomly generate four distinct values as display order No = Random.nextint (4), int j;for (j = 0; J <= I; j + +) {if (no = = Array[j]) {i--;break;}} if (j = = i + 1) {array[i] = no;}} LOG.I ("mainactivity", "0:" + array[0] + "1:" + array[1] + "2:" + array[2] + "3:" + array[3]);} /* Display to RadioButton */private void Showradibutton () {Rbphraseone.settext (pref.getstring (integer.tostring (array[0)), "" )); Rbphrasetwo.settext (Pref.getstring (integer.tostring (array[1]), ""); Rbphrasethree.settext (Pref.getString ( Integer.tostring (array[2]), ""), Rbphrasefour.settext (Pref.getstring (integer.tostring (array[3]), "")); /* Submit answer */public void Answersubmit (view view) {switch (View.getid ()) {Case R.id.ibsubmit:if (Rbphraseone.ischecked ()) answer = Rbphraseone.gettext (). toString (); else if (rbphrasetwo.ischecked ()) answer = Rbphrasetwo.gettext (). ToString (); else if (rbphrasethree.ischecked ()) answer = Rbphrasethree.gettext (). ToString (); Elseanswer = Rbphrasefour.gettext (). toString (); if (answer = = Animal.getname ()) {Tvrightorwrong.settext ("awesome, answer correct");      Tvrightorwrong.settextcolor (Color.rgb (7, +)); ibsubmit.setclickable (false); Answer right or wrong, submit button can not be clicked, prevent one to try to get answer bonus points score+=10;} else {tvrightorwrong.settext ("Ah, I answered wrong"); Tvrightorwrong.settextcolor (Color.rgb (255, XX, XX)); ibsubmit.setclickable (false);} if (score==60&&number<=10) {Tvrightorwrong.settext ("Congratulations, success in the Rush"); Tvrightorwrong.settextcolor (Color.rgb (7      ); ibnext.setclickable (false); Success or failure of the hurdle, the Next button can not be clicked}else if (score<=60&&number>=10) {Tvrightorwrong.settext ("Sorry, failure of the barrier"); Tvrightorwrong.settextcolor (Color.rgb (255, XX, XX)); ibnext.setclickable (false);} Break;case R.id.ibnext://Generate Next topic clearphrase ();//Empty TextView and RadioButton Properties GetNumber ();  Showphrase (); Getfourphrase (); Showradibutton (); number+=1; Number of topics plus a break;}}      public void Clearphrase () {//jumps to the next question, deleting the previous control's property Tvrightorwrong.settext ("");    Empty prompt ibsubmit.setclickable (true);    The submit button can be clicked Rbphraseone.setchecked (false); Radio buttons are not selected rbphrasetwo.setchecked (false); rbphrasethree.setchecked (false); rbphrasefour.setchecked (false);}}


8. SOURCE sharing: Click to open the link

9. Time reason page does not beautify, the interface layout also has some problems, temporarily can only provide the simple study, please understand.











Android Applet-Fun learning Idiom Games (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.