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