<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:hint="请输入手机号"android:inputType="phone" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:hint="请输入短信内容"android:inputType="text"android:lines="5" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:onClick="sendSms"android:text="发送" /></LinearLayout>
public void sendSMS(View v) {// 1.取出手机号EditText et_input_num = (EditText) findViewById(R.id.et_input_num);// trim: 过滤用户输入的空格String num = et_input_num.getText().toString().trim();// 2. 取出用户输入的短信内容EditText et_input_content = (EditText) findViewById(R.id.et_input_content);String content = et_input_content.getText().toString().trim();// 3. 校验Pattern pattern = Pattern.compile("^1[3578]\\d{9}$");Matcher matcher = pattern.matcher(num);if (matcher.matches()) {if (content != null && !content.equals("")) {// 4. 校验成功,发送短信SmsManager smsManager = SmsManager.getDefault();smsManager.sendTextMessage(num, null, content, null, null);} else {Toast.makeText(this, "请检查输入的内容", Toast.LENGTH_LONG).show();}} else {Toast.makeText(this, "请检查手机号", Toast.LENGTH_LONG).show();}}
原文:http://www.cnblogs.com/eryan/p/5393743.html