首页 > 代码库 > android之发送短信程序

android之发送短信程序

  首先改写activity_main.xml文件

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/MyLayout"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >	<TableLayout 	    xmlns:android="http://schemas.android.com/apk/res/android"	    android:id="@+id/TableLayout01"	    android:orientation="vertical"	    android:layout_width="fill_parent"	    android:layout_height="wrap_content">	    <TableRow>	        <TextView 	            android:text="收信人:"	            android:layout_width="90px"	            android:layout_height="wrap_content"	            android:textSize="20px"/>	        <EditText 	            android:id="@+id/tel"	            android:numeric="integer"	            android:layout_width="260px"	            android:layout_height="wrap_content"/>	    </TableRow>	    <View 	        android:layout_height="2px"	        android:background="#ff909090"/>	    <TableRow>	        <TextView 	            android:text="内容:"	            android:textSize="20px"	            android:layout_width="90px"	            android:layout_height="wrap_content"/>	            <EditText 	                android:id="@+id/content"	                android:lines="6"	                android:gravity="top"	                android:layout_width="260px"	                android:layout_height="wrap_content"/>	    </TableRow>	    <View	        android:layout_height="2px"	        android:background="#ff909090"/>    	</TableLayout>	<Button 	    android:id="@+id/mybut"	    android:layout_width="fill_parent"	    android:layout_height="wrap_content"	    android:text="发送短信"/></LinearLayout>

  然后编写相应的MainActivity类

代码如下:

package com.example.myclock;import android.net.Uri;import android.os.Bundle;import android.os.SystemClock;import android.os.Vibrator;import android.app.Activity;import android.app.Service;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Chronometer;import android.widget.Chronometer.OnChronometerTickListener;import android.widget.EditText;public class MainActivity extends Activity {	private Button mybut = null;	private EditText tel = null;	private EditText content = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);       this.mybut = (Button)super.findViewById(R.id.mybut);       this.tel = (EditText)super.findViewById(R.id.tel);       this.content = (EditText)super.findViewById(R.id.content);       this.mybut.setOnClickListener(new OnClickListenerlmpl());    }    private class OnClickListenerlmpl implements OnClickListener{    	public void onClick(View view){    		String telStr = MainActivity.this.tel.getText().toString();    		String note = MainActivity.this.content.getText().toString();    		Uri uri = Uri.parse("smsto:"+telStr);    		Intent it = new Intent();    		it.setAction(Intent.ACTION_SENDTO);    		it.putExtra("sms_body", note);    		it.setType("vnd.android-dir/mms-sms");    		it.setData(uri);    		MainActivity.this.startActivity(it);    	}    }}

  运行效果:

android之发送短信程序