Every Android application consists of interfaces, so every interface here can be viewed as an Activity. Activity can be viewed as a screen interface, that is, a screen interface corresponds to an Activity. The number of interfaces that an application has, that is, the number of activities.
I. Create an Activity
You need to know that every Activity is actually a class. You can create a page by defining the class and completing some operations. The procedure is as follows:
1) define a class, inherit from Activity, and override onCreate Method
public class Act2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }} |
2) define the corresponding configuration file. Note that the name should be in full lowercase format. Generally, the Activity name should be in full lowercase format.
<?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:orientation="vertical" > </LinearLayout> |
3) load the view in the onCreate method and use the setContentView method. The parameter is the act2 view specified by Layout in the R file.
setContentView(R.layout.act2); |
4) Add the activity tag under the AndroidManifest. xml main configuration file to complete Activity registration.
<activity android:name="Act2"></activity> |
This completes Activity creation.
2. jump between activities
1: auxiliary tool Intent
To jump between pages, we need an auxiliary tool to help us activate the Activity component and achieve the jump goal. Intent has the following functions:
1) activate a component: Create an Intent object to activate the component.
For example, there are two activation methods for the Activity component. Here we will briefly introduce one of the methods for directly starting the Activity.
① Create an Intent object to specify the source interface and Target Interface for page Jump
② Use the created Intent object as a parameter of the startActivity method to start the page
Intent I = new Intent (MainActivity. this, Act2.class); // defines the Intent object, indicating the source interface and Target Interface startActivity (I) when the page jumps; // directly start the Activity |
2) Complete sending short messages, making phone calls, and many other basic functions.
◆ Call
Statement analysis: The following statement completes the call function. It starts a call system Activity and the passed Intent object is specified by two parameters, the first parameter indicates that this object is an object with the call function, and the second parameter indicates the target address of the call. Pay attention to the fixed usage of tel.
startActivity(new Intent(Intent.ACTION_CALL,Uri.parse("tel://18704659809"))); |
◆ Text message
Statement analysis: The following statement completes the text messaging function. It starts a system Activity for text messaging, and the Intent object passed is specified by two parameters, the first parameter specifies that this object is an object with the SMS function, and the second parameter specifies the destination address of the text message. Pay attention to the fixed use of smsto.
startActivity(new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto://18704659809"))); |
◆ Permission setting
Before calling and sending text messages, you must add permissions to the AndroidManifest. xml main configuration file to implement this function. perform the following steps: Add the following two lines of code to the main configuration file:
<Uses-permission android: name = "android. permission. SEND_SMS"/> // send a text message <uses-permission android: name = "android. permission. CALL_PHONE"/> // call |
3) data transmission between components
① Create an Intent object to specify the source interface and Target Interface for page Jump
Intent i = new Intent(MainActivity.this,Act2.class); |
② Call the putExtra ("key", "value") method of the Intent object to store data in the form of key-value pairs
I. putExtra ("name", "Zhang San "); |
③ Use the created Intent object as a parameter of the startActivity method to enable the "carrying value" of the page.
④ Receive the values on the interface. Based on the key-worthy type, use the corresponding method to obtain the values through the key.
String val = getIntent().getStringExtra("name"); |
2: page Jump
Here, we want to complete the function: Add a button on the main interface, after the single button, the page Jump will jump to the custom Activity. Let's take a look at how it is implemented?
1) create a project, find the activity_main.xml file in Layout, and add the button in two ways:
◆ Drag the button control directly to the page, and then modify the corresponding code in the xml file to achieve the style of the button you need.
◆ Manually enter the code in the xml file. The mandatory attribute of the buttons here is
① Layout_width and layout_height: used to define the button size. The value is wrap_content adaptive)
② Id: id of the sweater resource used by the program as the button
③ Text: the text of the button, displayed on the button
<Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button"/> |
2) create a custom interface named Act2, as shown in the following steps. Do not repeat it here
3) Add a listener to the main interface button
◆ Define global button member variables in book class
◆ In the override onCreate method, find the button Based on the Resource Identifier as the findViewById method parameter
button = (Button) findViewById(R.id.button1); |
◆ Add an anonymous internal listener for the button
Button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Intent I = new Intent (MainActivity. this, Act2.class); // defines the page Jump auxiliary tool startActivity (I); // starts Activity }}); |
4) run the project and click the button to jump to the page.
We will all feel a bit blind when we are new to android, so we can continue to work on it .. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11095IW9-0.gif "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1279224