Java 如何打开按钮
我有一个问题,我需要我的“发送”按钮发送电子邮件到我的按钮时,用户点击它。我只是想知道,当用户点击“发送”按钮时,该按钮就会知道我的电子邮件地址,并自动发送到那里 到目前为止,我的EmailActivity.java中已经有了这个Java 如何打开按钮,java,android,Java,Android,我有一个问题,我需要我的“发送”按钮发送电子邮件到我的按钮时,用户点击它。我只是想知道,当用户点击“发送”按钮时,该按钮就会知道我的电子邮件地址,并自动发送到那里 到目前为止,我的EmailActivity.java中已经有了这个 import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.co
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 :"));
}
});
}
电子邮件布局.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" />
用这个。根据您的需要更改值我已经尝试过这个方法,但我不知道在我的代码中放在哪里,因为当我尝试时它仍然不起作用。我应该在代码中的何处放置此项?将此项放置在button.onClickListener方法在用户单击按钮后,用户将看到一个选择器电子邮件对话框->用户选择一个电子邮件发件人,然后在“发送邮件”活动中单击“发送”按钮。您确定这3个字符串具有正确的值吗?你有没有试过登录,有没有出错;如果没有错误,我所需要的只是让我的“发送”按钮已经注册了我的电子邮件。因此,当用户单击“发送”时,它将自动发送到我的电子邮件,这意味着您希望在收件箱中接收电子邮件,而不是从您的电子邮件中发送?是的,用户会键入他/她的电子邮件,然后单击“发送”按钮。它将在我的收件箱中弹出
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 :"));