实现用户注册页面,包括性别,特长(语文数学英语)的选择,并单击立即注册按钮时,启动另一个activity显示填写的注册信息
时间: 2023-06-12 10:03:33 浏览: 42
实现这个功能,需要以下步骤:
1. 创建注册页面的布局文件,包括性别和特长的选择控件以及“立即注册”按钮。
2. 在注册页面的Activity中,获取性别和特长的选择控件,并设置点击“立即注册”按钮时的监听事件。
3. 在监听事件中获取用户填写的注册信息,并将这些信息封装成一个Intent对象。
4. 调用startActivity方法,启动另一个Activity,将封装好的Intent对象传递过去。
5. 在另一个Activity中获取传递过来的注册信息,并显示在页面上。
下面是一个简单的示例代码:
1. 注册页面布局文件(activity_register.xml):
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择性别:" />
<RadioGroup
android:id="@+id/radio_group_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择特长:" />
<CheckBox
android:id="@+id/checkbox_chinese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="语文" />
<CheckBox
android:id="@+id/checkbox_math"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数学" />
<CheckBox
android:id="@+id/checkbox_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英语" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="立即注册" />
</LinearLayout>
```
2. 注册页面的Activity(RegisterActivity.java):
```
public class RegisterActivity extends AppCompatActivity {
private RadioGroup mRadioGroupGender;
private CheckBox mCheckBoxChinese;
private CheckBox mCheckBoxMath;
private CheckBox mCheckBoxEnglish;
private Button mBtnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// 获取性别和特长的选择控件
mRadioGroupGender = findViewById(R.id.radio_group_gender);
mCheckBoxChinese = findViewById(R.id.checkbox_chinese);
mCheckBoxMath = findViewById(R.id.checkbox_math);
mCheckBoxEnglish = findViewById(R.id.checkbox_english);
mBtnRegister = findViewById(R.id.btn_register);
// 设置立即注册按钮的点击监听事件
mBtnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户填写的注册信息
int genderId = mRadioGroupGender.getCheckedRadioButtonId();
String gender = "";
if (genderId == R.id.radio_male) {
gender = "男";
} else if (genderId == R.id.radio_female) {
gender = "女";
}
String chinese = mCheckBoxChinese.isChecked() ? "语文" : "";
String math = mCheckBoxMath.isChecked() ? "数学" : "";
String english = mCheckBoxEnglish.isChecked() ? "英语" : "";
// 封装注册信息
Intent intent = new Intent(RegisterActivity.this, UserInfoActivity.class);
intent.putExtra("gender", gender);
intent.putExtra("chinese", chinese);
intent.putExtra("math", math);
intent.putExtra("english", english);
// 启动另一个Activity
startActivity(intent);
}
});
}
}
```
3. 用户信息页的Activity(UserInfoActivity.java):
```
public class UserInfoActivity extends AppCompatActivity {
private TextView mTvGender;
private TextView mTvChinese;
private TextView mTvMath;
private TextView mTvEnglish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
// 获取页面上显示注册信息的控件
mTvGender = findViewById(R.id.tv_gender);
mTvChinese = findViewById(R.id.tv_chinese);
mTvMath = findViewById(R.id.tv_math);
mTvEnglish = findViewById(R.id.tv_english);
// 获取传递过来的注册信息
Intent intent = getIntent();
String gender = intent.getStringExtra("gender");
String chinese = intent.getStringExtra("chinese");
String math = intent.getStringExtra("math");
String english = intent.getStringExtra("english");
// 将注册信息显示在页面上
mTvGender.setText("性别:" + gender);
mTvChinese.setText("特长:" + chinese);
mTvMath.setText("特长:" + math);
mTvEnglish.setText("特长:" + english);
}
}
```
以上就是实现用户注册页面并在点击“立即注册”按钮时启动另一个页面显示注册信息的简单示例。