我创建了一个内置表单的应用程序,目前正在将其发送到预先确定的电子邮件地址。与其根据将其发送到哪个电子邮件地址来创建十几个版本的应用程序,有没有一种方法可以根据人们在表单中按的内容自动添加电子邮件地址?即点击滚动条中的美国,它会自动将美国的电子邮件地址放入电子邮件中发送?
如果是这样,是否有任何人都可以指出我的教程或示例?
我在下面附上了我的代码:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class NearMiss extends Activity {
Button send;
protected static final int CAMERA_PIC_REQUEST = 0;
private TextView tvDisplayDate;
private Button btnChangeDate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearmiss);
Button camera = (Button) findViewById(R.id.button2);
camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
;
}
});
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.EditTextDate);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(day).append("-").append(month + 1).append("-")
.append(year).append(" "));
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(day)
.append("-").append(month + 1).append("-").append(year)
.append(" "));
}
};
public void sendFeedback(View button) {
final EditText nameField = (EditText) findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
final EditText dateField = (EditText) findViewById(R.id.EditTextDate);
String date = dateField.getText().toString();
final EditText timeField = (EditText) findViewById(R.id.EditTextTime);
String time = timeField.getText().toString();
final Spinner feedbackLocation = (Spinner) findViewById(R.id.SpinnerLocationType);
String locationType = feedbackLocation.getSelectedItem().toString();
final EditText contactField = (EditText) findViewById(R.id.EditTextContact);
String contact = contactField.getText().toString();
final Spinner feedbackOperative = (Spinner) findViewById(R.id.SpinnerOperativeType);
String operativeType = feedbackOperative.getSelectedItem().toString();
final CheckBox responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);
boolean bRequiresResponse = responseCheckbox.isChecked();
// Take the fields and format the message contents
String subject = formatFeedbackSubject(feedbackType);
String message = formatFeedbackMessage(feedbackType, name,
email, feedback, date, time, locationType, operativeType, bRequiresResponse, contact);
// Create the message
sendFeedbackMessage(subject, message);
}
protected String formatFeedbackSubject(String feedbackType) {
String strFeedbackSubjectFormat = getResources().getString(
R.string.feedbackmessagesubject_format);
String strFeedbackSubject = String.format(strFeedbackSubjectFormat, feedbackType);
return strFeedbackSubject;
}
protected String formatFeedbackMessage(String feedbackType, String name,
String email, String feedback, String date, String time, String locationType, String operativeType, boolean bRequiresResponse, String contact) {
String strFeedbackFormatMsg = getResources().getString(
R.string.feedbackmessagebody_format);
String strRequiresResponse = getResponseString(bRequiresResponse);
String strFeedbackMsg = String.format(strFeedbackFormatMsg,
name, email, date, time, feedbackType, feedback, locationType, operativeType, strRequiresResponse, contact);
return strFeedbackMsg;
}
protected String getResponseString(boolean bRequiresResponse)
{
if(bRequiresResponse==true)
{
return getResources().getString(R.string.feedbackmessagebody_responseyes);
} else {
return getResources().getString(R.string.feedbackmessagebody_responseno);
}
}
public void sendFeedbackMessage(String subject, String message) {
Intent messageIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { "uk@abc.com", "usa@abc.com" };
messageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
messageIntent.setType("plain/text");
messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(messageIntent);
}
}
和 .xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/mainscreenc"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="125dip"
android:background="@drawable/title" >
</TableRow>
<EditText
android:id="@+id/EditTextName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:hint="@string/feedbackname"
android:inputType="textPersonName"
android:textColor="@color/red" >
</EditText>
<EditText
android:id="@+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="@string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:textColor="@color/red" ></EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="20dip"
android:text="Category of Near Miss:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
<Spinner
android:id="@+id/SpinnerFeedbackType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:entries="@array/feedbacktypelist"
android:prompt="@string/feedbacktitle"
android:textColor="@color/red" >
</Spinner>
<EditText
android:id="@+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="@string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent"
android:textColor="@color/red" ></EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="20dip"
android:text="Date of Near Miss:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
<EditText
android:id="@+id/EditTextDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Approximate Date of Near Miss"
android:textColor="@color/red" >
</EditText>
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="Change Date" />
<EditText
android:id="@+id/EditTextTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Approximate Time of Near Miss"
android:textColor="@color/red" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="20dip"
android:text="Location Of Near Miss:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
<Spinner
android:id="@+id/SpinnerLocationType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:entries="@array/locationtypelist"
android:prompt="@string/locationtitle"
android:textColor="@color/red" >
</Spinner>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="Your Main Office:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
<Spinner
android:id="@+id/SpinnerOperativeType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:entries="@array/operativetypelist"
android:prompt="@string/officetitle"
android:textColor="@color/red" >
</Spinner>
<CheckBox
android:id="@+id/CheckBoxResponse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="@string/feedbackresponse"
android:textColor="@color/black" >
</CheckBox>
<EditText
android:id="@+id/EditTextContact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Contact Number"
android:textColor="@color/red" >
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="Take Photo" />
<Button
android:id="@+id/ButtonSendFeedback"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginTop="15dip"
android:onClick="sendFeedback"
android:text="@string/feedbackbutton" >
</Button>
</LinearLayout>
</ScrollView>
非常感谢