我在Android Studio中编写了以下代码来打印注册表单的信息:
package com.example.session8_project1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class RegisterButtonActivity extends AppCompatActivity {
Button btn;
EditText Name, LastName, Email, Password;
RadioGroup genderRadioGroup;
RadioButton genderRadioButton;
CheckBox chk;
int genderId;
Boolean Conditions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_button);
btn = findViewById(R.id.reg_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chk = findViewById(R.id.CheckBoxConditions);
// Find Ids
Name = findViewById(R.id.NameText);
LastName = findViewById(R.id.LastNameText);
Email = findViewById(R.id.EmailText);
Password = findViewById(R.id.PasswordText);
// Convert to String
String name = Name.getText().toString();
String lastname = LastName.getText().toString();
String email = Email.getText().toString();
String password = Password.getText().toString();
genderRadioGroup = findViewById(R.id.RadGrp);
genderId = genderRadioGroup.getCheckedRadioButtonId();
genderRadioButton = findViewById(genderId);
String gender = genderRadioButton.getText().toString();
// Check whether the Checkboxes are selected or not.
// Conditions = chk.isChecked();
Conditions = chk.isSelected();
Intent in = new Intent(RegisterButtonActivity.this, ResultActivity.class);
in.putExtra("name ", name);
in.putExtra("lastname ", lastname);
in.putExtra("email ", email);
in.putExtra("password ", password);
in.putExtra("gender ", gender);
in.putExtra("Conditions ", Conditions);
startActivity(in);
}
});
}
}
此代码适用的活动如下: 包 com.example.session8_project1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Bundle extras = getIntent().getExtras();
result = findViewById(R.id.ResultText);
if (extras != null){
result.setText(extras.getString("name") + "\n" +
extras.getString("lastname" )+ "\n" +
extras.getString("email") + "\n" +
extras.getString("password") + "\n" +
extras.getString("gender") + "\n" +
extras.getBoolean("Conditions"));
}
}
}
当我填写表格的信息时,它会给我
null
结果。我不知道为什么?有什么不对?