Android learning Activity (1) activity and intent, androidintent

Source: Internet
Author: User

Android learning Activity (1) activity and intent, androidintent

Tool: JDK environment configuration + SDK + ADT Tool

I. Main Functions of Activity:

1. interfaces between users and Applications

2. Control container

II. Key points for creating an Activity: (in the directory of src)

1. An Activity is a class that inherits the class of android.

2. Need to be rewrittenOncreate MethodIn the first running Activity, oncreate is called.

3. Activity is a component. In Androidmanifest. xml, intent-filter is configured in which Activity is the first Activity started in android.

4. There is no content in the Activity itself. Add the necessary de components, Textview, button, and drop-down menu to the Activity. Single-choice button, multiple-choice button ....

3. layou layout File

1. android: orientation = "vertical" in LinearLayout (linear layout) // vertical

Android: layout_width = "fill_parent" // width

Android: layout_height = "fill_parent" // height

2. <TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_parent"

>

3. You can useFindviewbyid MethodFindviewbyid (R. id. Name of your app)

Public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // The Oncreate method setContentView (R. layout. activity_main); // set // obtain the control using findviewbyid TextView mytextView = (TextView) findViewById (R. id. mytextView); Button mybutton = (Button) findViewById (R. id. mybutton); mytextView. setText ("My first textview"); mybutton. setText ("My first button" + "\ n" + "line feed ");

Every programmer starts from hello world.

 

Iv. acticity and intent

(1) content:

1. Relationships between multiple activits

2. Basic functions of intent

3. Start another activity in one activity.

4. The basic method of using intent to transmit data in activity

 

(2) Relationship between multiple activities

It can be a jump between activities in the same program, or it can beDifferent applicationsActivity in

Started by intent is very important.

(3) basic functions of intent: // intent is an object, which is translated into Chinese as intent, just like a request.

1. component name // The activity to start.The component name information of the object, which component is started.

2. action // the official explanation is to specify what another activity will do.

3. data // data in the intent when this activity is passed to another activity.

4. category //

5. extras // additional information, key-value pairs, in intent

6. flags

(3) basic use of intent

Call another actcity using the startactivity method. We can store some key-value pairs in the intent and display them in another activity.

Use the button in the activity to get the button. Use the findviewbyid method to generate a private variable for the button.

Create the second activity, anotheractivity, inherit Activity, and rewrite oncreate (right-click, override/implemen in source ,)

Create the second layout file and add a textview to obtain the textview in anotheractivity. In the end, you need to set a setContentView (R. layout. other). This activity uses this layout.

To obtain the button in main. xml in Mainactivity, first design a listener and bind the listener to the button.

Class MyButtonListener implements OnClickListener // internal class r {@ Override public void onClick (View v) {// TODO Auto-generated method stub Intent intent = new Intent (); // generate an Intent object intent is equivalent to a request intent. putExtra ("testIntent", "123"); // The Name Of The key-value pair is testIntent and the value is 123 intent. setClass (MainActivity. this, anotheractivity. class); // adjust from MainActivity to anotheractivity MainActivity. this. startActivity (intent); // call the startActivity method in the current activity}

Bundled on the command button:

An exception is found after you click the button.

We found that we forgot to register the second anotheractivity in AndroidMainfest. xml, so an exception occurred. Bug

<activity android:name="anotheractivity"            android:label="@string/other"/>

 

Write in anotheractivity

// Extract the value Intent intent = getIntent (); // use the getintent method to obtain intent String value = intent in mainactivity. getStringExtra ("testIntent"); // The getStringExtra method transmits the key in mytextview = (TextView) findViewById (R. id. mytextview); mytextview. setText (value );

 

Run successfully:

The above is to jump to another activity.

The method used to pass data in two activities. The value is 123.

 

 

Use Intent to send messages

From one component to another

Text messageThis is a feature that we often use. android comes with a text message. The two activities in the two applications do not prevent intent from transmitting data.

Add a piece of code to anotheractivity. It is not the previous activity, but a text message activity.

Package com. helloworld; import android. support. v7.app. actionBarActivity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. button; import android. widget. textView; public class anotheractivity extends ActionBarActivity {private TextView mytextview = null; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. another); // obtain the value/* Intent intent = getIntent (); // getintent method String value = intent. getStringExtra ("testIntent"); mytextview = (TextView) findViewById (R. id. mytextview); mytextview. setText (value); */Uri uri = Uri. parse ("smsto: // 0800000123"); Intent intent = new Intent (Intent. ACTION_SENDTO, uri); // The ACTION_SENDTO intent carried by the operating system. putExtra ("sms_body", "The SMS Text"); startActivity (intent );}}

After running the command button:

 

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.