我创建了一个内置表单的应用,目前正在发送到预先确定的电子邮件地址。而不是创建十几个版本的应用程序,具体取决于发送给它的电子邮件地址,有没有办法,它可以自动添加电子邮件地址取决于人们在表单中按什么?即在滚动条中单击美国,它会自动将美国电子邮件地址放入要发送的电子邮件中吗?
如果是,那么有人可以指点我的教程或示例吗?
我已在下面附上我的代码:
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>
非常感谢
答案 0 :(得分:0)
valueTextView = (TextView)findViewById( R.id.selected );
Spinner s = (Spinner)findViewById(R.id.spinner);
final MyData items[] = new MyData[3];
items[0] = new MyData( "US","us@abc.com" );
items[1] = new MyData( "INDIA","india@abc.com" );
items[2] = new MyData( "UK","uk@abc.com" );
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>this,android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
MyData d = items[position];
valueTextView.setText( d.getValue() );
}
public void onNothingSelected(AdapterView<?> parent)
{
}
}
);
class MyData {
public MyData( String spinnerText, String value ) {
this.spinnerText = spinnerText;
this.value = value;
}
public String getSpinnerText() {
return spinnerText;
}
public String getValue() {
return value;
}
public String toString() {
return spinnerText;
}
String spinnerText;
String value;
}