Enter a phone number in the text box and click "call. (The correctness of the number is not verified in the small example)
1. Create an android project named "Phone Dialer. Because the new version of ADT supports Chinese projects, you can use Chinese directly. The basic project directory is as follows:
2. Develop the dialing interface.
Open the res/layout/activity_main.xml file of the project, and a visual interface tool will appear on the right. All the interface la s in android are written in xml. Although you can drag the corresponding control directly, the xml file is generated at the end. The simple layout is as follows:
3. write code, process click events, and call.
First, find the corresponding button object, add a click event, get the phone number when you click, and call the phone. The Code is as follows:
Package com. mxy. dail; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {// defines the variable private Button btnDail; private EditText phoneNumber; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // use findViewById to obtain the buttons and text box objects and forcibly convert them to btnDail = (Button) findViewById (R. id. btn_dail); // Add the listener event btnDail for the button. setOnClickListener (new MyListener (); phoneNumber = (EditText) findViewById (R. id. number);} private class MyListener implements OnClickListener {@ Overridepublic void onClick (View v) {// obtain the input phone number EditText phoneNumber = (EditText) MainActivity. this. findViewById (R. id. number); String number = phoneNumber. getText (). toString (); // verify whether the phone number is available // What do You want Intent intent = new Intent (); // What Kind Of intent do you want. setAction (Intent. ACTION_CALL); intent. setData (Uri. parse ("tel:" + number); // use this function in AndroidManifest. add the corresponding permissions to the xml file android. permission. CALL_PHONEstartActivity (intent );}}}
4. Add the corresponding permissions.
Add the dialing permission to the AndroidManifest. xml file. If you do not add this permission, the program will crash and add the following code:
5. Run
Right-click the project, choose Run As --> Android Application, and select the device to Run and test.
Engineering: http://pan.baidu.com/s/1gdwuybT