您的位置:首页 > 移动开发 > Android开发

android 电话拨号器(点击事件)的四种写法

2015-01-05 10:31 274 查看
安卓电话拨号器的界面



界面布局代码

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入要拨打的电话号码" />
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et1"
        android:text="请输入号码" />
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/xiaoluoli"
        />
     <Button 
        android:onClick="click"
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="250dp"
        android:text="拨号"  
         />
</LinearLayout>


点击事件的4种写法

抽取出的callPhone方法,点击事件中会调用的方法

private void callPhone() {
	String number = et1.getText().toString().trim();
	if("".equals(number)){
		Toast.makeText(MainActivity.this, "电话号码不能为空", Toast.LENGTH_SHORT).show();
	}else{
		//TODO:拨打电话  激活系统的拨号应用 把电话号码传给系统的应用.
		Intent intent = new Intent();//意图
		intent.setAction(Intent.ACTION_CALL);//设置动作
		intent.setData(Uri.parse("tel:"+number));
		startActivity(intent);
		}

点击事件第一种写法:点击事件的监听器


bt1.setOnClickListener(new myclicklistener());
public class myclicklistener implements OnClickListener {
	public void onClick(View view) {
		callPhone();
		}
	}
第二种写法:点击事件的监听器(匿名内部类)写到oncreate()方法中。

bt1.setOnClickListener(new OnClickListener(){
	public void onClick(View view)
		{
		callPhone();
		}});

第三种写法:activity实现点击事件的借口;这里需要Activity实现点击事件接口 implements OnClickListener

bt1.setOnClickListener(this);
public void onClick(View v)
	{
	switch(v.getId())
	{
		case R.id.bt1:
		callPhone();
		break;
				
		default:
		break;
		}
	}

第四种写法: 在xml文件里面定义 button点击的时候 调用什么方法

public void click(View view)
		{
			callPhone();
		}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: