设计一个Activity,输入学生姓名,性别(单选按钮),年级(下拉菜单),业余爱好(复选框),宅的程度(星级评分或拖动条)。提交按钮(Button)清除按钮 2.点击提交按钮后,在Textview中显示学生的选择内容。 3.点击清除按钮后,所有输入和选择的内容清除
时间: 2024-05-10 20:18:03 浏览: 21
下面是一个简单的实现:
1. 在layout文件中定义UI组件
```
<EditText
android:id="@+id/edit_text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:textSize="16sp" />
<RadioGroup
android:id="@+id/radio_group_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<Spinner
android:id="@+id/spinner_grade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/grades"
android:prompt="@string/select_grade" />
<CheckBox
android:id="@+id/check_box_sports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育" />
<CheckBox
android:id="@+id/check_box_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐" />
<CheckBox
android:id="@+id/check_box_reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="阅读" />
<SeekBar
android:id="@+id/seek_bar_indoor_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="5"
android:progress="0" />
<Button
android:id="@+id/button_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
<Button
android:id="@+id/button_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除" />
<TextView
android:id="@+id/text_view_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
```
2. 在Activity中初始化UI组件,并设置按钮的点击事件
```
public class MainActivity extends AppCompatActivity {
private EditText mEditTextName;
private RadioGroup mRadioGroupGender;
private Spinner mSpinnerGrade;
private CheckBox mCheckBoxSports;
private CheckBox mCheckBoxMusic;
private CheckBox mCheckBoxReading;
private SeekBar mSeekBarIndoorLevel;
private Button mButtonSubmit;
private Button mButtonClear;
private TextView mTextViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化UI组件
mEditTextName = findViewById(R.id.edit_text_name);
mRadioGroupGender = findViewById(R.id.radio_group_gender);
mSpinnerGrade = findViewById(R.id.spinner_grade);
mCheckBoxSports = findViewById(R.id.check_box_sports);
mCheckBoxMusic = findViewById(R.id.check_box_music);
mCheckBoxReading = findViewById(R.id.check_box_reading);
mSeekBarIndoorLevel = findViewById(R.id.seek_bar_indoor_level);
mButtonSubmit = findViewById(R.id.button_submit);
mButtonClear = findViewById(R.id.button_clear);
mTextViewResult = findViewById(R.id.text_view_result);
// 设置提交按钮的点击事件
mButtonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入和选择的内容
String name = mEditTextName.getText().toString();
int genderId = mRadioGroupGender.getCheckedRadioButtonId();
String gender = (genderId == R.id.radio_button_male) ? "男" : "女";
String grade = mSpinnerGrade.getSelectedItem().toString();
boolean sports = mCheckBoxSports.isChecked();
boolean music = mCheckBoxMusic.isChecked();
boolean reading = mCheckBoxReading.isChecked();
int indoorLevel = mSeekBarIndoorLevel.getProgress();
// 显示结果
String result = "姓名:" + name + "\n"
+ "性别:" + gender + "\n"
+ "年级:" + grade + "\n"
+ "业余爱好:";
if (sports) {
result += "体育 ";
}
if (music) {
result += "音乐 ";
}
if (reading) {
result += "阅读 ";
}
result += "\n宅的程度:" + indoorLevel + "星";
mTextViewResult.setText(result);
}
});
// 设置清除按钮的点击事件
mButtonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 清除用户输入和选择的内容
mEditTextName.setText("");
mRadioGroupGender.clearCheck();
mSpinnerGrade.setSelection(0);
mCheckBoxSports.setChecked(false);
mCheckBoxMusic.setChecked(false);
mCheckBoxReading.setChecked(false);
mSeekBarIndoorLevel.setProgress(0);
mTextViewResult.setText("");
}
});
}
}
```
3. 在strings.xml中定义下拉菜单的选项和提示信息
```
<resources>
<string name="app_name">Student Info</string>
<string name="select_grade">请选择年级</string>
<array name="grades">
<item>一年级</item>
<item>二年级</item>
<item>三年级</item>
<item>四年级</item>
<item>五年级</item>
<item>六年级</item>
<item>初一</item>
<item>初二</item>
<item>初三</item>
<item>高一</item>
<item>高二</item>
<item>高三</item>
</array>
</resources>
```
这样就可以实现一个简单的学生信息输入界面了。