Android初学者:电子邮件和字符串

时间:2012-06-02 17:43:46

标签: java android eclipse android-layout android-intent

我正在创建一个带有意见箱的应用。当用户将信息输入到编辑文本中时,检索信息并将其放入字符串值中。然后将该字符串放入电子邮件的文本中。但是,在发送电子邮件时,文本不包含编辑文本的值。

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.suggestions);

        Spinner spinner = (Spinner) findViewById(R.id.emailArea);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter
                .createFromResource(this, R.array.email_array,
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

        Button send = (Button) findViewById(R.id.emailSendButton);
        EditText ettext = (EditText) findViewById(R.id.etEmailText);
        EditText returnAddress = (EditText) findViewById(R.id.etReturnAddress);

        String text = ettext.getText().toString();
        String returnAddressText = returnAddress.getText().toString();

        emailText =
                "\n\nArea in the Guide: " + area
                + "\n\nSuggestion or Comment: " + text + 
                "\n\nReturn Address: "+ returnAddressText;

        send.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                Intent emailIntent = new Intent(
                        android.content.Intent.ACTION_SEND);

                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                        address);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        "Suggestions/Comments: Black Ops 2");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        emailText);
                emailIntent.setType("text/plain");

                startActivity(Intent.createChooser(emailIntent, "Send Mail"));
                finish();
            }

        });
    }

    private void showToast() {
        Toast.makeText(this, "Thank you. Your message has been sent.",
                Toast.LENGTH_SHORT).show();// TODO Auto-generated method stub

    }

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                long id) {

            area = areas[pos];
        }

        public void onNothingSelected(AdapterView parent) {

        }
    }
}

然而,无论输入什么,我总是得到空白文本,并为旋转器null。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

检索EditText中的onClickListener值并使用该值。

EditText etText;
EditText returnAddress;
public void onCreate(Bundle savedInstanceState) {
    //....
    Button send = (Button) findViewById(R.id.emailSendButton);
    etText = (EditText) findViewById(R.id.etEmailText);
    returnAddress = (EditText) findViewById(R.id.etReturnAddress);
     send.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            String text = etText.getText().toString();
            String returnAddressText = returnAddress.getText().toString();

            emailText =
                "\n\nArea in the Guide: " + area
                + "\n\nSuggestion or Comment: " + text + 
                "\n\nReturn Address: "+ returnAddressText;

            Intent emailIntent = new Intent(
                    android.content.Intent.ACTION_SEND);

            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                    address);
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    "Suggestions/Comments: Black Ops 2");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    emailText);
            emailIntent.setType("text/plain");

            startActivity(Intent.createChooser(emailIntent, "Send Mail"));
            finish();
        }

    });
    //...
}