Android电话拨号器案例

来源:互联网 发布:关注淘宝店图片 编辑:程序博客网 时间:2024/05/22 08:14

Android电话拨号器案例:


项目最终样式:





首先 我们我在eclipse中创建一个AndroidApplicationProject项目 项目创建完毕后,我们需要对我们的项目进行布局.

对于布局来说, 我们这可以采用线性布局(LinearLayout)  然后我们需要注意的一点就是我们布局的方向问题 也就是xml文件中orientation中的属性应该是垂直排列的(vertical)

代码如下:

 OK!

接下来我们就是插入的我们的控件了!上面的样式中我们看到基本的控件有三个.

(1)TextView

(2)EditText

(3)Button

因此我们的布局文件代码如下:

<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"    android:orientation="vertical"    tools:context=".MainActivity" ><!--我们应该养成一个好习惯 我的叫法是"归类" 比如说我们text属性值我们应该将该值存放到对应的目录下面@代表的是引用该文件中的数据  在我们的res 下 valuse 下 string.xml中,这里面存放的是字符的信息 -->    <TextView         android:id="@+id/main_tv_tag"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/main_tv"        />    <EditText         android:id="@+id/main_et_phonenumber"
<span style="white-space:pre"></span>android:inputType="number"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />    <Button         android:id="@+id/callphone"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/main_but_call"        /></LinearLayout>
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html"><span style="font-size:24px;"><strong>布局完成了!是不是很简单呢!(*</strong></span>
<span style="font-size:24px;"><strong>那么我接下就该在我们的代码中去实现拨打电话的功能了</strong></span>
<span style="font-size:24px;"><strong>Let's Go!</strong></span>

package com.example.callphone;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.InputType;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {// 将我们的控件定义成全局变量 方便使用private EditText phoneNumber;private Button callPhone;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                // 我们要初始化控件                phoneNumber = (EditText) findViewById(R.id.main_et_phonenumber);        callPhone = (Button) findViewById(R.id.callphone);                        // 首先我们应该明确一点 当我们输入我们的需要拨打的手机号时 输入完成之后 点击按钮就开始执行拨打的功能了        // 因此我们需要对该按钮进行监听 就是说这个按钮有没有单击过                // 这里是我们使用的是内部匿名对象 其实对于按钮的单击事件 我们可以有四种方法 这个我们以后细说        callPhone.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 获取输入框中内容String number = phoneNumber.getText().toString();// 判断一下 万一用户输入的是空值呢if(TextUtils.isEmpty(number)) {// 给个提示Toast.makeText(MainActivity.this, "手机号不能为空!", Toast.LENGTH_LONG).show();return; // 如果输入为空我们就不向下执行了} else {// 那就开始拨打电话呗Intent intent = new Intent(); // 意图intent.setAction(intent.ACTION_CALL);intent.setData(Uri.parse("tel:" + number));startActivity(intent);}}});            }      }


别忘记打电话的权限:  android.permission.CALL_PHONE


1 0
原创粉丝点击