1. First layout interface, the interface is a linear vertical way to layout
Activity_main.xml in the Layout interface file
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Vertical" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/mobile"/> <EditText android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "@null"Android:id= "@+id/mobile"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button"Android:id= "@+id/button"//This is the ID of the import button, which is automatically generated in the R.java/></linearlayout>
2. In the mainactivity file,
PackageCom.example.phone;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Register button event when window contents are displayedButton button = (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewButtonclicklistener ()); } //Defining internal Events Private Final classButtonclicklistenerImplementsview.onclicklistener{@Override Public voidOnClick (View v) {//first get the Input box object, then take the input valueEditText Mobiletext =(EditText) Findviewbyid (r.id.mobile); String Number=Mobiletext.gettext (). toString (); //Define an Intent object, specify the behavior and data, call the Android dialing functionIntent Intent =NewIntent (); Intent.setaction ("Android.intent.action.CALL"); //intent.addcategory ("Android.intent.category.DEFAULT");//The method automatically adds categories to the intent, so you do not have to addIntent.setdata (Uri.parse ("Tel:" +Number )); //Here the phone number should be in URI format startactivity (intent); //The method automatically adds a category to the intent, so comment out the Android.intent.category.DEFAULT//Note here that this is called, and you need to declare the dialing permissions in the configuration file.//Declare in Androidmanifest.xml//<!--here to import permissions, otherwise you can't call--//<uses-permission android:name= "Android.permission.CALL_PHONE"/> } } }
3. Declaring permissions in Androidmanifest.xml
<uses-permission android:name= "Android.permission.CALL_PHONE"/>
This dialer, after experiment, installed on the phone, you can call directly, good!!!
Java Learning Lesson 11th, Android to make phone calls function