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

猜你喜欢

转载自hmeng.iteye.com/blog/2124691