`
hmeng
  • 浏览: 15021 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

Android拨号器---熟练控件与布局的应用

阅读更多
    Android版的简易拨号器
实现的内容主要由界面布局设计和Activity的简单函数组成
首先是界面布局设计,最外层是垂直线性布局,分成三个部分:文本框,数字按键和功能键,在数字键部分采用网格布局,所有按钮用ImageButton。布局中控制界面比例自适应的方法在这里采用的是调节宽度或者长度的所占比重layout_weight。另外若需要使得界面去掉小标题,需要在AndroidMainfest.xml文件中在<activity里添加

 android:theme="@android:style/Theme.NoTitleBar"

并修改MainActivity所继承的类为Activity。
下面是我写的布局xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    tools:context="com.hm.mydialer.MainActivity"
    tools:ignore="MergeRootFrame" >


   <EditText
       android:id="@+id/editText1"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_gravity="right"
       android:layout_weight="2.5"
       android:background="@drawable/shape2"
       android:text=""
       android:textSize="40sp" />

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:background="@drawable/background2" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n1"
                android:tag="1" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n2"
                android:tag="2" />

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n3"
                android:tag="3" />

        </TableRow>

        <TableRow
            android:id="@+id/TableRow01"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" 
            >

            <ImageButton
                android:id="@+id/ImageButton01"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n4"
                android:tag="4" />

            <ImageButton
                android:id="@+id/ImageButton02"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n5"
                android:tag="5" />

            <ImageButton
                android:id="@+id/ImageButton03"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n6"
                android:tag="6" />

        </TableRow>

        <TableRow
            android:id="@+id/TableRow02"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/ImageButton04"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n7"
                android:tag="7" />

            <ImageButton
                android:id="@+id/ImageButton05"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n8"
                android:tag="8" />

            <ImageButton
                android:id="@+id/ImageButton06"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n9"
                android:tag="9" />

        </TableRow>

        <TableRow
            android:id="@+id/TableRow03"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" 
           >

            <ImageButton
                android:id="@+id/ImageButton07"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/star"
                android:tag="*" />

            <ImageButton
                android:id="@+id/ImageButton08"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/n0"
                android:tag="0" />

            <ImageButton
                android:id="@+id/ImageButton09"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/touch2"
                android:onClick="telNumber"
                android:src="@drawable/sharp"
                android:tag="#" />

        </TableRow>
    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/shape3"
        android:layout_weight="2" >

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/touch2"
            android:onClick="addContacts"
            android:src="@drawable/add_n" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/touch"
            android:onClick="call"
            android:src="@drawable/dialpad" />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/touch2"
            android:onClick="delete"
            android:src="@drawable/delete_n" />

    </LinearLayout>

</LinearLayout>


在MainAcvivity中包括几个重要的函数来实现其功能,数字键的监听方法,使文本框能显示所按下的数字;删除最后一位号码的方法;拨打电话的方法,此方法需要在AndroidMainfest.xml文件中添加拨号的权限:在<manifest里添加
 <uses-permission android:name="android.permission.CALL_PHONE" />

还有添加联系人的方法,播放背景音乐的功能等
public class MainActivity extends Activity {
	private EditText editNumber;
	private String number;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 关联布局
		setContentView(R.layout.activity_main);
		// 找到界面中的TextView
		editNumber = (EditText) findViewById(R.id.editText1);
		//音乐
		addMusic(this);
	
	}
	/*退出
	 */
	public void onBackPressed() {
		stopMusic();
		super.onBackPressed();
		System.gc();
	}

	/*
	 * 数字号码的监听方法
	 */
	public void telNumber(View view) {
		// 给TextView添加号码
		editNumber.append(view.getTag().toString());

	}

	/*
	 * 删除最后一位号码的方法
	 */
	public void delete(View view) {
		// 从TextView获取号码
		number = editNumber.getText().toString();
		// 字符长度大于1,则删除最后一位
		if (number.length() > 0)
			// 显示号码
			editNumber.setText(number.substring(0, number.length() - 1));
	}

	/*
	 * 拨打电话的方法
	 */
	public void call(View view) {
		// 从TextView获取号码
		number = editNumber.getText().toString();
		// 字符长度小于1,则返回
		if (number.length() > 0) {
			// 若不能通过验证
			if (!check(number)) {
				Toast.makeText(this, "请输入规范的号码!", 3000).show();
				;
				return;
			}
			// 实例化对象
			Intent callIntent = new Intent();
			// 设置拨打命令
			callIntent.setAction(Intent.ACTION_CALL);
			// 设置拨打号码
			callIntent.setData(Uri.parse("tel:" + number));
			// 开始执行命令
			startActivity(callIntent);
		} else {
			Toast.makeText(this, "请先输入号码!", 3000).show();
			;
		}
	}

	/*
	 * 添加联系人
	 */
	public void addContacts(View view) {
		// 从TextView获取号码
		number = editNumber.getText().toString();
		if (number.length() > 0) {
			// 系统界面跳转
			Intent it = new Intent(Intent.ACTION_INSERT, Uri.withAppendedPath(
					Uri.parse("content://com.android.contacts"), "contacts"));
			it.setType("vnd.android.cursor.dir/person");
			// 联系人姓名
			it.putExtra(android.provider.ContactsContract.Intents.Insert.NAME,
					"");
			// 手机号码
			it.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE,
					number);
			startActivity(it);
		}

	}

	/*
	 * 正则表达式验证手机号码
	 */
	public boolean check(String number) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher matcher = pattern.matcher(number);
		if (matcher.matches())
			return true;
		return false;

	}

	/*
	 * 音乐
	 */
	public MediaPlayer player = null;

	public void addMusic(Context context) {
		player = MediaPlayer.create(context, R.raw.l1);
		player.setLooping(true);
		player.start();
	}

	public  void stopMusic() {
		player.stop();
	}

}

界面效果图

另上传了HM拨号器apk
  • 大小: 70.2 KB
0
2
分享到:
评论

相关推荐

    Android开发应用实战详解源代码

    4.17 listactivity布局- 4.17.1 listactivity介绍 4.17.2 使用listactivity 4.18 matrix实现图片缩放 4.19 bitmap和matrix图片旋转 4.20 加载手机存储卡中的文件 4.21 小结 第5章 交互式服务 5.1 textview的花样 5.2...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...

    android基础

    2. 第一个android程序——电话拨号器 android布局和控件 LinearLayout:线性布局,从左到右 第一种方法实现界面布局:手动写布局 第二种方法实现界面布局:布局文件(layout中的activty_main.xml) 其实就是标签的...

    Android开发之电话拨号器和短信发送器实现方法

    本文实例讲述了Android开发之电话拨号器和短信发送器实现方法。分享给大家供大家参考,具体如下: 电话拨号器 实现原理:用户输入电话号码,当点击拨打的时候,由监听对象捕获,监听对象通过文本控件获取到用户输入...

    Google Android SDK开发范例大全的目录

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 –p254 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩...

    android开发实例大全_王东华

    分别讲解了UI布局实例集锦、控件实例集锦、自动化服务实例集锦、数据存储实例集锦、电话和短信实例集锦、图形图像实例集锦、和网络有关的实例集锦、多媒体实例集锦、Google地图实例集锦、GoogleAPI实例集锦、平板...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...

    Google Android SDK 开发范例大全01

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 –p254 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩...

    Google Android SDK 开发范例大全02

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 –p254 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩...

    Google Android sdk 开发范例大全 部分章节代码

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 –p254 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩...

    史上最全的ios开发源码

    指示器(HUD)之Android Style Toast 指示器-Activity Bar 指示器--Notify HUD 指示器之MBAlertView 指示器之YLActivityIndicatorView 其他类 财付通打印票据和拖动银行卡效果 寸光阴课程表 功能齐全的计算器 ...

    Google+Android+SDK开发范例大全

    5.21 任务管理器正在运行的程序——RunningTaskInfo 5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——...

    Google Android SDK开发范例大全(完整版)

    5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...

    android知识大总结【邯院】

    第三章 电话拨号器和发送短信小程序 12 第四章 日志输出和单元测试 17 第五章 数据的存储与访问 19 第六章 SD卡的存取和XML读取 24 第七章 SharedPreferences进行数据存储 38 第八章 SQLITE数据库 42 第九章 ...

Global site tag (gtag.js) - Google Analytics