Android 系列 4.2从视图发送电子邮件文本

263 篇文章 2 订阅
164 篇文章 0 订阅
4.2从视图发送电子邮件文本


问题
您想要从视图发送包含文本或图像的电子邮件。

将要通过电子邮件发送到邮件应用程序的数据作为参数传递给intent。
讨论
从视图发送电子邮件文本的步骤非常简单:
1.修改AndroidManifest.xml文件以允许Internet连接,以便可以发送电子邮件。 这在实施例4-1中示出。
2.使用用户单击的电子邮件按钮创建视觉表示层。 布局如例4-2所示,用于填充它的字符串如例4-3所示。
3.附加OnClickListener以允许在用户单击电子邮件按钮时发送电子邮件。 代码如例4-4所示。

实例4-1。 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.examples"
android:versionCode="1"
android:versionName="1.0">
<uses-permission
android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".Main"
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>


实例4-2。 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">
<Button
android:id="@+id/emailButton"
android:text="Email Text!"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<TextView
android:id="@+id/text_to_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/my_text" />
</LinearLayout>


实例4-3。Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="hello">Hello World, Main!</string>
<string
name="app_name">EmailFromView</string>
<string
name="my_text">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when
an unknown printer took a galley of type and scrambled it to make a type
specimen book. It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
</string>
</resources>

实例4-4。 Main.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener {
private static final String TAG = "Main";
private Button emailButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the View Layer
setContentView(R.layout.main);
// Get reference to Email Button
this.emailButton = (Button) this.findViewById(R.id.emailButton);
// Sets the Event Listener onClick
this.emailButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == this.emailButton) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_TITLE, "My Title");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
// Obtain reference to String and pass it to Intent
emailIntent.putExtra(Intent.EXTRA_TEXT,
getString(R.string.my_text));
startActivity(emailIntent);
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值