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.