初识安卓小程序(Android短信发送器)

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"短信发送器"

然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.csdn.sms.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/pl_input_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/please_input_number"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/input_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/pl_input_number"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="phone" />

    <TextView
        android:id="@+id/pl_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/input_number"
        android:layout_below="@+id/input_number"
        android:layout_marginTop="47dp"
        android:text="@string/dx_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/pl_content"
        android:layout_below="@+id/pl_content"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/dx_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/content"
        android:layout_below="@+id/content"
        android:layout_marginTop="64dp"
        android:text="@string/dx_send" />

</RelativeLayout>

最后在src下的java文件里MainActivity.java

package com.csdn.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
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 Activity {

	private Button dx_send;
	private EditText dx_content, dx_number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		dx_content = (EditText) this.findViewById(R.id.content);
		dx_number = (EditText) this.findViewById(R.id.input_number);
		dx_send = (Button) this.findViewById(R.id.dx_send);

		//第一种方法:通过点击事件来完成
		dx_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (v.getId()) {
				case R.id.dx_send:

					String content = dx_content.getText().toString().trim();
					String number = dx_number.getText().toString().trim();

					if (TextUtils.isEmpty(content) || TextUtils.isEmpty(number)) {
						Toast.makeText(MainActivity.this, "电话号码不能为空",
								Toast.LENGTH_SHORT).show();
						return;
					} else {
						// 短信管理器
						SmsManager smsManager = SmsManager.getDefault();
						// 如果超过字数限制,则自动拆分短信
						ArrayList<String> c = smsManager.divideMessage(content);
						for (String str : c) {
							smsManager.sendTextMessage(number, null, str, null,
									null);
						}
					}
					break;

				}

			}
		});
	}
	//第二种:通过让该类实现 OnClickListener接口来重写方法完成,和上面点击方法的内容是一样的

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
}

初识安卓小程序(Android短信发送器)

初识安卓小程序(Android短信发送器),布布扣,bubuko.com

初识安卓小程序(Android短信发送器)

上一篇:0301-APP-Dialog


下一篇:Android 电子邮件发送成功与失败的提示