Layout:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height= "Match_parent" tools:context= "com.example.msgSend.MainActivity" tools:ignore= "Mergerootframe" > <textview Android:id= "@+id/tv_pleaseinputphonenum" android:layout_width= "wrap_content" android:layout_height= "WR Ap_content "android:text=" @string/pleaseinputphonenum "/> <edittext android:id=" @+id/et_phonenum " Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentlef T= "true" android:layout_below= "@+id/tv_pleaseinputphonenum" android:layout_margintop= "14DP" android:e ms= "android:inputtype=" Phone "/> <textview android:id=" @+id/et_pleaseinputcontent "Andro Id:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_below= "@+id/et_phoneNum" android:layout_margintop= "16DP" android:text= "@string/pleaseinputcontent"/> <edittext Andro Id:id= "@+id/et_content" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android oid:layout_below= "@+id/et_pleaseinputcontent" android:layout_margintop= "26DP" android:layout_marginright= "10 DP "android:layout_marginleft=" 10DP "android:ems=" android:lines= "5" > </EditText> <button android:id= "@+id/bt_send" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:layout_alignleft=" @+id/et_content "android:layout_below=" @+id/et_content "Android:la yout_margintop= "26DP" android:text= "@string/send"/></relativelayout>
Activity code:
Package Com.example.msgsend;import Java.util.list;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.telephony.smsmanager;import android.text.TextUtils; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Actionbaractivity implements onclicklistener{/** phone */private EditText etphonenum;/** SMS content */private EditText etcontent;/** send button */private button BtS end; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get Component Etphonenum = (EditText) Findviewbyid (r.id.et_phonenum); etcontent = (EditText) Findviewbyid (r.id.et_content); btsend = (Button) Findviewbyid (r.id.bt_send); System.out.println ("Get Success");//Register Click event Btsend.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.bt_send:sendmessage (); break;default:break;}} Implement SMS Send private void SendMessage () {String phonenum = Etphonenum.gettext (). toString (); String content = Etcontent.gettext (). toString ();//Toast Prompt if (textutils.isempty (phonenum) | | Textutils.isempty (content)) {Toast.maketext (this, "mobile phone number and text message cannot be empty", Toast.length_long). Show (); return;} Smsmanager Smsmanager = Smsmanager.getdefault ();//SMS is limited in length, the content is split directly list<string> contents = Smsmanager.dividemessage (content);//Send for (String content1:contents) {smsmanager.sendtextmessage (phonenum, NULL, Content1, NULL, NULL);}}}
Effect: