安卓电话拨号器的界面
界面布局代码
<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();
}