The design interface, as shown, only accepts phone numbers in the edit box, enabling "Call" and "Send SMS" functions
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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"Ten Tools:context= "Com.example.wang.xuexi.TestActivity2" One Android:background= "#000" A android:orientation= "vertical"> - - <TextView the Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "Please enter phone number" - android:textsize= "20SP" + Android:textcolor= "#fff" - Android:id= "@+id/edittext" /> + <EditText A Android:layout_width= "Match_parent" at Android:layout_height= "40DP" - Android:background= "#fff" - Android:id= "@+id/et_phone" - Android:inputtype= "Phone"/> - - <LinearLayout in Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content"> to + <Button - Android:layout_width= "0DP" the Android:layout_height= "Wrap_content" * Android:layout_weight= "1" $ Android:layout_marginleft= "16DP"Panax Notoginseng Android:text= "Dial out this number" - Android:onclick= "Bt1_onclick" the /> + A <Button the Android:layout_width= "0DP" + Android:layout_height= "Wrap_content" - Android:layout_weight= "1" $ Android:layout_marginright= "16DP" $ Android:text= "Send SMS to this number" - Android:onclick= "Bt2_onclick"/> - the </LinearLayout> - Wuyi </LinearLayout>
. XML
1 PackageCom.example.wang.xuexi;2 3 Importandroid.content.Intent;4 ImportAndroid.net.Uri;5 Importandroid.support.v7.app.AppCompatActivity;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.View;8 ImportAndroid.widget.EditText;9 ImportAndroid.widget.Toast;Ten One Public classTestActivity2extendsappcompatactivity { A - EditText Et_phone; - the - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_test2); - +Et_phone=(EditText) Findviewbyid (r.id.et_phone); A atIntent Intent =getintent (); - - - } - - //get phone number and verification in PublicString Getphone () - { toString phone =Et_phone.gettext (). toString (). Trim (); + - if(phone.length () = = 0 | | phone.length ()! = 11) the { *Toast.maketext (TestActivity2. This, "Please fill in the correct phone number", Toast.length_short). Show (); $ Panax Notoginseng return NULL; - the}Else + { A returnphone; the } + } - $ //Direct Phone calls $ //Public void Bt1_onclick (View v) - // { - // the //String Phone=getphone (); - //Wuyi //if (phone==null) return; the // - //Intent intent=new Intent (intent.action_call); Wu // - //Uri Uri=uri.parse ("Tel:" +phone); About // $ //Intent.setdata (URI); - // - //try { - // A //startactivity (intent); + // the // } - //catch (Exception e) $ // { the //e.printstacktrace (); the // } the // the // - // } in the //bring up the call screen the Public voidBt1_onclick (View v) About { theString phone=Getphone (); the the if(phone==NULL)return; + -Intent intent=NewIntent (intent.action_dial); the BayiUri Uri=uri.parse ("Tel:" +phone); the the Intent.setdata (URI); - - startactivity (intent); the } the the //Open the Send SMS page the Public voidBt2_onclick (View v) - { theString phone=Getphone (); the the if(phone==NULL)return;94 theIntent intent=NewIntent (intent.action_view); the theUri uri=uri.parse ("SMS:" +phone);98 About Intent.setdata (URI); - 101 startactivity (intent);102 }103 104}
. Java
Call and send SMS functions