Android self-study notes-4-simple phone dial

Source: Internet
Author: User

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

Related Article

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.