Android studio Intent

Use of Intent

2021/3/16

Purpose

Deepen the programming concept of Android logic and view separation; be familiar with and use Intent

Experiment content

1. Add a button to the Activity that was originally empty, and make the click response of the button a prompt, with the prompt content as your student ID and name.

(1) Create an empty Activity

(2) Add a button

Add a button in the activity_main.xml of the layout with the id button_1

Insert picture description here

Declare this button under MainActivity under java

Insert picture description here

Create a new button click method

onClick(View v): the action after the click

Set the click event of the button to point to our new click method

setOnClickListener: set the action to be triggered after the click

Now we add the code for the pop-up dialog box in onClick.

AlertDialog: a dialog box class.

MainActivity.this: The location where the dialog box is displayed.

setTitle: Set the title.

setMessage: Set the content.

setPositiveButton: Set the button of the dialog box.

show(): Display the dialog box.

Insert picture description here

final effect

Insert picture description here

Insert picture description here

2. Jump between two Activities

(1) Jump through the button to display the new Intent(context, toActivity)

Create a new blank activity (SecondActivity)

Create the xml file under the corresponding layout and add a TextView (the content is Welcome to the new page!)

Insert picture description here

Declare TextView under SecondActivity

Insert picture description here

Next, declare the second button in MainActivity and add the second button (id=button_2) in activity_main.xml, the process is the same as in content 1.

Insert picture description here

Create a new button click method

button_2.setOnClickListener(new View.OnClickListener() {
@Override

        public void onClick(View v) {

            Intent intent=new Intent(MainActivity.this,SecondActivity.class);

            startActivity(intent);
        }
    });

Declare SecondActivity in AndroidManifest.xml under manifests

Insert picture description here

The final effect (if it can't be displayed, you can also see the attachment 1.gif)

Insert picture description here

(2) Implicitly jump through action and category

Principle: The implicit jump does not specify the activity we want to jump to. Instead, we need to fill in the parameter in new Intent(), which corresponds to the value of the action attribute under the label of the activity you want to implicitly jump to. Two They need to be consistent, and category attributes must be added at the same time.

Same as above, first create a button on mainactivity (id=button_3)

Note that the parameters passed in are not MainActivity.this and SecondActivity, but a parameter we customize (that is, this parameter can be defined arbitrarily, but this customization is based on SecondActivity, that is, this parameter must correspond to us The attribute of the ActivityManifest.xml to be jumped)

Add the following code in mainactivity

Insert picture description here

Add the following code above the manifest.SecondActivity

Insert picture description here

The final effect (if it can't be displayed, you can also see the attachment 2.gif)

Insert picture description here

(3) Jump to the browser

As above, continue to create a button (id=button_4)

Add the following code on mainactivity

Insert picture description here

uri: Universal Resource Identifier ("URI" for short). Uri represents the data to be manipulated, and every resource (images, video clips, web pages, etc.) available on Android can be represented by Uri. Conceptually, URI includes URL.

The final effect (if it cannot be displayed, you can also see the attachment 3.gif)

Insert picture description here

3. Additional questions (stay on the startup page for 5 seconds, automatically jump to the next page)

Because of file conflicts, I created a new empty activity

Add the following code on mainactivity

Insert picture description here

Timer timerTask is used here

timer is a timer

Multiple TimerTasks can share a Timer. A thread can be created by calling the Schedule method of Timer, and the TimerTask loops infinitely after calling schedule once.

The final effect (if it cannot be displayed, the attachment 4.gif can also be seen)

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44956841/article/details/115036297