Android Hands-on tutorial The third simple implementation call function _android

Source: Internet
Author: User

The example of this article for you to share the Android phone function of the implementation code, need a text input box input number, need a button to call.

Essence: Click on the button to invoke the system call function.

XML layout file code::

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 xmlns:tools= "http:// Schemas.android.com/tools " 
 android:layout_width=" match_parent " 
 android:layout_height=" Match_parent " 
 tools:context= ". Mainactivity " 
 android:orientation=" vertical " 
 > 
 
 <textview 
  android:layout_width=" Wrap_ Content " 
  android:layout_height=" wrap_content " 
  android:text=" Please enter the number/> 
 <edittext 
  Android:id= "@+id/et_phone" 
  android:layout_width= "match_parent" 
  android:layout_height= "Wrap_content" 
  /> 
 <button 
  android:id= "@+id/bt_call" 
  android:layout_width= "Wrap_content" 
  android:layout_height= "Wrap_content" 
  android:text= "dial" 
  /> 
 

Code in Mainactivity:

Package Com.ydl.dialer; 
Import Android.net.Uri; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
 
Import Android.widget.EditText; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super 
  . OnCreate (Savedinstancestate); 
   
  Setcontentview (R.layout.activity_main); 
  Click on the button to listen for//1. Get the button object buttons bt = (Button) Findviewbyid (r.id.bt_call);//button class is a subclass of view. 
 2. Set listening Bt.setonclicklistener (new MyListener ()); Class MyListener implements onclicklistener{//button is clicked, this method calls @Override public void OnClick (View v) {/ 
   /Get the user input number edittext et = (edittext) Findviewbyid (R.id.et_phone); 
    
   String phone = Et.gettext (). toString (); 
   We need to tell the system our actions: I want to call//Create Intent object Intent Intent = new Intent (); Encapsulate the call action Action_call to the intent object IntenT.setaction (Intent.action_call); Set Who to call Intent.setdata (Uri.parse ("Tel:" + phone));//This Tel: must be added, means I have to call. 
   Otherwise there will be no call function, because in the call list file set up the "Protocol"//action to tell the system, start the system call function. 
  StartActivity (Intent);  } 
   
 } 
  
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.