Android Learning 3-First knowledge activity

Source: Internet
Author: User

Activity---The container for the control under the Android system. Popular speaking is equivalent to the Cframe in MFC. With the completion of the android4.4.2 development environment, it is found that the development efficiency is much higher than 2.1.

Android 2.1 relies on XML to add and articulate controls, but 4.4.2 provides visual editing tools that speed up development.


We add a button and the command response to the button is added in Mainactivty.java.

1. How do I get controls? We looked up the activity's help file and found the Findviewbyid function. Index the control through the resource ID.

2. People who have learned Java know how to add listener inner classes to implement the internal response of a button

3, button set monitoring. The associated internal listener class.

The specific code is as follows

Package Com.example.myactivity;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.os.Bundle;import Android.view.layoutinflater;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.os.build;public class Mainactivity extends actionbaractivity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button MyButton = (button) Findviewbyid (R.id.button1); Mybutton.setonclicklistener (new Mybuttonlistener ()); if ( Savedinstancestate = = null) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container, new Placeholderfragment ()). commit ();}} Class Mybuttonlistener implements onclicklistener{@Overridepublic void OnClick (View v) {//TODO auto-generated method Stu BButton MyButton = (Button)Findviewbyid (R.id.button1); Mybutton.settext ("Helloyyxhhx");}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} /** * A placeholder fragment containing a simple view. */public Static class Placeholderfragment extends Fragment {public placeholderfragment () {} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View RootView = inflater.inflate (R.layout.fragment_main, Container,false); return Rootview;}}}
The program initial interface is as follows

Click the button


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.