Android Learning the next day: Toast (Reminder), menu, intent explicit and implicit (including open, fit site, call dial interface, etc.)

Source: Internet
Author: User


1.Toast Reminders
Add a reminder for the button program that was written yesterday, and add the following code to the mainactivity:
Button BT1 = (button) Findviewbyid (r.id.button_1); Bt1.setonclicklistener (new View.onclicklistener () {   @Override Public   void OnClick (view view) {        Toast.maketext (mainactivity.this, "You clicked Button 1", Toast.length_short). Show ();    });

the Findviewbyid () method is used to get the elements in the layout file file, and the value is specified by the property ID. (The return value is a view object and needs to be turned into a button object)The Setonclicklistener () method is used to register the listener, click ExecuteThe Maketext () method requires three parameters, the first being the context (a toast-requirement, the activity itself being a context), the second being the displayed content, and the third being the display at times.
The results of the operation are as follows:

2.Menu Menu
first create a menu folder in the Res directory and create an XML file within the file named main
the code in the Main.xml file is as follows:
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item       android:id= "@+id/add _item "       android:title=" Add "/>    <item       android:id=" @+id/remove_item "android:title="       Remove "/></menu>

each item tag is defined as an option in a menu
Add the following code in Mainactivity, overriding the Oncreatoptionsmenu () method
public boolean Oncreateoptionsmenu (Menu menu) {   Super.oncreateoptionsmenu (menu);    Getmenuinflater (). Inflate (R.menu.main,menu);   return true;}

the Getmenuinflater () method is able to get the Menuinflater object, and the inflate () method can be used to create a menu for the current activity. The inflate () method requires two parameters, the first is the resource file from which to create the menu, and the second is used to specify which menu item to add to. (The return value true is used to display the menu)
The results of the operation are as follows:
3. Add listeners to the menu
Add the following code in Mainactivity, overriding the Onoptionsitemselected () method
public boolean onoptionsitemselected (MenuItem item) {   switch (Item.getitemid ()) {case       R.id.add_item:            Toast.maketext (This, "You clicked the Add button", Toast.length_short). Show ();           break;       Case R.id.remove_item:            toast.maketext (This, "You clicked the Remove button", Toast.length_short). Show ();           break;       Default:    }   return true;}

Use the Item.getitemid () method to determine which menu item is clicked.
The results of the operation are as follows (click Add):

4. Explicit Intent
start by creating an activity, named Secondactivity
public class Secondactivity extends appcompatactivity{    protected void onCreate (Bundle savedinstancestate) {       Super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_second);    }}

create an XML in layout, named Activity_second
<?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:o rientation= "vertical" >    <textview       android:layout_width= "wrap_content"       android:layout_height= " Wrap_content "       android:text=" @string/hello_world "  /></linearlayout>


I let this new activity show Hello world
Finally, register for the new event in Androidmanifest
<activity android:name= ". Secondactivity "></activity>

To be able to use the second activity, change the listener for the button in mainactivity to:
Button BT1 = (button) Findviewbyid (r.id.button_1); Bt1.setonclicklistener (new View.onclicklistener () {   @Override Public   void OnClick (view view) {        Intent Intent = new Intent (mainactivity.this,secondactivity.class);        StartActivity (intent);    }});

The results of the program run are as follows (click Button 1):


The Intent () method requires two parameters, and the first parameter context requires that each start activity be given the contexts, and the second class specifies the activity target that you want to start. the StartActivity () method is used to initiate the activity to perform this intent.
5. Implicit Intent
first modify the secondactivity that you just registered in Androidmanifest:
<activity android:name= ". Secondactivity ">    <intent-filter>        <action android:name=" Bistu.com.test.ACTION_START "/>        <category android:name= "Android.intent.category.DEFAULT"/>    </intent-filter></activity >

then modify the button listener in the mainactivity:
Button BT1 = (button) Findviewbyid (r.id.button_1); Bt1.setonclicklistener (new View.onclicklistener () {   @Override Public   void OnClick (view view) {        Intent Intent = new Intent ("Bistu.com.test.ACTION_START");        StartActivity (intent);    }});

then run the program, just like the explicit effect. you will find that the category is not specified in the listener and, in effect, default. If you add the phrase "intent. Addcategory ( " Bistu.com.test.MY_CATEGORY ") to the listener, you will get an error, just in the event registration, adding "< category android : name= " Bistu.com.test.MY_CATEGORY " /> " can.
6. More Implicit intent usage
①. Change the button to click to open the Web page and change the listener in mainactivity to:
Bt1.setonclicklistener (New View.onclicklistener () {   @Override public   void OnClick (view view) {        Intent Intent = new Intent (Intent.action_view);        Intent.setdata (Uri.parse ("http://www.baidu.com"));        StartActivity (intent);    }});

The results of the operation are as follows:

(click on the button, the browser opened the Baidu website)The Wang Zhi string is parsed into a URI object by the Uri.parse () method, and the URI object is passed in by calling the Insert's SetData () method.
②. On the basis of ①, let this program be adapted to the Web page
Modify the registration code to:
<activity android:name= ". Secondactivity ">    <intent-filter>        <action android:name=" Android.intent.action.VIEW "/>        <category android:name= "Android.intent.category.DEFAULT"/>        <data android:scheme= "http"/>    </intent-filter></activity>

After clicking the button:

you can choose to use the software to fit a webpage (but not use it)
③. Calling the system dial-up interface
Change the button's listener to:
Bt1.setonclicklistener (New View.onclicklistener () {   @Override public   void OnClick (view view) {        Intent Intent = new Intent (intent.action_dial);        Intent.setdata (Uri.parse ("tel:10086"));        StartActivity (intent);    }});

Click on the button after the following:


Android Learning the next day: Toast (Reminder), menu, intent explicit and implicit (including open, fit site, call dial interface, etc.)

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.