<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="25sp"
android:layout_marginLeft="25dp"
/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名长度不小于8"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25sp"
android:layout_marginLeft="50dp"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入6到16位的密码"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重复密码:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/repassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请重新输入密码"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="25sp"
android:layout_marginLeft="50dp"/>
<RadioGroup
android:id="@+id/rdg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="25sp"
android:layout_marginLeft="10dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="女"
android:textSize="25sp"
android:layout_marginLeft="10dp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:textSize="25sp"
android:layout_marginLeft="50dp"
/>
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="籍贯:"
android:textSize="25sp"
android:layout_marginLeft="50dp"
/>
<Button
android:id="@+id/bt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="注册" />
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="取消" />
/>
</LinearLayout>
</LinearLayout>
Java代码:
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.security.acl.Group;
import java.text.BreakIterator;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText username;
private EditText password;
private EditText repassword;
private CharSequence[] items1=new CharSequence[]{
"旅游", "美食", "看电影", "运动"
};
private CharSequence[] items2=new CharSequence[]{
"中国大陆", "中国香港", "中国澳门", "中国台湾","其他"
};
private boolean[] checkedItems1=new boolean[]{false,false,false,false};
private int checkedItems2=1;
private String text="";
private RadioGroup rdg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt1).setOnClickListener(this);
findViewById(R.id.bt2).setOnClickListener(this);
findViewById(R.id.bt3).setOnClickListener(this);
findViewById(R.id.bt4).setOnClickListener(this);
rdg=findViewById(R.id.rdg);
rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rbtn){
text="男";
} else {
text ="女";
}
}
});
}
@Override
public void onClick(View v) {
AlertDialog dialog;
AlertDialog.Builder builder;
switch (v.getId()){
case R.id.bt1:
builder=new AlertDialog.Builder(this)
.setTitle("请添加兴趣爱好:")
.setIcon(R.mipmap.ic_launcher)
.setMultiChoiceItems(items1, checkedItems1, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems1[which]=isChecked;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer stringBuffer=new StringBuffer();
for(int i=0;i<=checkedItems1.length-1;i++){
if(checkedItems1[i]){
stringBuffer.append(items1[i]).append("");
}
}
((Button)findViewById(R.id.bt1)).setText(stringBuffer);
dialog.dismiss();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog=builder.create();
dialog.show();
break;
case R.id.bt2:
builder=new AlertDialog.Builder(this)
.setTitle("请选择籍贯:")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(items2,checkedItems2,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkedItems2 = which;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer stringBuffer=new StringBuffer();
for(int i=0;i<=items2.length-1;i++){
((Button) findViewById(R.id.bt2)).setText(items2[checkedItems2]);
}
if(stringBuffer!=null){
Toast.makeText(MainActivity.this,""+stringBuffer,
Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog=builder.create();
dialog.show();
break;
case R.id.bt3:
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
repassword = (EditText) findViewById(R.id.repassword);
Toast.makeText(MainActivity.this,
"注册成功!"+"\n"
+"用户名:"+username.getText()+"\n"
+"密码:"+password.getText()+"\n"
+"性别:"+text+"\n"
+"爱好:"+((Button) findViewById(R.id.bt1)).getText()+"\n"
+"籍贯:"+((Button) findViewById(R.id.bt2)).getText()+"\n",Toast.LENGTH_SHORT).show();
break;
case R.id.bt4:
Toast.makeText(MainActivity.this,"点击事件2",Toast.LENGTH_SHORT).show();
break;
}
}
}
页面实现: