Android实现键盘拨号

1.创建一个Android工程,整体的目录如下图所示:



2.新建一个activity,取名为activity_title,activity_title.xml的代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="#545454"
        android:text="back"
        android:textColor="#fff"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="拨号键盘"
        android:textColor="#fff"
        android:textSize="24sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="#545454"
        android:text="edit"
        android:textColor="#fff"
        android:textSize="24sp" />

</LinearLayout>

3.activity_main.xml里面的代码如下所示:

<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" >

    <include layout="@layout/activity_title" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/tv_text1"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:text=""
            android:textColor="#000000"
            android:textSize="50dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:gravity="center"
            android:text=""
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_delete"
            android:layout_width="54dp"
            android:layout_height="wrap_content"
            android:background="#545454"
            android:onClick="deleteClick"
            android:text="del"
            android:textColor="#fff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_five"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_six"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_seven"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_eight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_nine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_star"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:text="*"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="0"
            android:textSize="20sp" />

        <Button
            android:id="@+id/bt_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="#"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_function"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="功能" />

        <Button
            android:id="@+id/bt_call"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="拨号" />

        <Button
            android:id="@+id/bt_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="联系人" />
    </LinearLayout>

</LinearLayout>

4.MainActivity.java的代码如下所示:

package edu.cn.call;

import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

	private TextView tv_text1;
	private TextView tv_text2;
	private Button bt_delete;
	private Button bt_zero;
	private Button bt_one;
	private Button bt_two;
	private Button bt_three;
	private Button bt_four;
	private Button bt_five;
	private Button bt_six;
	private Button bt_seven;
	private Button bt_eight;
	private Button bt_nine;
	private Button bt_star;
	private Button bt_bottom;
	private Button bt_call;
	private Button bt_all;

	private TextView tempText;
	private String all = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tv_text1 = (TextView) findViewById(R.id.tv_text1);
		tv_text2 = (TextView) findViewById(R.id.tv_text2);
		bt_delete = (Button) findViewById(R.id.bt_delete);
		bt_zero = (Button) findViewById(R.id.bt_zero);
		bt_one = (Button) findViewById(R.id.bt_one);
		bt_two = (Button) findViewById(R.id.bt_two);
		bt_three = (Button) findViewById(R.id.bt_three);
		bt_four = (Button) findViewById(R.id.bt_four);
		bt_five = (Button) findViewById(R.id.bt_five);
		bt_six = (Button) findViewById(R.id.bt_six);
		bt_seven = (Button) findViewById(R.id.bt_seven);
		bt_eight = (Button) findViewById(R.id.bt_eight);
		bt_nine = (Button) findViewById(R.id.bt_nine);
		bt_star = (Button) findViewById(R.id.bt_star);
		bt_bottom = (Button) findViewById(R.id.bt_bottom);
		bt_call = (Button) findViewById(R.id.bt_call);
		bt_all = (Button) findViewById(R.id.bt_all);

		// 由于我的API版本太低了,所以没有办法执行这个代码
		// ActionBar actionBar = getSupportActionBar();
		// if (actionBar != null) {
		// actionBar.hide();
		// }

		bt_zero.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_zero);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_one.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_one);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_two.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_two);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_three.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_three);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_four.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_four);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_five.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_five);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_six.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_six);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_seven.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_seven);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_eight.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_eight);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_nine.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_nine);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_star.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_star);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_bottom.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_bottom);
				all += tempText.getText().toString();
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

		bt_call.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				tempText = (TextView) findViewById(R.id.bt_call);
				Intent intent = new Intent(Intent.ACTION_DIAL);
				intent.setData(Uri
						.parse("tel:" + tempText.getText().toString()));
				startActivity(intent);
			}
		});

		bt_all.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				intent.setData(Contacts.People.CONTENT_URI);
				startActivity(intent);
			}
		});

		bt_delete.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				all = all.substring(0, all.length() - 1);
				tv_text1.setText(all);
				tv_text2.setText(all);
			}
		});

	}
}



5.我的AndroidManifest.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.cn.call"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="edu.cn.call.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>

6.运行能够实现拨号功能,代码下载地址:http://download.csdn.net/download/qq_29656961/9979275。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值