设计界面如下图如示,在编辑框中只接受电话号码,实现“拨打电话”和“发送短信”的功能

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.xuexi.TestActivity2"
11     android:background="#000"
12     android:orientation="vertical">
13 
14     <TextView
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="请输入电话号码"
18         android:textSize="20sp"
19         android:textColor="#fff"
20         android:id="@+id/editText" />
21     <EditText
22         android:layout_width="match_parent"
23         android:layout_height="40dp"
24         android:background="#fff"
25         android:id="@+id/et_phone"
26         android:inputType="phone"/>
27 
28     <LinearLayout
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content">
31 
32             <Button
33                 android:layout_width="0dp"
34                 android:layout_height="wrap_content"
35                 android:layout_weight="1"
36                 android:layout_marginLeft="16dp"
37                 android:text="拨出此号码"
38                 android:onClick="bt1_OnClick"
39                 />
40 
41             <Button
42                 android:layout_width="0dp"
43                 android:layout_height="wrap_content"
44                 android:layout_weight="1"
45                 android:layout_marginRight="16dp"
46                 android:text="向此号码发短信"
47                 android:onClick="bt2_OnClick"/>
48 
49     </LinearLayout>
50 
51 </LinearLayout>
.xml
  1 package com.example.wang.xuexi;
  2 
  3 import android.content.Intent;
  4 import android.net.Uri;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.os.Bundle;
  7 import android.view.View;
  8 import android.widget.EditText;
  9 import android.widget.Toast;
 10 
 11 public class TestActivity2 extends AppCompatActivity {
 12 
 13     EditText et_phone;
 14 
 15 
 16     @Override
 17     protected void onCreate(Bundle savedInstanceState) {
 18         super.onCreate(savedInstanceState);
 19         setContentView(R.layout.activity_test2);
 20 
 21         et_phone=(EditText)findViewById(R.id.et_phone);
 22 
 23         Intent intent =getIntent();
 24 
 25 
 26     }
 27 
 28     //获取电话号码及验证
 29     public String getphone()
 30     {
 31         String phone = et_phone.getText().toString().trim();
 32 
 33         if (phone.length() == 0 || phone.length() != 11)
 34         {
 35             Toast.makeText(TestActivity2.this, "请填写正确电话号码", Toast.LENGTH_SHORT).show();
 36 
 37             return null;
 38 
 39         } else
 40         {
 41             return phone;
 42         }
 43     }
 44 
 45     //直接拨打电话
 46 //    public void bt1_OnClick(View v)
 47 //    {
 48 //
 49 //        String phone=getphone();
 50 //
 51 //        if(phone==null) return;
 52 //
 53 //        Intent intent=new Intent(Intent.ACTION_CALL);
 54 //
 55 //        Uri uri=Uri.parse("tel:"+phone);
 56 //
 57 //        intent.setData(uri);
 58 //
 59 //        try {
 60 //
 61 //            startActivity(intent);
 62 //
 63 //        }
 64 //        catch (Exception e)
 65 //        {
 66 //            e.printStackTrace();
 67 //        }
 68 //
 69 //
 70 //    }
 71 
 72     //调出拨打电话界面
 73     public void bt1_OnClick(View v)
 74     {
 75         String phone=getphone();
 76 
 77         if(phone==null) return;
 78 
 79         Intent intent=new Intent(Intent.ACTION_DIAL);
 80 
 81         Uri uri=Uri.parse("tel:"+phone);
 82 
 83         intent.setData(uri);
 84 
 85         startActivity(intent);
 86     }
 87 
 88     //打开发送短信页面
 89     public void bt2_OnClick(View v)
 90     {
 91         String phone=getphone();
 92 
 93         if(phone==null) return;
 94 
 95         Intent intent=new Intent(Intent.ACTION_VIEW);
 96 
 97         Uri uri=Uri.parse("sms:"+phone);
 98 
 99         intent.setData(uri);
100 
101         startActivity(intent);
102     }
103 
104 }
.java

 

posted on 2016-05-13 23:22  安然罒  阅读(561)  评论(0编辑  收藏  举报