从表单中发送数据以在另一个视图Android中显示

时间:2012-02-02 08:49:20

标签: java android string variables android-intent

我正在尝试显示用户输入的文本,如果它不是null但我遇到了一些麻烦(我是一个Java noob)。以下是我的应用流程:

使用按钮从 Main.java 开始:

-Button:个人资料

如果您点击个人资料,该应用会转到个人资料页 Display.java ,该页面也有一个按钮:

-Button:编辑个人资料

- 此视图还显示信息(姓名,电话号码,邮政编码等)

当用户点击编辑个人资料时,程序会转到 EditProfile.java 的表单,其中有一个表单,供用户输入信息,然后有一个提交按钮。

-Button:提交

此提交按钮将用户带回到上一个视图(Display.java),并显示以前在表单中输入的信息,其中包含字符串 resultText

我不确定如何使这项工作。如果有人有任何建议,我真的很感激帮助!

编辑:有一点需要注意的是,我在Display.java中的if表达式上遇到“死代码”错误

Display.java:

public class Display extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);

    String newline = System.getProperty("line.separator");

    TextView resultText = (TextView) findViewById(R.id.resultText);
    Bundle bundle = getIntent().getExtras();

    String firstName = null;
    String lastName;
    String phoneNumber;
    String city;
    String zipCode;

    if(firstName != null) {

        firstName = bundle.getString("EditTextFirstName");
        lastName = bundle.getString("EditTextLastName");
        phoneNumber = bundle.getString("EditTextPhoneNumber");
        city = bundle.getString("EditTextCity");
        zipCode = bundle.getString("EditTextZipCode");
        resultText.setText("Name: " + firstName + " " + lastName + newline + "Phone Number: " + phoneNumber +
        newline + "City: " + city + newline + "Zip Code: " + zipCode + newline);
    }

    Button profile = (Button) findViewById(R.id.button1);
    profile.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            startActivity(new Intent(Display.this, EditProfile.class));             
        }
    });
}   
}

EditProfile.java:

public class EditProfile extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile);       

 }
public void sendFeedback(View button) {
    final EditText firstnameField = (EditText)this.findViewById(R.id.EditTextFirstName);
    String firstname = firstnameField.getText().toString();

    final EditText lastnameField = (EditText) findViewById(R.id.EditTextLastName);
    String lastname = lastnameField.getText().toString();

    final EditText phoneNumberField = (EditText) findViewById(R.id.EditTextPhoneNumber);
    String phoneNumber = phoneNumberField.getText().toString();

    final EditText cityField = (EditText) findViewById(R.id.EditTextCity);
    String city = cityField.getText().toString();

    final EditText zipCodeField = (EditText) findViewById(R.id.EditTextZipCode);
    String zipcode = zipCodeField.getText().toString();

    int count = 0;
    int fnlen=firstname.length();
    int lnlen=lastname.length();
    int phlen=phoneNumber.length();
    int citylen=city.length();
    int zclen=zipcode.length();

    if (fnlen<=0){
        firstnameField.setError("Enter your first name");
    }
    else {
        count += 1;
    }



    if (lnlen<=0){
        lastnameField.setError("Enter your last name");
    }
    else {
        count += 1;
    }


    if (phlen<=0){
        phoneNumberField.setError("Enter your ten digit phone number");
    }
    else if (phlen!=10){
        phoneNumberField.setError("Phone number must be ten digits");
    }   
    else {
        count += 1;
    }

    if (citylen<=0){
        cityField.setError("Enter your city");
    }
    else {
        count += 1;         
    }
    if (zclen<=0){
        zipCodeField.setError("Enter your Zip Code");
    }
    else if (zclen!=5){
        zipCodeField.setError("Enter a five digit zip code");
    }
    else {
        count += 1;
    }   

    if (count == 5) {

        Intent intent = new Intent();
        intent.setClass(this,Display.class);
        intent.putExtra("EditTextFirstName",firstnameField.getText().toString());
        intent.putExtra("EditTextLastName",lastnameField.getText().toString());
        intent.putExtra("EditTextPhoneNumber",phoneNumberField.getText().toString());
        intent.putExtra("EditTextCity",cityField.getText().toString());
        intent.putExtra("EditTextZipCode",zipCodeField.getText().toString());

        startActivity(intent);
    }
    else {
        count = 0;
    }
}   
}

1 个答案:

答案 0 :(得分:1)

尝试使用`Intent i = getIntent();         如果(i.getExtras()!= NULL){

        firstName = bundle.getString("EditTextFirstName");
    lastName = bundle.getString("EditTextLastName");
    phoneNumber = bundle.getString("EditTextPhoneNumber");
    city = bundle.getString("EditTextCity");
    zipCode = bundle.getString("EditTextZipCode");
    resultText.setText("Name: " + firstName + " " + lastName + newline + "Phone Number: " + phoneNumber +
    newline + "City: " + city + newline + "Zip Code: " + zipCode + newline);
    }`in your Display Class instead of firstName != null) { ...} and let me know if any queries.