涉及知识点: Toast、CheckBox、Radio、Spinner和相关的点击事件
效果图
当没有输入姓名的时候会提示输入姓名
代码
MainActivity.java
package cn.edu.henu.homework;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import javax.security.auth.Subject;
public class MainActivity extends AppCompatActivity implements CheckBox.OnCheckedChangeListener{
private Spinner spin;
private EditText name;
private RadioGroup gender;
private CheckBox hobby1,hobby2,hobby3,hobby4;
private Button submit,cancel;
RadioButton male,female;
private String result;
private String hobbys="";
private String genderRes="";
private String majorRes="";
private static final String[] majors={"计算机科学与技术","信息安全","软件工程","大数据","网络工程"};
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.spin=findViewById(R.id.spin);
this.name=findViewById(R.id.name);
this.male=findViewById(R.id.male);
this.female=findViewById(R.id.female);
this.hobby1=findViewById(R.id.hobby1);
this.hobby2=findViewById(R.id.hobby2);
this.hobby3=findViewById(R.id.hobby3);
this.hobby4=findViewById(R.id.hobby4);
this.submit=findViewById(R.id.submit);
this.cancel=findViewById(R.id.cancel);
this.gender=findViewById(R.id.gender);
hobby1.setOnCheckedChangeListener(this);
hobby2.setOnCheckedChangeListener(this);
hobby3.setOnCheckedChangeListener(this);
hobby4.setOnCheckedChangeListener(this);
this.spin.setPrompt("请选择你的专业:");
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,majors);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
majorRes=majors[i];
adapterView.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==male.getId()){
genderRes="男";
}
else {
genderRes = "女";
}
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(name.getText().toString().equals("")){
Toast.makeText(submit.getContext(),"请输入您的姓名",Toast.LENGTH_LONG).show();
}
else{
result="";
result+="你好"+name.getText().toString()+"!"+"\n";
result+="你的性别是"+genderRes+"\n";
result+="你的专业是"+majorRes+"\n";
result+="你的个人爱好有"+hobbys+"\n";
Toast.makeText(submit.getContext(),result,Toast.LENGTH_LONG).show();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
hobbys="";
if(hobby1.isChecked())
hobbys+=hobby1.getText().toString()+",";
if(hobby2.isChecked())
hobbys+=hobby2.getText().toString()+",";
if(hobby3.isChecked())
hobbys+=hobby3.getText().toString()+",";
if(hobby4.isChecked())
hobbys+=hobby4.getText().toString()+",";
if(!hobbys.equals("")){
hobbys=hobbys.substring(0,hobbys.length()-1);
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="20sp"
/>
<EditText
android:id="@+id/name"
android:layout_width="250dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="性别"
android:textSize="20sp"
/>
<RadioGroup
android:paddingBottom="10dp"
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专业"
android:textSize="20sp"
/>
<Spinner
android:id="@+id/spin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣爱好"
android:textSize="20sp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/hobby1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"/>
<CheckBox
android:id="@+id/hobby2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/hobby3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳"/>
<CheckBox
android:id="@+id/hobby4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="阅读"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="提交"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="取消"/>
</LinearLayout>
</LinearLayout>