Four types of onclick events in Android

Source: Internet
Author: User

Package Com.example.dailphone;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.text.TextUtils;import Android.content.intent;import Android.net.uri;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.widget.edittext;import Android.widget.toast;import Android.os.build;public class Mainactivity extends Actionbaractivity implements onclicklistener{/** Call button */private button bt_dail;/** text box when entering a phone */private EditText et_number; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);    /Get button Bt_dail = (button) Findviewbyid (R.id.bt_dail);    Get text Box Et_number = (EditText) MainActivity.this.findViewById (R.id.et_number);Add event//1 to the button.    Create an internal class definition click event//bt_dail.setonclicklistener (New Mylisenner ()); 2. New an anonymous inner class defines the Click event/* Bt_dail.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Callphone ( );}}); *///3.           Mainactivity implement Onclicklistener interface, common method, through the view of id+switch implementation of multiple events//bt_dail.setonclicklistener (this); }//4. In the layout of the file surface binding a click on the method/* * <button android:onclick= "dailbuttononclicked" android:id= "@+id/bt_dail" and roid:layout_margintop= "40DP" android:layout_marginleft= "200DP" android:layout_width= "Wrap_content" an  droid:layout_height= "Wrap_content" android:text= "@string/dail"/> */public void dailbuttononclicked (View v) {Callphone ();} Private class Mylisenner implements Onclicklistener {@Overridepublic void OnClick (View v) {callphone ();}} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.bt_dail:callphone (); break;default:break;}} Call private void Callphone () {String number = Et_nUmber.gettext (). toString (). Trim ();//The empty operation gives the "spin" hint if (textutils.isempty (number)) {Toast toast = Toast.maketext ( Mainactivity.this, "Number cannot be empty", toast.length_short); Toast.show (); return;} intention, want to do one thing (tea) Intent Intent = new Intent (); intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" + number); StartActivity (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.