Android编程使用旧的定义新变量?

时间:2013-05-27 02:31:00

标签: java android variables

如何在Android中的java中使用旧变量定义新变量?我在谈论这段代码中有字符串to, message, subject的部分,然后是等号右边的一些占位符。我需要知道如何将textSubject等值更改为先前定义的相关变量。当我彻底更换它们时,由于某种原因它不起作用。

另外,我无法弄清楚为什么在此之前它不会采用onClick方法。这是代码。

public class RefillActivity extends Activity {
    public String name;
    public String birthday;
    public String prescriptionOptions;
    public String notes;
    public String number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.refill_activity_main);
    sendRefillRequest();
    Button submit = (Button) findViewById(R.id.ButtonSendRefillRequest);
    submit.setOnClickListener(new View.onClickListener() {

        @Override
        public void onClick(View view) {

            String to = textTo.getText().toString();
            String message = textMessage.getText().toString();
            String subject = name.getText().toString();

            Intent mEmail = new Intent(Intent.ACTION_SEND);
            mEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@example.com"});
            mEmail.putExtra(Intent.EXTRA_SUBJECT, subject);
            mEmail.putExtra(Intent.EXTRA_TEXT, message);

            // prompts to choose email client
            mEmail.setType("message/rfc822");
            startActivity(Intent.createChooser(mEmail, "Choose an email client to send your feedback!"));
            }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.refill, menu);
    return true;
}
public void sendRefillRequest(){
    final EditText nameField = (EditText) findViewById(R.id.EditTextName);
    name = nameField.getText().toString();
    final EditText birthdayField = (EditText) findViewById(R.id.EditTextBirthday);
    birthday =  birthdayField.getText().toString();
    final EditText numberField = (EditText) findViewById(R.id.EditTextPrescriptionNumber);
    number = numberField.getText().toString();
    final Spinner prescriptionOptions = (Spinner) findViewById(R.id.SpinnerPrescriptionOptions);
    this.prescriptionOptions = prescriptionOptions.getSelectedItem().toString();
    final EditText notesField = (EditText) findViewById(R.id.EditTextSpecialNotes);
    notes = notesField.getText().toString();
}

}

1 个答案:

答案 0 :(得分:2)

您将变量定义为这些方法的局部变量。使它们成为成员变量

public class RefillActivity extends Activity {
public String name;
public String birthday;
public String prescriptionOptions;
public String notes;
public String number;

 EditText nameField;     // define them here
EditText birthdayField; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.refill_activity_main);
    sendRefillRequest();
    Button submit = (Button) findViewById(R.id.ButtonSendRefillRequest);
    submit.setOnClickListener(new View.onClickListener() {

将它们定义为成员变量,然后您可以在sendRefillRequest()中初始化它们,您将可以访问它们

此外,在您的sendRefillRequest()方法中,将String变量定义为final,您可以在onClick

中访问这些变量

如果这不能解决您的问题“...为什么它不会采取onClick方法...”那么请更具体一点。它在做什么/不在做什么?当您点击Button

时,是否在那里放置断点以查看是否输入了断点