如何使按钮打开 [英] How to make button open

查看:111
本文介绍了如何使按钮打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,当用户点击它时,我需要我的发送按钮将电子邮件发送到我的按钮。我只是想到达哪里,当用户点击发送按钮时,该按钮已经知道我的电子邮件地址并自动发送到那里的电子邮件。

I have a problem, I need my 'Send' button to send the email to my button when the user clicks on it. I just want to get where, when users click send Button then the button will already know my email address and send it to there E-mail automatically.

到目前为止,我的 EmailActivity.java

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class EmailActivity extends Activity {

Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email_layout);

buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);

buttonSend.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        String to = textTo.getText().toString();
        String subject = textSubject.getText().toString();
        String message = textMessage.getText().toString();

        Intent email = new Intent(Intent.ACTION_SEND);
        email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
        //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
        //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
        email.putExtra(Intent.EXTRA_SUBJECT, subject);
        email.putExtra(Intent.EXTRA_TEXT, message);

        //need this to prompts email client only
        email.setType("message/rfc822");

        startActivity(Intent.createChooser(email, "Choose an Email client :"));

    }
});
}

email_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewPhoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" >

<requestFocus />

</EditText>

<TextView
android:id="@+id/textViewSubject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextSubject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</EditText>

<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="5" />

<Button
android:id="@+id/buttonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" />

推荐答案

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto","email@email.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));

USe thi。根据您的需要更改值

USe thi. Change the values as per your need

这篇关于如何使按钮打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆