1

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

这是我的代码:

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) {

        }
    }
}

但是,无论输入什么,我总是得到空白文本和微调器的空值。有什么建议么?

4

1 回答 1

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();
        }

    });
    //...
}
于 2012-06-02T18:14:07.283 回答