android 实现拨号功能,让用户选择是否拨号,不自动拨号

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/activity_main"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7     <EditText
 8         android:id="@+id/phone"
 9         android:layout_width="match_parent"
10         android:layout_height="25dp"
11         android:hint="请输入手机号" />
12     <Button
13         android:id="@+id/cal"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:onClick="onClick"
17         android:text="拨号"/>
18 </LinearLayout>

MainActivity.java

 1 package com.example.teletephone;
 2 import android.content.Intent;
 3 import android.net.Uri;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
 9     private EditText phone;
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         phone= (EditText) findViewById(R.id.phone);
15     }
16     @Override
17     public void onClick(View v) {
18         switch (v.getId()){
19             case R.id.cal:
20                 Intent intent = new Intent(Intent.ACTION_DIAL);
21                 intent.setData(Uri.parse("tel:"+phone.getText()));
22                 startActivity(intent);//内部类
23                 break;
24         }
25     }
26 }

 

posted @ 2016-12-15 14:26  疯子~  阅读(830)  评论(0编辑  收藏  举报