发送Email程序----Intetnt在Email上的使用

java:

package EX05_04.txt;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;

public class EX05_04 extends Activity {
	private EditText mEditText1;
	private EditText mEditText2;
	private EditText mEditText3;
	private EditText mEditText4;
	private Button mButton;
	private String[] strEmail = null;
	private String[] strCc = null;
	private String strSubject;
	private String strText;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initApp();
    }

	private void initApp() {
		// TODO Auto-generated method stub
		mEditText1 = (EditText)findViewById(R.id.EditText01);
		mEditText2 = (EditText)findViewById(R.id.EditText02);
		mEditText3 = (EditText)findViewById(R.id.EditText03);
		mEditText4 = (EditText)findViewById(R.id.EditText04);		
		mButton = (Button)findViewById(R.id.Button01);
		
		mEditText1.setOnKeyListener(new EditText.OnKeyListener(){
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (isEmail(mEditText1.getText().toString())) {
					mButton.setEnabled(true);
				} else {
					mButton.setEnabled(false);
				}
				return false;
			}

			private boolean isEmail(String email) {
				// TODO Auto-generated method stub
				String expression = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@" +
						"[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
				CharSequence input = email;
				
				Pattern pattern = Pattern.compile(expression);
				Matcher matcher = pattern.matcher(input);
				return matcher.matches();
			}
			
		});
		
		mButton.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getEditText();
				Intent mIntent = new Intent(android.content.Intent.ACTION_SEND);
				mIntent.setType("plain/text");
				mIntent.putExtra(android.content.Intent.EXTRA_EMAIL, strEmail);
				mIntent.putExtra(android.content.Intent.EXTRA_CC, strCc);
				mIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, strSubject);
				mIntent.putExtra(android.content.Intent.EXTRA_TEXT, strText);
				
				startActivity(Intent.createChooser(mIntent, "your client"));
			}

			private void getEditText() {
				// TODO Auto-generated method stub
				strEmail = new String[]{mEditText1.getText().toString()};
				strCc = new String[]{mEditText2.getText().toString()};
				strSubject = mEditText3.getText().toString();
				strText = mEditText4.getText().toString();
			}
			
		});
	}
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout
	android:layout_marginTop = "20dip"
	android:orientation="horizontal" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">	
	<TextView
		android:layout_marginLeft = "20dip" 
		android:textSize = "16sp"
		android:text="收件人" 
		android:id="@+id/TextView01" 
		android:layout_width="50dip" 
		android:layout_height="wrap_content">
	</TextView>
	<EditText 
		android:text=""
		android:layout_marginLeft = "10dip"  
		android:id="@+id/EditText01" 
		android:layout_width="230dip" 
		android:layout_height="wrap_content">
	</EditText>
</LinearLayout>

<LinearLayout
	android:layout_marginTop = "20dip"
	android:orientation="horizontal" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">	
	<TextView
		android:layout_marginLeft = "20dip" 
		android:textSize = "16sp"
		android:text="附件" 
		android:id="@+id/TextView02" 
		android:layout_width="50dip" 
		android:layout_height="wrap_content">
	</TextView>
	<EditText 
		android:text=""
		android:layout_marginLeft = "10dip"  
		android:id="@+id/EditText02" 
		android:layout_width="230dip" 
		android:layout_height="wrap_content">
	</EditText>
</LinearLayout>

<LinearLayout
	android:layout_marginTop = "20dip"
	android:orientation="horizontal" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">	
	<TextView
		android:layout_marginLeft = "20dip" 
		android:textSize = "16sp"
		android:text="主旨" 
		android:id="@+id/TextView03" 
		android:layout_width="50dip" 
		android:layout_height="wrap_content">
	</TextView>
	<EditText 
		android:text=""
		android:layout_marginLeft = "10dip"  
		android:id="@+id/EditText03" 
		android:layout_width="230dip" 
		android:layout_height="wrap_content">
	</EditText>
</LinearLayout>

<LinearLayout
	android:layout_marginTop = "20dip"
	android:orientation="horizontal" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">	
	<TextView
		android:layout_marginLeft = "20dip" 
		android:textSize = "16sp"
		android:text="内容" 
		android:id="@+id/TextView04" 
		android:layout_width="50dip" 
		android:layout_height="wrap_content">
	</TextView>
	<EditText 
		android:text=""
		android:layout_marginLeft = "10dip"  
		android:id="@+id/EditText04" 
		android:layout_width="230dip" 
		android:layout_height="wrap_content">
	</EditText>
</LinearLayout>


<Button 
	android:text="发送" 
	android:layout_marginTop = "30dip"
	android:id="@+id/Button01" 
	android:layout_gravity = "center"
	android:layout_width="100dip" 
	android:enabled = "false"
	android:layout_height="wrap_content">
</Button>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值