[Android game development 5] Game registration interface Demo-implements switching between two activities and data interaction!

Source: Internet
Author: User

 

Today, I will explain how to implement switching between two or more activities and data interaction in Surfaceview. I have provided a demo of the game logon interface to improve my image, I also wrote the layout of the input interface at will, mainly because the function does not take time to beautify, so you can change the layout in xml or code by yourself, the width and height of each widget. Okay, next (I love it ~ Doraemon, so I have to use the figure of Doraemon, Wahaha)

 

Because there are three classes in the Code, it is not convenient to paste them all here; you can download the source code at the end of the article;

There are two steps to switch between two activities:

1. Implement code switching. 2. Declare another acitivity in the configuration!

Let's take a look at the first step: Here is a piece of code in the touch screen processing:

 

 

Public boolean onTouchEvent (MotionEvent event ){

Float pointx = event. getX ();

Float pointy = event. getY ();

If (pointx> bp_x + 14 & pointx <bp_x + 14 + 117 ){

If (pointy> bp_y + 43 & pointy <bp_y + 43 + 15 ){

// Account

Intent I = new Intent (); // get an instance of Intent

I. putExtra ("count", 1); // write data

I. putExtra ("himi", str_zh );

I. setClass (MainActivity. instance, Register. class); // sets the current activity and the class to be operated

MainActivity. instance. startActivity (I); // use the current activity to start another activity

}

}

}

Display and define an intent object. The mechanism of the Intent class is to assist in interaction. The detailed description is not described here;

The putExtra () function in Intent interacts with two activities. This method is the same as put in hashtable or hashmap. The first parameter is the key (index ), the latter parameter volue (value). Based on the key, we can get the corresponding volue. Then I will also attach the accepted processing.

The setClass () function in Intent is also used to input two parameters. The first parameter is the activity object of the current instance. The next parameter is the activity class to be opened! Then we can use the current activity object to start another activity. Then let's take a look at how another activity creates and accepts data.

/**

*

*/

Package com. himi;

Import android. app. Activity;

Import android. content. Intent;

Import android. OS. Bundle;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

Import android. widget. EditText;

Import android. widget. LinearLayout;

Import android. widget. TextView;

/**

* @ Author Himi

*

*/

Public class Register extends Activity {

Private Button button_ OK;

Private EditText et;

Private TextView TV;

Private LinearLayout ly;

Private Register rs;

Private byte count;

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Rs = this;

Ly = new LinearLayout (this );

Button_ OK = new Button (this );

Button_ OK .setWidth (100 );

Button_ OK .setText ("OK ");

Button_ OK .setOnClickListener (new OnClickListener (){

Public void onClick (View v ){

If (count = 1 ){

MySurfaceView. str_zh = et. getText (). toString ();

} Else if (count = 2 ){

MySurfaceView. str_pass = et. getText (). toString ();

}

Rs. finish ();

}

});

Intent intent = this. getIntent ();

Count = (byte) intent. getIntExtra ("count", 0 );

String temp_str = "";

String temp_str2 = "";

Et = new EditText (this );

TV = new TextView (this );

If (count! = 3 ){

Temp_str = intent. getStringExtra ("himi ");

If (count = 1 ){

Rs. setTitle ("enter an account! ");

} Else {

Rs. setTitle ("enter the password! ");

}

Ly. addView (TV );

Ly. addView (et );

Ly. addView (button_ OK );

If (temp_str! = Null ){

Et. setText (temp_str );

}

} Else {

Temp_str = intent. getStringExtra ("himi_zh ");

Temp_str2 = intent. getStringExtra ("himi_pass ");

Rs. setTitle ("your input information :");

TV. setText ("account:" + temp_str + "/n" + "password" + temp_str2 );

Ly. addView (TV );

Ly. addView (button_ OK );

If (temp_str! = Null ){

Et. setText (temp_str );

}

}

SetContentView (ly );

}

}

The code above shows that to create an activity, you only need to inherit the Activity and override the onCreate () method. Of course, the creation still requires a very important step. In the second step, we will explain in detail how to accept data from previous activities ,.

Intent intent = this. getIntent ();

Count = (byte) intent. getIntExtra ("count", 0 );

The acceptance is also very concise and easy to understand. Create an Intent object and call the getIntExtra function to obtain the previous data, according to the key! Of course, the getStringExtra () and other functions are similar, but different functions are selected based on the data you input. The children's shoes should pay attention to what the second parameter in getIntExtra means. In fact, when the key cannot be found to match, it will return 0 by default;

Next we will introduce Step 2: declare in configuration

When creating an activity, we must live the class we created in AndroidMainFeset. xml. It is an Activity! The code in xml is attached below!

<Activity android: name = "com. himi. Register" android: theme = "@ android: style/Theme. Dialog"

Android: screenOrientation = "landscape" android: configChanges = "keyboardHidden | orientation">

</Activity>

<! -- I am a comment -->

<Activity android: name = "com. himi. Register"> </activity>

<! ----> This symbol is a comment

Here we have two declaration Methods Separated by comments. The preceding Declaration also sets some attributes and theme display formats, in fact, the simplest statement is also possible, just as the statement below can be used.

Make sure to declare a new activity. Do not forget this step.

Note: There is a property in the configuration-> android: screenOrientation this is to set the horizontal screen, so clicking on the vertical screen will have a problem. If you want to display the portrait screen normally, you can delete android in xml by yourself: screenOrientation = "landscape.

 

Source code: http://www.bkjia.com/uploadfile/2011/1113/20111113073935833.rar

Author HIMI

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.