Android Learning 3-Phone Dialer

Source: Internet
Author: User

This test mainly implements an Android phone call function

One: Interface preview

As can be seen from the diagram, this activity requires 3 controls: TextView, EditText, Button

In fact, to achieve a function to go through a few steps:

1,activity settings (i.e. interface layout)

Because this feature is relatively simple, there is only one activity, which is created by default when you create an Android project using Eclipse main_activity.xml

The code is as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

    <textview
        android:layout_width= "Fill_ Parent "
        android:layout_height=" wrap_content "
         android:text= "@string/mobile"/>
   
    <edittext
        android:id= "@+id/main_activity_phonenumber"
        android:layout_width= "Fill_parent"
         android:layout_height= "wrap_content"/>
   
    <button
        android:id= "@+id/main_activity_phonecaller"
         android:layout_width= "Fill_parent"
        android:layout_height= "Wrap_content"
        android:text= "dial"/>

</LinearLayout>

For an explanation of this section of code:

First, the layout of this activity is linear: linearlayout, do not forget the Orientation property is vertical when setting linearlayout

About the TextView control, because it provides only one display function in this example, so you don't have to set its ID property. Each control must have its two properties set: Android:layout_width and Android:layout_height, there are two main values for these two properties: Fill_parent and Wrap_content. Fill_parent indicates that the width (or height) property of this control is filled with its parent window, wrap_content indicates that the height (or width) of the control is enclosing its contents, that is, the size of the control will change depending on the contents of the control.

TextView Last property: Android:text is used to display the text content on the control.

Here you may wonder what @string/mobile and @+id/main_activity_phonenumber mean, @string/indicates that the variable following the backslash is a string type whose name is mobile (the key value pair of the string is in the res/ Configuration in Values/string.xml). While @+id/means that the variable following the backslash is the newly added content, the variable name is automatically generated and assigned in the R.java file, so the ID of the control can then be obtained by r.id.+ the variable name in Mainactivity.java.

There is also a property in the control that is important, that is, android:layout_weight, and I'll tell you about it in the next blog, because we don't use it here.

2.MainActivity Code writing (if your app uses multiple activity, i.e. you need to create an activity yourself, don't forget to add activity registrations to the Androidmainifest.xml file)

The code is as follows:

Package Com.example.phonecaller;

Import android.support.v7.app.ActionBarActivity;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;

public class Mainactivity extends Actionbaractivity {
Private button button;
Private EditText EditText;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Button = (button) Findviewbyid (R.id.main_activity_phonecaller);
EditText = (editText) Findviewbyid (R.id.main_activity_phonenumber);
Button.setonclicklistener (New Buttonclicklistener ());
}

Public final class Buttonclicklistener implements View.onclicklistener
{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
String PhoneNumber = Edittext.gettext (). toString (). Trim ();
Intent phonecallintent = new Intent ();
Phonecallintent.setaction (Intent.action_call);
Phonecallintent.setdata (Uri.parse ("Tel:" + phonenumber));
StartActivity (phonecallintent);
}
}
}

Explanation of this code:

The code in front of this code does not have to wonder, when you use eclipse for development, whenever you use the classes in some packages, pressing Ctrl+1,eclipse will automatically fill the package that needs to be loaded (without deliberately learning the Eclipse shortcut, in the process of code writing, You will gradually become familiar with and master these shortcuts.

Mainactivity inherited from Actionbaractivity, probably because my version of Eclipse is newer, many older versions will inherit from activity when they create mainactivity, and then I look at the code, Actionbaractivity inherits from activity, so here your mainactivity can be whatever you inherit. After inheriting actionbaractivity, implement its OnCreate method and implement the code function inside the method.

First, the control is acquired by Findviewbyid (), Button.setonclicklistener (New Buttonclicklistener ()), which means adding a click event for this button. The parameter of this method is an object that implements the View.onclicklistener interface and implements the OnClick () method of this interface in the Buttonclicklistener class.

In the OnClick method, first get the phone number in EditText (the trim () method is to remove the space on either side of the string), and then create a intent function to call the system. The Phonecallintent.setaction method indicates that the purpose of this intent is to action_call, namely, to call. The SetData method passes the dialed phone number to intent, because the SetData method receives a URI for the property that is received, so the parse method of the URI is used here to convert the string to a URI. The system specifies the telephone format for the intent to be transmitted to the calling Tel:+phonenumber. After setting intent, call the StartActivity () method to start this intent.

Note: Given the neatness of code writing, I did not use an anonymous inner class when I added a click event to the button control. Instead of rewriting a class to view.onclicklistener the interface, I saw a lot of tutorials using anonymous inner classes to write code, but see a tutorial in this way to write code, think that this really makes the code neat a lot of, so in this way, for the reader to use which way, then choose their own, each way is has its advantages and disadvantages.

Note that because system permissions are required to call the system for calls, add a user right to the app and add the following code to the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.CALL_PHONE"/>

Last Run Program:

Android Learning 3-Phone Dialer

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.