Android Learning four---activity and intent

Source: Internet
Author: User

1.android project resources in-depth understanding

In-depth study of Android, the first to play a good game on the mobile phone application, most programs have an icon, click the icon, program start, a certain time, the program will jump to the first interface, such as mobile phone QQ, click on the icon, will jump out of a picture, and then jump to the QQ landing interface (such as). This interface has input box input QQ number, password, there is a login button, remember the password selection box, there are some icons and text. If you enter the password and account, click on the login, the program will respond. Send to the server side will check the account number, password. If the account password is the same, it will jump to the user interface.

Suppose you need to implement such an interface, what resources are needed for the corresponding Android project? First, you need to place an initial display image in the drawable in the Res resource folder, and the string resource on the various buttons is placed in the string in value. Then the login interface is an activity, which can respond to various buttons, text boxes, or jump to another activity, the code of the listener or response is defined in SRC Java source code. and the layout file for this interface is in the XML file. Added a resource that will automatically generate an index in Gen R.java.

2. A simple Android Project 2.1 effect

Click Send in Activity1 to jump to Activity2 and then click Back to return to Activity1.

2.2 Ideas

First of all to build two interfaces, each interface is activity, we define interface one activity is activity1, define interface two activity is Activity2. Create a layout for interface one or two, respectively. The program is mainly to solve two problems, (1) How to know the key is pressed down (2) How to communicate between two activity.

For the first question, get the button object in the layout, then call the object's Setonclicklistener to listen, and when the Listener event occurs (press the key), the OnClick method is executed.

For the second problem, we need to use a class of Android intent to achieve, intent as the name implies that the program wants to express the intention, how to express this intention? Actions and specific data that need to be implemented. The role of intent here is primarily to assist in the completion of communication between the various components of Android. For example, call StartActivity () to initiate an activity, or broadcaseintent () to all interested broadcasereceiver, or by StartService ()/ Bindservice () to start a service in the background.

2.3 Implementation

Create a new blank activity and define a button in the generated. xml file.

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.activitiy1.MainActivity" > <Button Android:id= "@+id/buttonsend"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button_send"Android:onclick= "SendMessage"/></relativelayout>

android:id= "@+id/buttonsend" for the ID of the button, the first time to add the resource needs plus + number, the name can be arbitrarily taken, it is best to take meaningful good east bank.

Android:layout_width: Defines the width of the current view on the screen, fill_parent/match_parent fills the entire screen.
Android:layout_height: Defines the height that the current view occupies on the screen, fill_parent/match_parent fills the entire screen.

Wrap_content: Changes the width or height of the view as the size of the text varies.

android:text= "@string/button_send": Refers to Strings in string, where the strings need to be defined in the String.xml in values.

Defined as <string name= "Button_send" >send</string>. Two <><> strings are displayed in the interface and support Chinese.

Also click New–other to create a Activity2

Fill in the Activity name and layout name, and the names used in this project are Activity2 and Main_activity2 respectively.

Also create interface Main_activity2.xml code

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.activitiy1.Activity2" > <Button Android:id= "@+id/buttonback"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button_back"Android:onclick= "SendMessage"/></relativelayout>

After the interface is ready, you need to implement the corresponding function in the SRC source code. Mainly in the OnCreate, listen to the button, press the button, create a new intent, and then use this intent to start another activity. ‘

The code for Mainactivity.java and Activity2 is as follows

 Packagecom.example.activitiy1;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (r.id.buttonsend); Button.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (mainactivity. This, Activity2.class);                StartActivity (Intent); Mainactivity. This. Finish ();    }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Activity2

 Packagecom.example.activitiy1;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classActivity2extendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main_activity2); Button Button=(Button) Findviewbyid (r.id.buttonback); Button.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (Activity2. This, Mainactivity.class);                StartActivity (Intent); Activity2. This. Finish ();    }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.activity2, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.activitiy1"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>" <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <Activity Android:name=". Mainactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter&        Gt </activity> <Activity Android:name=". Activity2 "Android:label= "@string/title_activity_activity2" > </activity> </application></manifest>

Android Learning four---activity and intent

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.