gpt4 book ai didi

java - 从表单发送数据以显示在另一个 Android View 中

转载 作者:行者123 更新时间:2023-12-01 15:34:30 25 4
gpt4 key购买 nike

我正在尝试显示用户输入的文本(如果它不为空),但我遇到了一些麻烦(我是一个 Java 菜鸟)。这是我的应用程序的流程:

Main.java 开始,带有一个按钮:

-按钮:个人资料

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

-按钮:编辑个人资料

-此 View 还显示信息(姓名、电话号码、邮政编码等)

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

-按钮:提交

此提交按钮将用户带回到上一个 View (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;
}
}
}

最佳答案

尝试使用`Intent i= getIntent(); if(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.

关于java - 从表单发送数据以显示在另一个 Android View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9109381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com