在Aadroid studio用相对布局做注册页面,注册页面有用户名、密码和男女按钮,当用户名或密码为空时,不能跳转,用Toast提示不能为空,当点击注册时,在另一个页面显示所注册的信息,给出相关文件代码。
时间: 2024-05-02 21:20:19 浏览: 136
登录和注册:这是Android Studio中的登录和注册页面
5星 · 资源好评率100%
1. 在res/layout文件夹下创建register_layout.xml文件,使用相对布局实现注册页面:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/username_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textColorHint="#999"
android:layout_marginTop="40dp"
android:padding="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<EditText
android:id="@+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textColorHint="#999"
android:layout_below="@id/username_et"
android:padding="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/gender_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textColor="#000"
android:layout_below="@id/password_et"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<RadioGroup
android:id="@+id/gender_rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_toRightOf="@id/gender_tv"
android:layout_alignBaseline="@id/gender_tv"
android:layout_marginLeft="10dp">
<RadioButton
android:id="@+id/male_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/female_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:id="@+id/register_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="#fff"
android:background="#1E90FF"
android:layout_below="@id/gender_tv"
android:layout_margin="20dp"/>
</RelativeLayout>
```
2. 在res/layout文件夹下创建register_info_layout.xml文件,用于显示注册信息:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/username_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"/>
<TextView
android:id="@+id/password_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/gender_info_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginTop="10dp"/>
</LinearLayout>
```
3. 在MainActivity.java中实现注册页面逻辑:
```java
public class MainActivity extends AppCompatActivity {
private EditText mUsernameEt;
private EditText mPasswordEt;
private RadioGroup mGenderRg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
mUsernameEt = findViewById(R.id.username_et);
mPasswordEt = findViewById(R.id.password_et);
mGenderRg = findViewById(R.id.gender_rg);
Button registerBtn = findViewById(R.id.register_btn);
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsernameEt.getText().toString();
String password = mPasswordEt.getText().toString();
int genderId = mGenderRg.getCheckedRadioButtonId();
if (TextUtils.isEmpty(username)) {
Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(MainActivity.this, RegisterInfoActivity.class);
intent.putExtra("username", username);
intent.putExtra("password", password);
if (genderId == R.id.male_rb) {
intent.putExtra("gender", "男");
} else {
intent.putExtra("gender", "女");
}
startActivity(intent);
}
});
}
}
```
4. 在RegisterInfoActivity.java中显示注册信息:
```java
public class RegisterInfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_info_layout);
TextView usernameTv = findViewById(R.id.username_tv);
TextView passwordTv = findViewById(R.id.password_tv);
TextView genderTv = findViewById(R.id.gender_info_tv);
Intent intent = getIntent();
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
String gender = intent.getStringExtra("gender");
usernameTv.setText("用户名:" + username);
passwordTv.setText("密码:" + password);
genderTv.setText("性别:" + gender);
}
}
```
阅读全文