[Android Development] example 4-guess the box in which the gem is stored, android4-

Source: Internet
Author: User

[Android Development] example 4-guess the box in which the gem is stored, android4-
A mini game that implements "Guess which box the gem is placed in": There are three boxes in the main interface. clicking any box will open the box and show whether there are any gems in it, in addition, the box that has not been clicked is set to semi-transparent. The box that has been clicked is displayed normally, and the corresponding result is displayed based on whether the box has a gem. If the box you clicked does not have a gem, "Sorry, I guess it is wrong. Do you want to try again? "Prompt text, if You guessed it, it will make all the boxes transparent, and show" Congratulations, you guessed it, I wish you happiness! "

:

When the user does not select a box:

The effects of the box with the gemstone selected by the user:


The user does not select a box with a gem:


Force landscape screen method:
In AndroidManifest. in the xml configuration file, add android: screenOrientation = "landscape" // horizontal screen android: theme = "@ android: style/Theme. noTitleBar. fullscreen "// remove the status bar and title bar

Implementation Method:
Res/layout/main. xml:
Table layout: divided into three layers: Up and down, placing game information, three boxes of the game, and the "Next" button
<TableLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: id = "@ + id/tableLayout1" android: background = "@ drawable/background"> <TableRow android: id = "@ + id/tableRow1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: gravity = "center" android: layout_weight = "2"> <TextView android: text = "@ string/title" android: padding = "10px" android: gravity = "center" android: textSize = "35px" android: textColor = "# FFFFFF" android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </TableRow> <TableRow android: id = "@ + id/tableRow2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: gravity = "center" android: layout_weight = "1"> <LinearLayout android: orientation = "horizontal" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <ImageView android: id = "@ + id/imageview1" android: src = "@ drawable/box_default" android: paddingLeft = "30px" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <ImageView android: id = "@ + id/imageview2" android: src = "@ drawable/box_default" android: paddingLeft = "30px" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <ImageView android: id = "@ + id/imageview3" android: src = "@ drawable/box_default" android: paddingLeft = "20px" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout> </TableRow> <LinearLayout android: orientation = "horizontal" android: layout_weight = "1" android: gravity = "center_horizontal" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <Button android: text = "try again" android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout> </TableLayout>

MainActivity:

Package com. example. test; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. textView; public class MainActivity extends Activity {// defines an array int [] imageIds = new int [] {R. drawable. box_default, R. drawable. box_default, R. drawable. box_ OK}; private ImageV Iew image1; private ImageView image2; private ImageView image3; private TextView result; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); image1 = (ImageView) findViewById (R. id. imageview1); // obtain the ImageView1 component image2 = (ImageView) findViewById (R. id. imageview2); // obtain the ImageView2 component image3 = (ImageView) findViewById (R. id. imageview3); // obtain the ImageView 3 component result = (TextView) findViewById (R. id. textView1); // get the TextView1 component reset (); // disrupt the box order // Add a click event for the first box to listen to image1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {isRight (v, 0); // judgment result }}); // Add the Click Event listener image2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {isRight (v, 1) for the second box ); // judgment result}); // Add an event listener image3.setOnClickListener (new OnClickListener () for the third box () {@ Overridepublic void onClick (View v) {isRight (v, 2); // judgment result}); // obtain the "play again" Button button Button = (button) findViewById (R. id. button1); // Add an event listener button for the "play again" button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {reset (); result. setText (R. string. title); // restore the title to the default value. // restore the image transparency to image1.setAlpha (255); image2.setAlpha (255); image3.setAlpha (255 ); // set the image to be able to click image1.setClickable (true); image2.setC Lickable (true); image3.setClickable (true); image1.setImageDrawable (getResources (). getDrawable (R. drawable. box_default); image2.setImageDrawable (getResources (). getDrawable (R. drawable. box_default); image3.setImageDrawable (getResources (). getDrawable (R. drawable. box_default) ;}}) ;}/ ** determine the result of the guess. * param v * param index **/private void isRight (View v, int index) {// use the image resource ID in the random array to set each ImageViewimage1.setImageD Rawable (getResources (). getDrawable (imageIds [0]); image2.setImageDrawable (getResources (). getDrawable (imageIds [1]); image3.setImageDrawable (getResources (). getDrawable (imageIds [2]); // sets a translucent effect for each imageView./* The Alpha channel is an 8-bit grayscale channel, this channel uses a gray scale of 256 * to record the transparency information in the image, defining transparent, opaque, and translucent regions */image1.setAlpha (100); image2.setAlpha (100); image3.setAlpha (100 ); imageView v1 = (ImageView) v; v1.setAlpha (255); // you can no longer click image1.setClickable (False); image2.setClickable (false); image3.setClickable (false); if (imageIds [index] = R. drawable. box_ OK) {result. setText ("Congratulations, you guessed it. I wish you happiness! ");} Else {result. setText (" sorry, I guessed it wrong. Do you want to try again? ") ;}} // Use a random number to specify the private void reset () {for (int I = 0; I <3; I ++) {int temp = imageIds [I]; int index = (int) (Math. random () * 2); imageIds [I] = imageIds [index]; imageIds [index] = temp ;}}}

Reprinted please indicate the source: http://blog.csdn.net/acmman/article/details/44851479

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.