在Aadroid studio用相对布局做注册页面,注册页面有用户名、密码和男女按钮,用户名和密码为空时,用Toast提示不能为空,当点击注册时,在另一个页面显示所注册的信息,给出相关文件代码。
时间: 2024-04-29 22:23:48 浏览: 14
activity_register.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--用户名-->
<TextView
android:id="@+id/text_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"/>
<EditText
android:id="@+id/edit_username"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_username"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"/>
<!--密码-->
<TextView
android:id="@+id/text_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:layout_below="@id/text_username"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_password"
android:layout_below="@id/edit_username"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"/>
<!--性别-->
<TextView
android:id="@+id/text_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_below="@id/text_password"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"/>
<RadioGroup
android:id="@+id/radio_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_password"
android:layout_toRightOf="@id/text_gender"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp">
<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>
<!--注册按钮-->
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_below="@id/radio_gender"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
```
RegisterActivity.java:
```java
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEditUsername;
private EditText mEditPassword;
private RadioGroup mRadioGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEditUsername = findViewById(R.id.edit_username);
mEditPassword = findViewById(R.id.edit_password);
mRadioGender = findViewById(R.id.radio_gender);
Button btnRegister = findViewById(R.id.btn_register);
btnRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_register:
String username = mEditUsername.getText().toString().trim();
String password = mEditPassword.getText().toString().trim();
int genderId = mRadioGender.getCheckedRadioButtonId();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
String gender = "";
if (genderId == R.id.radio_male) {
gender = "男";
} else if (genderId == R.id.radio_female) {
gender = "女";
}
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("username", username);
intent.putExtra("password", password);
intent.putExtra("gender", gender);
startActivity(intent);
break;
default:
break;
}
}
}
```
ResultActivity.java:
```java
public class ResultActivity extends AppCompatActivity {
private TextView mTextUsername;
private TextView mTextPassword;
private TextView mTextGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
mTextUsername = findViewById(R.id.text_username);
mTextPassword = findViewById(R.id.text_password);
mTextGender = findViewById(R.id.text_gender);
Intent intent = getIntent();
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
String gender = intent.getStringExtra("gender");
mTextUsername.setText("用户名:" + username);
mTextPassword.setText("密码:" + password);
mTextGender.setText("性别:" + gender);
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)