随笔 - 23  文章 - 0 评论 - 14 trackbacks - 0

废话少说,直入主题~!

结构如图:

Send_Email_AddFileActivity.java代码:

public class Send_Email_AddFileActivity extends Activity {
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 0) {
			final Uri uriRet = data.getData();
			if (uriRet != null) {
				try {
					Cursor c = managedQuery(uriRet, null, null, null, null);
					c.moveToFirst();
					String strName = c.getString(c
							.getColumnIndexOrThrow(People.NAME));
					String[] PROJECTION = new String[] {
							Contacts.ContactMethods._ID,
							Contacts.ContactMethods.KIND,
							Contacts.ContactMethods.DATA };
					Cursor newcur = managedQuery(
							Contacts.ContactMethods.CONTENT_URI,
							PROJECTION,
							Contacts.ContactMethods.PERSON_ID + "=\'"
									+ c.getLong(c.getColumnIndex(People._ID))
									+ "\'", null, null);
					startManagingCursor(newcur);
					String email = "";
					if (newcur.moveToFirst()) {
						email = newcur.getString(newcur
								.getColumnIndex(Contacts.ContactMethods.DATA));
						receive_txt.setText(email);
					}

				} catch (Exception e) {
					// TODO: handle exception
					Toast.makeText(Send_Email_AddFileActivity.this,
							e.toString(), 1000).show();
				}

			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private EditText receive_txt;
	private EditText subject_txt;
	private EditText file_txt;
	private EditText content_txt;
	private Button send_btn;
	private Button select_btn;
	private Button people_btn;
	ArrayList<String> al = null;
	String path = "//sdcard";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findAll();
		bind();
	}

	public void findAll() {
		receive_txt = (EditText) this.findViewById(R.id.receive_txt);
		subject_txt = (EditText) this.findViewById(R.id.subject_txt);
		file_txt = (EditText) this.findViewById(R.id.file_txt);
		content_txt = (EditText) this.findViewById(R.id.content_txt);
		send_btn = (Button) this.findViewById(R.id.send_btn);
		select_btn = (Button) this.findViewById(R.id.select_btn);
		people_btn = (Button) this.findViewById(R.id.people_btn);
	}

	public void bind() {
		send_btn.setOnClickListener(mylistener);
		select_btn.setOnClickListener(mylistener);
		people_btn.setOnClickListener(mylistener);
	}

	private View.OnClickListener mylistener = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.send_btn) {
				if (!(receive_txt.getText().toString().trim().equals(""))) {
					if (isEmail(receive_txt.getText().toString().trim())) {
						try {
							Intent mailIntent = new Intent(
									android.content.Intent.ACTION_SEND);

							mailIntent.setType("plain/test");
							String[] strEmailReciver = new String[] { receive_txt
									.getText().toString() };
							String[] strEmailCC = new String[] { file_txt
									.getText().toString() };
							String strEmailSubject = subject_txt.getText()
									.toString();
							String strEmailBody = content_txt.getText()
									.toString();
							mailIntent.putExtra(
									android.content.Intent.EXTRA_EMAIL,
									strEmailReciver);
							mailIntent
									.putExtra(android.content.Intent.EXTRA_CC,
											strEmailCC);
							mailIntent.putExtra(
									android.content.Intent.EXTRA_SUBJECT,
									strEmailSubject);
							mailIntent.putExtra(
									android.content.Intent.EXTRA_TEXT,
									strEmailBody);
							startActivity(Intent
									.createChooser(mailIntent, "发送"));
							Toast.makeText(Send_Email_AddFileActivity.this,
									"发送成功", Toast.LENGTH_LONG).show();

						} catch (Exception ex) {
							Toast.makeText(Send_Email_AddFileActivity.this,
									"发送失败", Toast.LENGTH_LONG).show();
						}
					}
				}
			} else if (v.getId() == R.id.select_btn) {

				LayoutInflater lif = getLayoutInflater();
				View pop = lif.inflate(R.layout.popwindow, null);
				final PopupWindow pw = new PopupWindow(pop, 300, 300, true);
				LinearLayout ll = (LinearLayout) pop.findViewById(R.id.popid);
			    final LinearLayout lly=(LinearLayout) findViewById(R.id.lly);
			    lly.setEnabled(false);
			    ll.setBackgroundColor(Color.BLUE);
				al = new ArrayList<String>();
				File f = new File(path);
				if (f.exists()) {
					readdirectory(path);
				}
				if (al.size() > 0) {
					for (int x = 0; x < al.size(); x++) {
						final TextView atv = new TextView(
								Send_Email_AddFileActivity.this);
						atv.setText(al.get(x));
						ll.addView(atv);
						atv.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								// TODO Auto-generated method stub
								file_txt.setText(atv.getText().toString());
								lly.setEnabled(true);
								pw.dismiss();
							}
						});
					}
				}
				pw.showAtLocation(select_btn, Gravity.CENTER, 0, 0);

			} else if (v.getId() == R.id.people_btn) {
				Uri uri = Uri.parse("content://contacts/people");
				Intent intent = new Intent(Intent.ACTION_PICK, uri);
				startActivityForResult(intent, 0);

			}
		}
	};

	public void readfile(String path) {
		File f = new File(path);
		if (f.exists()) {
			if (!f.isDirectory()) {
				al.add(f.getPath());
			}
		}
	}

	public void readdirectory(String path) {
		File f = new File(path);
		if (f.exists()) {
			String[] filelist = f.list();
			for (int i = 0; i < filelist.length; i++) {
				File fe = new File(path + "\\" + filelist[i]);
				if (fe.isFile()) {
					readfile(path + "\\" + filelist[i]);
				} else if (fe.isDirectory()) {
					readdirectory(path + "\\" + filelist[i]);
				}
			}

		}
	}

	public static boolean isEmail(String strEmail) {
		String strPattern = "^[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]$";

		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(strEmail);
		return m.matches();
	}

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertica" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:id="@+id/lly">
	<LinearLayout android:layout_height="wrap_content"
		android:id="@+id/linearLayout1" android:layout_width="fill_parent">
		<Button android:text="Button" android:id="@+id/send_btn"
			android:layout_width="wrap_content" android:layout_height="fill_parent"></Button>
		<LinearLayout android:layout_height="fill_parent"
			android:id="@+id/linearLayout2" android:orientation="vertical"
			android:layout_width="fill_parent">
			<LinearLayout android:layout_height="wrap_content"
				android:id="@+id/linearLayout3" android:layout_width="fill_parent">
				<TextView android:text="receive:" android:id="@+id/textView1"
					android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
				<EditText android:id="@+id/receive_txt"
					android:layout_weight="1" android:layout_height="wrap_content"
					android:layout_width="fill_parent">
					<requestFocus></requestFocus>
				</EditText>
				<Button android:text="people" android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:id="@+id/people_btn" />
			</LinearLayout>
			<LinearLayout android:layout_height="wrap_content"
				android:id="@+id/linearLayout4" android:layout_width="fill_parent">
				<TextView android:text="subject:" android:id="@+id/textView2"
					android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
				<EditText android:id="@+id/subject_txt"
					android:layout_weight="1" android:layout_height="wrap_content"
					android:layout_width="fill_parent">
					<requestFocus></requestFocus>
				</EditText>
			</LinearLayout>
			<LinearLayout android:layout_height="wrap_content"
				android:id="@+id/linearLayout5" android:layout_width="fill_parent">
				<TextView android:text="file:" android:id="@+id/textView3"
					android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
				<EditText android:id="@+id/file_txt" android:layout_weight="1"
					android:layout_height="wrap_content" android:layout_width="wrap_content"></EditText>
				<Button android:text="select" android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:id="@+id/select_btn" />
			</LinearLayout>
		</LinearLayout>
	</LinearLayout>
	<EditText android:id="@+id/content_txt" android:layout_height="fill_parent"
		android:layout_width="fill_parent" android:inputType="textMultiLine"></EditText>
</LinearLayout>

 

popwindow.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"
  android:id="@+id/popid"
  >
    
</LinearLayout>

androidmanifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="Send_Email_AddFile.Jason"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Send_Email_AddFileActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
posted on 2011-09-22 16:58 Jwc 阅读(...) 评论(...) 编辑 收藏