First, prepare the character resource
<string name= "Tip_phone" > Please enter your phone number </string>
<string name= "Tip_sms" > Please enter send Message </string>
<string name= "Tip_send" > Send </string>
Second, the page layout
<relativelayout 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"
tools:context= "${relativepackage}.${activityclass}" >
<textview
Android:id= "@+id/tvphone"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/tip_phone"/>
<edittext
Android:id= "@+id/etphone"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/tvphone"
Android:inputtype= "Phone"/>
<textview
Android:id= "@+id/tvsms"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/etphone"
android:text= "@string/tip_sms"/>
<edittext
Android:id= "@+id/etsms"
Android:layout_width= "Match_parent"
android:layout_height= "100DP"
android:layout_below= "@+id/tvsms"
Android:inputtype= "textmultiline"/><!-SMS content line-up
<button
Android:id= "@+id/sendbtn"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/etsms"
android:onclick= "Sendsms"
android:text= "@string/tip_send"/>
</RelativeLayout>
Third, the specific implementation of the Code
public class Smsactivity extends Activity {
Private EditText etphone,etsms;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_sms);
Gets the phone number of the control object
Etphone= (EditText) Findviewbyid (R.id.etphone);
Gets the control object that sent the message
Etsms= (EditText) Findviewbyid (r.id.etsms);
}
public void Sendsms (View v) {
Switch (V.getid ()) {
Case R.ID.SENDBTN:
String Phone=etphone.gettext (). toString ();
String Content=etsms.gettext (). toString ();
2. Message Manager Object
Smsmanager Smsmanager=smsmanager.getdefault ();
3. Split SMS Content
List<string> list=smsmanager.dividemessage (content);
4. Traverse the Sending message
for (String sms:list) {
5. Send a message
Smsmanager.sendtextmessage (phone, NULL, SMS, NULL, NULL);
}
6. Prompt text message sent successfully
Toast.maketext (This, "sent successfully", Toast.length_long). Show ();
Break
Default
Break
}
}
}
Iv. Obtaining permissions
Add text message permissions in Androidmanifest.xml
<uses-permissionandroid:name= "Android.permission.SEND_SMS"/>
The final effect is as follows:
android--Simple Texting function