<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/black">
<ImageView
android:layout_width="60dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher_round"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="填写用户信息"
android:textSize="25sp"
android:textColor="@color/white"
android:padding="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="25dp"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="25sp"
android:textColor="@color/black"/>
<EditText
android:id="@+id/username"
android:layout_weight="6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="username"
android:textSize="25sp"
android:outlineAmbientShadowColor="@color/colorPrimaryDark"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="25sp"
android:textColor="@color/black"/>
<EditText
android:id="@+id/password"
android:layout_weight="6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="password"
android:textSize="25sp"
android:outlineAmbientShadowColor="@color/colorPrimaryDark"/>
</LinearLayout>
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="12dp">
<TextView
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="性 别:"
android:textSize="25sp"
android:textColor="@color/black"/>
<RadioButton
android:id="@+id/rb_man"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="男"
android:textSize="25sp"
android:checked="true"
android:onClick ="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rb_woman"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="女"
android:textSize="25sp"
android:onClick ="onRadioButtonClicked"/>
</LinearLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
android:textSize="25sp"
android:background="@color/darkgrey"
android:layout_marginTop="15dp"
android:layout_marginBottom="25dp"
/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class HomeWorkActivity extends Activity {
private Button btnSubmit;
private EditText username;
private EditText password;
private RadioGroup rgroupSex;
private RadioButton rbWoman;
private RadioButton rbMan;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homework);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
rbMan = findViewById(R.id.rb_man);
rbWoman = findViewById(R.id.rb_woman);
btnSubmit = findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = "";
String unStr = username.getText().toString();
String pwdStr = password.getText().toString();
String sex = "";
if (rbMan.isChecked()) {
sex = rbMan.getText().toString();
} else {
sex = rbWoman.getText().toString();
}
str = unStr + " " + pwdStr + " " + sex;
Toast.makeText(HomeWorkActivity.this, str, Toast.LENGTH_LONG).show();
}
});
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.rb_man:
if (checked) {
rbWoman = findViewById(R.id.rb_woman);
rbWoman.setChecked(false);
break;
}
case R.id.rb_woman:
if (checked) {
rbMan = findViewById(R.id.rb_man);
rbMan.setChecked(false);
break;
}
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4eaa0265c01a77e10f8c47481e1443c4.png)