"Android Development Learning note", "lesson three" activity and intent

Source: Internet
Author: User

  Let's start by looking at an activity that starts another activity, directly on the code:

(1) to start with multiple activity, first create a new other.xml under Res-layout to act as a layout file for the second activity

    

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <TextViewAndroid:id= "@+id/mytest"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"     /></LinearLayout>

(2) Add a src to add a class file (so in Java, I've been doing C + +, I'm not sure what's in Java)

Remember that the class must inherit activity (public class * * * extends activity), and then override the OnCreate method

    

    

 PackageCom.example.cart;ImportAndroid. r.string;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classOtheractivityextendsactivity{PrivateTextView mytext =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {        Super. OnCreate (savedinstancestate); Setcontentview (r.layout.othery);MyText=(TextView) Findviewbyid (r.id.mytest); Mytext.settext ("Hello activity"); }}

This activity, in fact, is very simple is only a textview, the content is "Hello activity"

(3) Next we need to register this activity in the Androidmanifest.xml

(4) We need to call in the activity of the main, the concrete practice is first in the main activity layout is only a button, then we listen to this button press event,

Pressing this button will invoke another activity via intent, which is the activity we created above, and here is the code:

 PackageCom.example.cart;Importandroid.app.Activity;ImportAndroid.app.ActionBar;Importandroid.app.Fragment;ImportAndroid.content.DialogInterface;Importandroid.content.Intent;ImportAndroid.view.View.OnClickListener;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.os.Build; Public classMainactivityextendsActivity { PublicButton MyButton =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); MyButton=(Button) Findviewbyid (R.id.mybutton); Mybutton.settext ("Nihao-ha2ha"); Mybutton.setonclicklistener (NewMybuttonlisten ()); }        classMybuttonlistenImplementsOnclicklistener {@Override/* If you encounter Aetonclicklistener error, follow the below to do:
1. Change the onclick (dialoginterface arg0, int arg1) to onclick (View v)
2. Android.content.DialogInterface.OnClickListener the import; Change into import Android.view.View.OnClickListener; */
  Public void OnClick (View v) { New Intent ();  Intent.setclass (mainactivity. this, otheractivity. class ); Mainactivity.  This . StartActivity (intent);   } }}

(5) After running the program, first move to the first activity, click the button and then turn to the second activity

  Next, when you try to start another activity in an activity, pass the data to another activity:

Intent contains: componentname, Action, data, Extras, Category, Flags, we will use extra for data transfer

(1) This call in the main activity

            New Intent ();            Intent.putextra ("Testintent", "Ccyy");            Intent.setclass (mainactivity. this, otheractivity. class );            Mainactivity. this. startactivity (intent);    

(2) The receiving end is like this

    protected void onCreate (Bundle savedinstancestate)     {        //  TODO auto-generated method stub        Super . OnCreate (savedinstancestate);        Setcontentview (r.layout.othery);                 = getintent ();         = Intent.getstringextra ("testintent");                         = (TextView) Findviewbyid (r.id.mytest);        Mytext.settext ("Hello activity" + myvalue);    }

  Of course, intent is not only able to pass the data in the same application without activity, but also can make different application calls and data passing:

The following URI calls the text messaging interface to explain, the code is added to the corresponding button just now:

            Uri uri = uri.parse ("smsto:13008574656");             New Intent (Intent.action_sendto, URI);            Intent.putextra ("Sms_body", "Hi,i am a student~");            Mainactivity. this. startactivity (intent);

After running, click the button to enter the text messaging interface:

  

  

The main understanding of this study:

1. Multiple activity

The basic role of 2.Intent

3. Start another activity in one activity

4. Use intent to pass data between activity

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.