Android development: the third day -- two activities

Source: Internet
Author: User

A project created in Android usually has an activity by default. Today, we learn how to create another activity based on this, and pass the value in the past through intent.

/** Key points for creating an activity * 1. an activity is a class, which must inherit from activity * 2. the oncreate Method * 3 needs to be rewritten. every activity must be in androidmainifest. configure in XML file * 4. add necessary controls for the activity **/

First, create a new class named otheractivity and rewrite the oncreate () method ,:

Create an XML layout file. You can copy the content from the activity created by the system and add a textview control to display the values passed by intent:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"               android:layout_width="fill_parent"               android:layout_height="fill_parent">    <TextView        android:id="@+id/myTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>        </LinearLayout>

Bind it to the androidmanifest. xml file:

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

The default activity contains several more lines, as shown below:

<activity            android:name=".MainActivity"            android:label="@string/title_activity_main" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

Add a button control on the previous activity and add a listener.

<Button         android:id="@+id/myButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>
// Add the button listener class mybuttonlistener implements android. view. view. onclicklistener {public void onclick (view v) {// todo auto-generated method stub // generate an intent object intent = new intent (); // intent can transmit data intent between two activities in two different applications. putextra ("testintent", "123"); intent. setclass (mainactivity. this, otheractivity. class); mainactivity. this. startactivity (intent );}}

Associate the button control with the listener above:

// The Return Value of the following function is view, and view is the parent class of all android controls. Button mybutton = (button) findviewbyid (R. id. mybutton); mybutton. settext ("first button"); mybutton. setonclicklistener (New mybuttonlistener ());

Get and display the intent value in otheractivity:

// Get intent = getintent (); string value = intent. getstringextra ("testintent"); mytextview = (textview) findviewbyid (R. id. mytextview); mytextview. settext (value );

OK!

Work hard every day. Come on!

Bytes ------------------------------------------------------------------------------------------

Author: PANG Hui

Source: http://www.cnblogs.com/pang123hui/

This article is based on the signature 2.5 mainland China license agreement. You are welcome to repost, interpret, or use it for commercial purposes. However, you must keep the signature Pang Hui (including the link) of this Article ).

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.