用安卓写一个大学生日常问卷页面,要求有学生性别,年级,日常消费,生活费等问题,并且有提交和退出按钮,用代码实现
时间: 2024-04-29 21:26:49 浏览: 18
好的,以下是一个简单的 Android 应用程序的代码,它包括一个问卷页面,其中包含学生性别、年级、日常消费和生活费等问题,以及提交和退出按钮。
MainActivity.java
```
package com.example.questionnaire;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RadioGroup genderRadioGroup, gradeRadioGroup;
private RadioButton genderRadioButton, gradeRadioButton;
private EditText dailyExpenseEditText, livingExpenseEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
genderRadioGroup = findViewById(R.id.genderRadioGroup);
gradeRadioGroup = findViewById(R.id.gradeRadioGroup);
dailyExpenseEditText = findViewById(R.id.dailyExpenseEditText);
livingExpenseEditText = findViewById(R.id.livingExpenseEditText);
Button submitButton = findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selectedGenderId = genderRadioGroup.getCheckedRadioButtonId();
int selectedGradeId = gradeRadioGroup.getCheckedRadioButtonId();
String dailyExpense = dailyExpenseEditText.getText().toString();
String livingExpense = livingExpenseEditText.getText().toString();
if (selectedGenderId == -1 || selectedGradeId == -1 || dailyExpense.isEmpty() || livingExpense.isEmpty()) {
Toast.makeText(MainActivity.this, "请填写所有问题", Toast.LENGTH_SHORT).show();
} else {
genderRadioButton = findViewById(selectedGenderId);
gradeRadioButton = findViewById(selectedGradeId);
String gender = genderRadioButton.getText().toString();
String grade = gradeRadioButton.getText().toString();
Toast.makeText(MainActivity.this, "你是 " + gender + "," + grade + ",每天花费 " + dailyExpense + " 元,生活费 " + livingExpense + " 元", Toast.LENGTH_SHORT).show();
}
}
});
Button exitButton = findViewById(R.id.exitButton);
exitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生性别:"
android:textSize="18sp"/>
<RadioGroup
android:id="@+id/genderRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/maleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男性"/>
<RadioButton
android:id="@+id/femaleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女性"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年级:"
android:textSize="18sp"/>
<RadioGroup
android:id="@+id/gradeRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/freshmanRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大一"/>
<RadioButton
android:id="@+id/sophomoreRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大二"/>
<RadioButton
android:id="@+id/juniorRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大三"/>
<RadioButton
android:id="@+id/seniorRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大四"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="每天花费:"
android:textSize="18sp"/>
<EditText
android:id="@+id/dailyExpenseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生活费:"
android:textSize="18sp"/>
<EditText
android:id="@+id/livingExpenseEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
<Button
android:id="@+id/exitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
</LinearLayout>
</LinearLayout>
```
这个应用程序将问卷页面布局为一个垂直线性布局,其中包含四个问题:学生性别、年级、每日花费和生活费。性别和年级问题使用水平排列的单选按钮组。每日花费和生活费问题使用 EditText 控件。提交和退出按钮水平排列,并使用 LinearLayout 嵌套在垂直线性布局中。当用户单击提交按钮时,应用程序将检查所有问题是否都已回答。如果所有问题都已回答,则应用程序将 Toast 显示用户的答案。如果有一个或多个问题没有回答,则应用程序将 Toast 显示错误消息。当用户单击退出按钮时,应用程序将关闭。
希望这可以帮助你开始编写你的应用程序!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)