Android first line code -2.activity basic usage

Source: Internet
Author: User

Summary:

This section is mainly concerned with activity creation, title bar hiding, button binding method (Toast usage), menu usage, activity destruction

1.activity Creation and Registration

Create:

public class Testactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }}

Registered:

        <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >            <ActivityAndroid:name=". Testactivity "Android:label= "Testactivity" >                <Intent-filter>                    <ActionAndroid:name= "Android.intent.action.MAIN" />                    <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter>            </Activity>        </Application>

2. Hide the title bar

The interface is called in the OnCreate in testactivity

 Public class extends Activity {    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Requestwindowfeature (window.feature_no_title);        Setcontentview (R.layout.activity_main);    }}

3. Bind a button to a method (toast as an example)

(1) Create button

New button in Activity_main.xml (layout)

    <button        android:id= "@+id/button_1"        android:layout_width= "Match_parent"         Android:layout_height= "Wrap_content"        android:text= "Alex"        />

(2) Bind method by ID

 Public classTestactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_main); FinalButton button_test =(Button) Findviewbyid (r.id.button_1); Button_test.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (testactivity. This, "You click Test button", Toast.length_short). Show ();    }        }); }}

3.menu use

(1). Add Menu

The default corresponding menu in Res/menu, here is Menu_main.xml, here add two options

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >    <ItemAndroid:id= "@+id/add_item"Android:title= "Add"/>    <ItemAndroid:id= "@+id/remove_item"Android:title= "Remove"/></Menu>

(2) rewrite the Oncreateoptionsmenu to allow the menu bar to show up

    @Override Public    Boolean oncreateoptionsmenu (Menu menu) {        getmenuinflater (). Inflate (R.menu.menu_main, menu);        return true;    }

(3) Add a response event to the menu bar

    @Override Public    boolean onoptionsitemselected (MenuItem menu) {        switch (Menu.getitemid ()) {case            R.id.add _item:                Toast.maketext (This, "You clicked Add", Toast.length_short). Show ();                break;            Case R.id.remove_item:                toast.maketext (This, "You clicked Remove", Toast.length_short). Show ();                break;            Default:        }        return true;    }

4. Activity Destruction

The destruction activity is relatively simple, and binding an action is OK.

Button1.setonclicklistener (new  Onclicklistener () {        @Override        public void OnClick (View v) {            finish ();}});

Android first line code -2.activity basic usage

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.