Android Control monitoring, with the interface to achieve the best listening, in the Android itself provides a variety of control monitoring interface, we just follow this implementation, it looks like the code is neat. The implementation of the effect is also very good, below I listed the common Control interface monitoring, layout, Checkbox,radiogroup, as well as the click of the ListView or long-press monitoring. See the code below for a comment. This article source code address: Click here to downloadReprint Please specify Source: http://blog.csdn.net/qq_16064871
Package Com.example.impletedemo;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.os.bundle;import Android.app.activity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemlongclicklistener;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.listview;import Android.widget.radiogroup;import Android.widget.simpleadapter;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity implements Onclicklistener,compoundbutton.onchecke dchangelistener,radiogroup.oncheckedchangelistener,onitemlongclicklistener,onitemclicklistener{@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initui ();} private void Initui () {View Layout1 = fiNdviewbyid (R.ID.LAYOUT1); CheckBox checkBox1 = (checkbox) Findviewbyid (R.id.checkbox1); CheckBox checkBox2 = (checkbox) Findviewbyid (R.ID.CHECKBOX2); Radiogroup radioGroup1 = (radiogroup) Findviewbyid (r.id.radiogroup1);//interface Implementation Monitoring, all is required to set the listening if (Layout1! = null) Layout1.setonclicklistener (this); if (checkBox1! = null) Checkbox1.setoncheckedchangelistener (( Compoundbutton.oncheckedchangelistener) this); if (checkBox2! = null) Checkbox2.setoncheckedchangelistener (( Compoundbutton.oncheckedchangelistener) this); if (radioGroup1! = null) Radiogroup1.setoncheckedchangelistener (( Radiogroup.oncheckedchangelistener) this); ListView Mylistview = (ListView) Findviewbyid (R.id.listview1); Mylistview.setonitemlongclicklistener (this); Set the ListView Long Press Mylistview.setonitemclicklistener (this); Set ListView Click simpleadapter simpleadapter = new Simpleadapter (this, Getdatasource (), R.layout.layout_list_item, new string[]{"title"}, New Int[]{r.id.textview1}); Mylistview.setadapter (Simpleadapter);} Public List<map<string,Object>> Getdatasource () {list<map<string, object>> listems = new arraylist<map<string, Object >> (); map<string, object> map = new hashmap<string, object> (); Map.put ("title", "Beijing"); Listems.add (map); map = new hashmap<string, object> (); Map.put ("title", "Shanghai"); Listems.add Map = new hashmap<string, object> (), Map.put ("title", "Guangzhou"), Listems.add (map), map = new hashmap<string, Object> (); Map.put ("title", "Nanjing"); Listems.add (map); map = new hashmap<string, object> (); Map.put ("title", "Suzhou" ); Listems.add (map); return listems;} @Override//General Layout button Listener implementation method public void OnClick (View arg0) {if (Arg0.getid () = = r.id.layout1) {TextView TextView = (Te Xtview) Findviewbyid (r.id.textview1); Textview.settext ("Layout monitoring"); Toast.maketext (this, "Layout monitoring", Toast.length_short). Show ();}} @Override//checkbox Listener Implementation method public void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {if (Arg0.getid () = = R.id.checkbox1) {if (arg1) {Toast.maketext (This, "select single 1", Toast.length_short). Show ();} else {Toast.maketext (this, "Cancel single 1", Toast.length_short). Show ();}} else if (arg0.getid () = = R.id.checkbox2) {if (arg1) {Toast.maketext (This, "Select Single 2", Toast.length_short). Show ();} else {Toa St.maketext (This, "Cancel single 2", Toast.length_short). Show ();}}} @Override//radiogroup Listener Implementation method public void OnCheckedChanged (Radiogroup arg0, int arg1) {if (Arg0.getid () = = R.id.radiogroup1) {if (arg1 = = r.id.radio1) {Toast.maketext (this, "Check male", Toast.length_short). Show (); else if (arg1 = = R.id.radio2) {Toast.maketext (this, "checked female", Toast.length_short). Show ();}} @Override//listview Long Press the Listener implementation method public Boolean Onitemlongclick (adapterview<?> arg0, View arg1, int arg2,long arg3) { Toast.maketext (This, "ListView long Press" + Getdatasource (). Get (Arg2), Toast.length_short). Show ();//Return here must be true, Otherwise it will trigger the click return true; } @Override//listview Click the Listen implementation method public void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) { Toast.maketext (This, "ListView click" + Getdatasource (). Get(arg2), Toast.length_short). Show ();}}
The XML is as follows
<linearlayout xmlns:tools= "Http://schemas.android.com/tools" xmlns:android= "http://schemas.android.com/apk/ Res/android "android:id=" @+id/linearlayout1 "android:layout_width=" match_parent "android:layout_height=" Match_par Ent "android:orientation=" vertical "> <linearlayout android:id=" @+id/layout1 "Android:layout_wid Th= "Match_parent" android:layout_height= "60DP" android:gravity= "center_vertical" android:orientation= "Vertical" > <textview android:id= "@+id/textview1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "TextView"/> </LinearLayout> <l Inearlayout android:layout_width= "match_parent" android:layout_height= "60DP" android:gravity= "Center_ Vertical "android:orientation=" vertical "> <checkbox android:id=" @+id/checkbox1 " Android:layout_width= "Wrap_conteNT "android:layout_height=" Wrap_content "android:text=" single 1 "/> <checkbox and Roid:id= "@+id/checkbox2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text= "Single 2"/> </LinearLayout> <linearlayout android:layout_width= "Match_parent" android:layout_height= "90DP" android:gravity= "center_vertical" android:orientation= "vertical" > <radiogroup android:id= "@+id/radiogroup1" android:layout_width= "Wrap_content" and roid:layout_height= "Wrap_content" > <radiobutton android:id= "@+id/radio1" a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "male"/&G T <radiobutton android:id= "@+id/radio2" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "female"/> </RadioGroup> </LinearLayout> &L T ListView android:id= "@+id/listview1" android:layout_width= "match_parent" android:layout_height= "Wrap_ Content "> </ListView></LinearLayout>
Interface
Here's a look at common listening interfaces, which, as programmers, will use code to automatically prompt and populate. Next look: You can find the interface you need.
good to this end, every time I write a blog, are self-practice, write code. So there's a demo. Welcome to the Exchange. This article source code address: Click here to downloadReprint Please specify Source: http://blog.csdn.net/qq_16064871
Android common Control Interface monitoring