一、采用的是LinearLayout布局,activity_main.xml默认是别的布局。
注意点:
1、wrap_content 自适应大小,radiogroup内对多个radiobutton建议用这个。
2、将原有activity_main.xml删除并重建时,命名也要是activity_main.xml
activity_main.xml
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView //textview用于显示文字
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:text="用户名称" />
<EditText //edittext用于用户自定义的文字编辑框
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户密码" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberPassword" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别" />
<RadioGroup //radiogroup用于管理多个radioboutton用于只可选一个选项
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> ***//radiogroup的设置多个按钮的水平排列或垂直排列的选项***
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton //radiobutton 圆形选项
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="爱好" />
<CheckBox // 用户选择框 可选多个
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳远" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="长跑" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳高" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="散打" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结果" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
二、mainActivity.java
package com.zjut.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private CheckBox check1,check2,check3,check4;
private TextView text4;
private EditText text1;
private EditText text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button2);
View.OnClickListener listener1=new View.OnClickListener() {
@Override
public void onClick(View v) {
String result ="性别:";
RadioButton radio1=(RadioButton) findViewById(R.id.radioButton3);
RadioButton radio2=(RadioButton) findViewById(R.id.radioButton4);
check1=(CheckBox) findViewById(R.id.checkBox2);
check2=(CheckBox) findViewById(R.id.checkBox3);
check3=(CheckBox) findViewById(R.id.checkBox4);
check4=(CheckBox) findViewById(R.id.checkBox5);
text4=(TextView) findViewById(R.id.textView6);
text1=(EditText) findViewById(R.id.editText);
text2=(EditText) findViewById(R.id.editText2);
if(radio1.isChecked())
{
result+="男\n";
}
else if (radio2.isChecked()){
result+="女\n";
}
result+="爱好:";
if(check1.isChecked())
{
result+="跳远 ";
}
if(check2.isChecked())
{
result+="长跑 ";
}
if(check3.isChecked())
{
result+="跳高 ";
}
if(check4.isChecked())
{
result+="散打";
}
result="用户名:"+text1.getText()+"\n"+"密码:"+text2.getText()+"\n"+result;
text4.setText(result);
}
};
button.setOnClickListener(listener1);
}
}
三、效果