复习 转载自http://blog.csdn.net/hanmengaidudu/article/details/17203609
实现一个简单的电话拨号器
电话拨号器布局文件如下:
- <RelativeLayout 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" >
- <EditText
- android:id="@+id/et_number"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="phone"
- />
- <Button
- android:id="@+id/bt_dail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/et_number"
- android:text="@string/dail"/>"
- </RelativeLayout>
详细参数介绍如下:
id:用于表示这个控件;
layout_wedth:表示这个控件的宽度。共有3个参数,match_parent和fill_parent是一样的意思,表示填满父窗体,wrap_content表示包裹内容。
layout_height:表示控件的高度。
inputType:表示文本框输入的类型。phone为电话类型。
layout_below:表示这个控件位于哪个控件的下方。
text:表示这个控件上显示的类容。
效果如下图:
布局文件已经完成,那接下来就要对这些进行操作。具体如下:
- package com.example.dail;
- import android.net.Uri;
- import android.os.Bundle;
- import android.renderscript.Int2;
- import android.text.TextUtils;
- import android.app.Activity;
- import android.content.Intent;
- 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 implements OnClickListener {
- private Button bt_dail = null;
- private EditText et_number = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- bt_dail = (Button) findViewById(R.id.bt_dail);
- et_number = (EditText) findViewById(R.id.et_number);
- bt_dail.setOnClickListener(new MyListener());
- }
- private void callPhone() {
- String number = et_number.getText().toString();
- if(TextUtils.isEmpty(number)) {
- Toast.makeText(MainActivity.this, "号码不能为空", 1).show();
- }
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_CALL);
- intent.setData(Uri.parse("tel:"+number));
- startActivity(intent);
- }
- private class MyListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- callPhone();
- }
- }
- }
首先,通过findViewById获取到对应的控件,然后就是对拨号按钮做监听,当按下拨号按钮的话,会调用MyListener这个监听器。
基本的已经完成,但是现在要拨打电话的话,还需要加上CALL_PHONE权限,要不然会出现进程无法运行的错误。如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.dail"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <!-- 电话服务权限 -->
- <uses-permission android:name="android.permission.CALL_PHONE"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.dail.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
如果没有输入号码,就点击拨号的话就会如下图所示: