Android Learning Note (1)--Phone Dialer

Source: Internet
Author: User

Moving from my blog: Android Learning note (1)--Phone Dialer


The implementation of the program is very simple, broadly divided into the following steps:

    • Determine the function of the program and roughly determine the UI interface.
    • Make the interface more aesthetically pleasing by adjusting the XML file parameters.
    • Write code in the activity file, complete the corresponding event, and so on.

For telephone dialer, our final interface is as follows:

The corresponding layout file is as follows, using the relative layout.

?
123456789101112131415161718192021222324252627282930 <RelativeLayoutxmlns: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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.xgezhang.dial.MainActivity">    <EditText        android:id="@+id/et_number"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="phone">    </EditText>    <Button        android:onClick="dialButtonClicked"        android:id="@+id/bt_dial"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft= "true"        android:layout_below="@+id/et_number"        android:layout_marginTop="10dp"        android:text="@string/dial"         /></RelativeLayout>

Next we write the corresponding Java code, call this process, the first is to extract the value inside the Et_number, click the button to the Et_number number to make a call. Button Events We can do this by registering the inner class to complete its interface. Note that the onclicklistener is called here; if it is under Android.view.View, it will go wrong.

?
12345678910111213141516171819202122232425262728293031323334353637383940 package com.xgezhang.dial;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extendsActivity implementsOnClickListener {    privateEditText et_number;    @Override    protectedvoid onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //加载布局文件        setContentView(R.layout.activity_main);        //查找按钮        Button bt_dial = (Button) findViewById(R.id.bt_dial);        //查找文本框        et_number = (EditText) MainActivity.this.findViewById(R.id.et_number);        //  1.创建一个内部类,实现OnClickListener的接口                        bt_dial.setOnClickListener(newMyListener());       }    privateclass MyListener implements OnClickListener{        /**         * 当按钮被点击时调用的方法         */        @Override        publicvoid onClick(View v) {                  callPhone();        }    }}

Next we need to implement the Callphone () function. First, we can determine whether the phone number is empty, if it is empty can give hints, Android prompt through the toast function to implement

?
12345 string number = Et_number.gette XT (). toString (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if (textutils.isempty (number)) {               toast.maketext (mainactivity. this "number cannot be empty" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; return ; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

where Toast.maketext () has three parameters, the first is the context, here is the mainactivity, the second parameter is the prompt content, the third content is the hint for the duration of duration. They are Toast.length_long and Toast.length_short respectively.

Next we are going to pass on a call intent, here through the intent to achieve:

?
1234 Intent intent = newIntent();        intent.setAction(Intent.ACTION_CALL);        intent.setData(Uri.parse("tel:"+number));        startActivity(intent);

Where Intent.action_call is an already encapsulated action, that is, dialing, after converting the phone number character into a URI, we can dial by opening the interface StartActivity (intent).

The complete Callphone () function is as follows:

?
12345678910111213 private void callPhone() {    // 意图 松耦合      String number = et_number.getText().toString();    if(TextUtils.isEmpty(number)) {        Toast.makeText(MainActivity.this,"号码不能为空", 0).show();                return;    }    Intent intent =new Intent();    intent.setAction(Intent.ACTION_CALL);    intent.setData(Uri.parse("tel:"+number));    startActivity(intent);}

But this is not the end, we often see a warning when installing apps using an Android phone, saying that the app will have some permissions on the system to indicate whether or not to allow it. The phone Dialer is the same, and it also needs to have dial-up permissions.

We opened the Androidmanifest.xml file and chose Permission->userpermission,add, a permission called Call_phone.

Then we can open two simulator, with one to another dial, the simulator directly dial the port number 5556. To see the effect:

Welcome reprint, please indicate the source.


Android Learning Note (1)--Phone Dialer

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.