As opposed to HelloWorld, the phone Dialer is also an introductory demo of Android, and from this example we need to figure out the idea of doing the Android project.
broadly divided into three steps:
1. Understand your needs and clarify your ideas
2. Designing the UI
3. Code Implementation
Phone Dialer
1. Understand the requirements:
* a text box--to receive a phone number
* a button--to trigger the event
2. Designing the UI
3. Code Implementation
public class Mainactivity extends Activity {private EditText et_number;//define variable to receive phone number private Button btn;//definition Butto n @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Set interface content Setcontentview (R.layout.activity_main); Find control, text input box et_number= (EditText) Findviewbyid (R.id.et_number); Locate the button control btn= (buttons) Findviewbyid (R.ID.BTN); Set the Click event Listener Btn.setonclicklistener (New MyListener ()); }//Internal class way Implementation Click event class MyListener implements onclicklistener{@Overridepublic void OnClick (View v) {//Perform dial operation//1. Get user input The number string Number=et_number.gettext (). toString ();//2. Perform a dial-up action//create a dial-up intent Intent intent=new intent ();//Set the number to dial (URL: Uniform Resource Locator, URI: Uniform Resource Identifier) Intent.setdata (Uri.parse ("tel://" +number));//Set Action, Dial action Intent.setaction (intent. Action_call);//jump to Dialer interface startactivity (intent);} }}
After the code is finished, add the call permission from the configuration file Call_phone OK.
The phone dialer above is a way to implement a click event, and it is also the first way to describe the click event :
Four Click event implementations1.Inner class mode
(see above telephone dial demo, but more introduction )
2.Create anonymous inner class mode
/** * Anonymous inner class mode implementation Click event * @author Hugh */public class Mainactivity extends Activity {private EditText et_number;private Button BTN; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Set interface content Setcontentview (r.layout.activity_main); Find control, text input box et_number= (EditText) Findviewbyid (r.id.et_number); Locate the button control btn= (buttons) Findviewbyid (R.ID.BTN); Set Click event Listener Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Perform dial Operation//1. Gets the number entered by the user string Number=et_number.gettext (). toString ();//2. Perform a dial operation//Intent Intent intent=new intent ();// Set the number to dial Intent.setdata (Uri.parse ("tel://" +number));//Set Action, Dial action Intent.setaction (intent. Action_call);//Jump to dial-up interface startactivity (intent);}});} }
3.implementing the current class object for the Onclicklistener interface
/** * The current class object that implements the Onclicklistener interface * @author Hugh */public class Mainactivity extends Activity implements Onclicklistener {PR Ivate EditText et_number;private Button btn; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);//Set interface content Setcontentview (r.layout.activity_main);//Find control, text input box Et_number = ( EditText) Findviewbyid (r.id.et_number);//Find Button control BTN = (Button) Findviewbyid (R.ID.BTN);// Set Click event Listener Btn.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//Perform a dial-up operation//1. Gets the number entered by the user, string # = Et_number.gettext (). toString ();//2. Perform a dial-up operation/ /Intent Intent Intent = new Intent ()//Set the number to be dialed Intent.setdata (Uri.parse ("tel://" + #);//Set Action, Dial action Intent.setaction ( Intent. Action_call);//Jump to dial-up interface startactivity (intent);}}
4.Add a Click event Response method to the button in the layout file, and then implement this method in your code
/message mass/res/layout/activity_main.xml under define attributes in XML android:onclick= "call" <button android:id= "@+id/btn" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignleft = "@+id/et_number" android:layout_below= "@+id/et_number" android:onclick= "call" android:text= "Press dial" />
Implement this method in your code
public class Mainactivity extends Activity {private EditText et_number;private Button btn; @Overrideprotected void Oncreat E (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Set interface content Setcontentview (R.layout.activity_main) ;//lookup control, text input box Et_number = (EditText) Findviewbyid (r.id.et_number);} public void call () {;//perform a dial-up operation//1. Gets the number entered by the user, string # = Et_number.gettext (). toString ();//2. Perform a dial operation//intent Intent Intent = New Intent ();//Set the number Intent.setdata (Uri.parse ("tel://" + numbers) to be dialed;//Set Action, Dial action Intent.setaction (Intent. Action_call);//Jump to dial-up interface startactivity (intent);}}
For Android learning, need to constantly accumulate, smashing solid foundation. This blog is a start, I want to pick up the blog, refueling!
Getting Started with Android-Phone dialer and four click events