Preliminary analytical intent

Intent shuttle activity using
explicit Intent
an Activity too thin, bald as a program ape, senior point we should
we then create an activity named SecondActivity: File-New-Activity- Empty Activity
we get a second activity
activity-main.xml the directory layout editing, the following code is replaced

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1">
</Button>


</LinearLayout>

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
<a representative of the Button button after the other we can speak
Then we add the following code in MainActivity

Button1 = the findViewById the Button (R.id.button_1);
button1.setOnClickListener (new new View.OnClickListener () {
@Override
public void the onClick (View V) {
the Intent the Intent Intent new new = (MainActivity.this, SecondActivity.class);
startActivity ( Intent);
}
});
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
the findViewById () method for obtaining the elements defined in the layout file, button_1 is through android: so designated id = "@ + id / button_1 ".
Then we register a listener for the button by calling setOnClickListener () method, which executes onClick method of the button is clicked.
Intent plurality overloaded constructor, which is one. This constructor receives two parameters, the first one of Context context requires start-up activities, and the second class is the targeting of the desired promoter activity. After the establishment of methods you can start the activity by calling startActivity (), which receives an Intent parameter, we will build a good pass in Intent on the line.
Then run, you can skip the event!

Implicit Intent
compared to the explicit, implicit Intent bit smarter, we can specify more abstract action and category information, and then let the system help us find which activities you want to start.
action: a string that indicates the current activities can respond to this string
category: indicates the current environmental activities
only action and at the same time matching category, this activity Intent to respond to
our first open AndroidManifest.xml, find SecondActivity, add the following code

<Activity Android: name =. "SecondActivity">
<filter-Intent>
<Action Android: name = "the Start" />
<category Android: name = "android.intent.category.DEFAULT" />
</ filter-Intent>
</ Activity>
. 1
2
. 3
. 4
. 5
. 6
here we specify the action to SecondActivity "Start", category of "android.intent.category.DEFAULT".
Enter MainActivity, modify the contents like this onClick

void the onClick public (View V) {
the Intent the Intent Intent new new = ( "the Start");
startActivity (Intent);
}
. 1
2
. 3
. 4
we can also enter the second activity

Guess you like

Origin www.cnblogs.com/hyhy904/p/11667857.html