Snail-android basic Simple scissors Game

Source: Internet
Author: User

Mainactivity.java

Package Org.example.guess;import Android.app.activity;import Android.os.bundle;import android.view.View;import Android.view.view.onclicklistener;import Android.widget.imagebutton;import Android.widget.imageview;import Android.widget.textview;public class Mainactivity extends Activity {private ImageButton r_imgbtn, p_imgbtn, s_imgbtn;// The buttons of stone, cloth and scissors are private ImageView imgview; Game interface above the button private TextView reslut_tv, COUN_TV; Results and number of games textviewint count = 0; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); r_imgbtn = (ImageButton) Findviewbyid (R.id.btnrock);p _ IMGBTN = (ImageButton) Findviewbyid (r.id.btnpaper); s_imgbtn = (ImageButton) Findviewbyid (r.id.btnsci); Imgview = ( ImageView) Findviewbyid (r.id.viewcmp); reslut_tv = (TextView) Findviewbyid (r.id.textresult); coun_tv = (TextView) Findviewbyid (R.id.textcount); Myonclicklistener Myonclicklistener = new Myonclicklistener (); R_imgbtn.setonclicklistener (MyOnClickListener);p _imgbtn.setonclicklistener (Myonclicklistener); S_imgbtn.setonclicklistener (Myonclicklistener);} Private class Myonclicklistener implements Onclicklistener {@Overridepublic void OnClick (View v) {//TODO auto-generated m Ethod stubint rand = (int) (Math.random () * 3 + 1); Get the random number of count++;//game Times ++switch (rand) {/** * when rand=1, that is, the computer out of the stone, and then judge what the user out. */case 1:imgview.setimageresource (r.drawable.rock); switch (V.getid ()) {Case R.id.btnrock:reslut_tv.settext ( GetString (R.string.result) + getString (R.string.result_draw)) Coun_tv.settext ("Number of games:" + count); break;case R.id.btnpaper:reslut_tv.settext (getString (r.string.result) + getString (r.string.result_lose)); Coun_tv.settext (" Number of Games: "+ count"), Break;case R.id.btnsci:reslut_tv.settext (getString (r.string.result) + getString (R.string.result_win ); Coun_tv.settext ("Number of games:" + count); Break;case 2:imgview.setimageresource (r.drawable.paper); switch (V.getid ()) {Case R.id.btnrock:reslut_tv.settext ( GetString (R.string.result) + getString (r.string.reSult_win)); Coun_tv.settext ("Number of games:" + count); Break;case R.id.btnpaper:reslut_tv.settext (getString (R.string.result) + getString (R.string.result_draw)) Coun_tv.settext ("Number of games:" + count); Break;case R.id.btnsci:reslut_tv.settext ( GetString (R.string.result) + getString (r.string.result_lose)) Coun_tv.settext ("Game count:" + count); Break;case 3:imgview.setimageresource (r.drawable.scissors); switch (V.getid ()) {case R.id.btnrock:reslut_tv.settext (GetString (R.string.result) + getString (r.string.result_lose)); Coun_tv.settext ("Number of games:" + count); break;case R.id.btnpaper:reslut_tv.settext (getString (r.string.result) + getString (R.string.result_win)); Coun_tv.settext (" Number of Games: "+ count"), Break;case R.id.btnsci:reslut_tv.settext (getString (r.string.result) + getString (r.string.result_ Draw)); Coun_tv.settext ("Number of games:" + count); Break;}}}}

Activity_main.xml

<relativelayout 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/welcome" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/ Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/ Activity_vertical_margin "tools:context=". Mainactivity "> <imageview android:id=" @+id/fg_title "android:layout_width=" Match_parent "an droid:layout_height= "Wrap_content" android:src= "@drawable/fg_title"/> <textview android:id= "@+id/l Ine "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:layout_below=" @         Id/fg_title "android:ellipsize=" marquee "android:focusable=" true "android:focusableintouchmode=" true " Android:marqueerepeatlimit= "Marquee_forever" android:singleline= "true" android:text= "@string/notification" Android:textsize= "10sp"/> <textview android:id= "@+id/cmpshow" android:layout_width= "Wrap_conte NT "android:layout_height=" Wrap_content "android:layout_below=" @id/line "android:layout_marginleft=" 2 0DP "android:text=" @string/cmpshow "android:textcolor=" #ff0000 "/> <textview android:id=" @+i D/playershow "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout _alignparentright= "true" android:layout_below= "@id/line" android:layout_marginright= "20DP" Android:te        xt= "@string/playershow" android:textcolor= "#ff0000"/> <imagebutton android:id= "@+id/btnrock" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@id/pla Yershow "Android:layout_below= "@id/playershow" android:layout_marginright= "20DP" android:layout_margintop= "9DP" Androi d:contentdescription= "@string/rockdes" android:src= "@drawable/rock"/> <imagebutton android:id= "@+i D/btnpaper "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_a lignleft= "@id/btnrock" android:layout_below= "@id/btnrock" android:layout_marginright= "20DP" android:l ayout_margintop= "9DP" android:contentdescription= "@string/paperdes" android:src= "@drawable/paper"/> & Lt;imageview android:id= "@+id/viewcmp" android:layout_width= "wrap_content" android:layout_height= "Wra P_content "android:layout_aligntop=" @id/btnpaper "android:layout_below=" @id/cmpshow "android:layout_m arginleft= "20DP" android:contentdescription= "@string/cmpviewdes"/> <imagebutton android:id= "@+id/b Tnsci "Android:laYout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@id/btnpaper"        android:layout_below= "@id/btnpaper" android:layout_marginright= "20DP" android:layout_margintop= "9DP" android:contentdescription= "@string/scissorsdes" android:src= "@drawable/scissors"/> <textview an Droid:id= "@+id/textresult" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:layout_alignleft= "@+id/viewcmp" android:layout_alignparentbottom= "true" android:layout_marginbottom= " 14DP "android:text=" @string/result "android:textcolor=" #ff6ec7 "android:textsize=" 15sp "/> < TextView android:id= "@+id/textcount" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:layout_alignleft=" @+id/textresult "android:layout_alignparentbottom=" true "android:t ext= "Game count:" AndroId:textcolor= "#ff2400" android:textsize= "15SP"/></relativelayout> 

Welcomeactivity.java

Package Org.example.guess;import Android.app.activity;import Android.content.intent;import android.os.Bundle; public class Welcomeactivity extends Activity {/* * Game start screen 、、、 wait for 1.5s to enter the game interface */@Overridepublic void OnCreate (Bundle bundle {super.oncreate (bundle); Super.setcontentview (R.layout.welcome); Thread splashtread = new Thread () {@Overridepublic void run () {try {sleep (1500);//Action here} catch (Interruptedexception e) {E.printstacktrace ();} Finally {finish ();//Call this activity after the finish method, the main interface press on the phone to put back the key will be directly quit the program//start the main application Intent intent = new intent (); Intent.setclass ( Welcomeactivity.this, Mainactivity.class); startactivity (intent);}}; Splashtread.start ();}}

Welcome.xml

<?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:o rientation= "vertical" >    <imageview        android:src= "@drawable/welcome"         android:layout_width= " Match_parent "        android:layout_height=" Match_parent "/></linearlayout>



Related Article

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.