项目需求

使用Android Studio开发,使用手机开发者模式调试。

(1)图中显示“语文数学”处为一个 TextView 对象,该对象可以 显示:

1、 程序启动后,默认显示个人的姓名和学号。

2 、按钮“左边”点击时,显示“左边”,按钮“右边”点击时, 显示“右边”, 开关被点击时,显示开关的状态“开”,“关”;

多选框“语文”、“数学”、“英语”被勾选时,显示被勾选的科 目;

SeekBar 控件,被拖动的时候,实时显示其值 ;

5星级控件被拖动时,实时显示值。

(2)上图中在文本框中输入“67”,然后点“确定”按钮,更新这两 个控件上面的进度条到 67%的位置。

(3)单选按钮“Apple”和“Android”为一组按钮(即都在同一个 RadioGroup 里),当选择 Apple 时,右边的图标显示为 Apple 的 logo, 当选择 Android 时,右边的图标显示为 Android 的 logo.

编写activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="45dp"
android:text="英语"
app:layout_constraintBottom_toTopOf="@+id/ratingBar"
app:layout_constraintEnd_toEndOf="@+id/seekBar" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="37dp"
android:layout_marginLeft="37dp"
android:layout_marginTop="79dp"
android:layout_marginEnd="37dp"
android:layout_marginRight="37dp"
android:text="左边"
app:backgroundTint="#B8B3B3"
app:layout_constraintEnd_toStartOf="@+id/switch1"
app:layout_constraintStart_toStartOf="@+id/seekBar"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="57dp"
android:layout_marginRight="57dp"
android:text="右边"
app:backgroundTint="#C3C2C2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="26dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="75dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="75dp"
android:layout_marginRight="75dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="name number"
android:textSize="40px"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:gravity="center"
android:text="开关"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="61dp"
android:layout_marginLeft="61dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
app:layout_constraintEnd_toStartOf="@+id/progressBar2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch1" />

<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginTop="59dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="59dp"
android:indeterminate="true"
app:layout_constraintBottom_toBottomOf="@+id/progressBar3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/progressBar"
app:layout_constraintTop_toTopOf="@+id/switch1" />

<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="31dp"
android:layout_marginTop="62dp"
android:layout_marginBottom="67dp"
app:layout_constraintBottom_toBottomOf="@+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/progressBar" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="67dp"
android:ems="10"
android:inputType="textPersonName"
android:text="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />


<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="38dp"
android:layout_marginRight="38dp"
android:text="确定"
app:backgroundTint="#C6C5C5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar3" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="37dp"
android:layout_marginLeft="37dp"
android:layout_marginBottom="16dp"
android:text="语文"
app:layout_constraintBottom_toTopOf="@+id/seekBar"
app:layout_constraintStart_toStartOf="@+id/seekBar" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="数学"
app:layout_constraintBottom_toTopOf="@+id/seekBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="15dp"
android:layout_marginStart="6dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="97dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />


<ImageView
android:id="@+id/imageView"
android:layout_width="155dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="@+id/checkBox3"
app:layout_constraintEnd_toStartOf="@+id/checkBox3"
app:layout_constraintStart_toStartOf="@+id/checkBox3"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
app:srcCompat="@drawable/oip"
tools:ignore="ContentDescription,MissingConstraints" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="154dp"
android:layout_height="0dp"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="31dp"
app:layout_constraintBottom_toTopOf="@+id/checkBox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
tools:ignore="MissingConstraints">

<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="76dp"
android:text="Apple" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="Android" />
</RadioGroup>


</androidx.constraintlayout.widget.ConstraintLayout>

编写MainActivity.java

不得不说,这代码写的是真的烂。

基本是一个功能写一个函数,简直毫无可读性,而且懒得写注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Switch;
import android.widget.RadioButton;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {
private TextView text;
private TextView edit;
private Button leftBtn;
private Button rightBtn;
private Button sureBtn;
private CheckBox chineseBtn;
private CheckBox mathBtn;
private CheckBox englishBtn;
private RadioButton appleBtn;
private RadioButton androidBtn;
private Switch aSwitch;
private ProgressBar progressBar1;
private ProgressBar progressBar2;
private RatingBar ratingBar;
private ImageView imageView;
private SeekBar seekBar;
private String str = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
leftBtn = findViewById(R.id.button);
rightBtn = findViewById(R.id.button2);
sureBtn = findViewById(R.id.button3);
text = findViewById(R.id.textView);
edit = findViewById(R.id.editTextTextPersonName);
aSwitch = findViewById(R.id.switch1);
appleBtn = findViewById(R.id.radioButton);
androidBtn = findViewById(R.id.radioButton2);
chineseBtn = findViewById(R.id.checkBox);
mathBtn = findViewById(R.id.checkBox2);
englishBtn = findViewById(R.id.checkBox3);
imageView = findViewById(R.id.imageView);
progressBar1 = findViewById(R.id.progressBar2);
progressBar2 = findViewById(R.id.progressBar3);
ratingBar = findViewById(R.id.ratingBar);
seekBar = findViewById(R.id.seekBar);

//实现功能
//左边、右边、开关
leftBtn.setOnClickListener(v -> {
text.setText("左边");
});
rightBtn.setOnClickListener(v -> {
text.setText("右边");
});
aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
text.setText("开");
} else {
text.setText("关");
}
});

//复选框多选功能
//可以用Array实现,这里懒得写了
chineseBtn.setOnClickListener(v -> {
if (chineseBtn.isClickable() ) {
//判断str是否已经存在了名称
if (str.contains(chineseBtn.getText())) {
//删除该名称
str = str.replace(chineseBtn.getText(), "");
}else{
str += chineseBtn.getText();
}
text.setText(str);
}
});

mathBtn.setOnClickListener(v -> {
if (mathBtn.isClickable() ) {
if (str.contains(mathBtn.getText())) {
str = str.replace(mathBtn.getText(), "");
}else{
str += mathBtn.getText();
}
text.setText(str);
}
});

englishBtn.setOnClickListener(v -> {
if (englishBtn.isClickable() ) {
if (str.contains(englishBtn.getText())) {
str = str.replace(englishBtn.getText(), "");
}else{
str += englishBtn.getText();
}
text.setText(str);
}
});

//确定按钮控制其上方的进度条
//圈圈和圈圈右边的进度条不用控制
sureBtn.setOnClickListener(v -> {
int num = Integer.parseInt(edit.getText().toString());
if ((0 <= num) && (num <= 100)) {
progressBar2.setProgress(num);
}
});

//seekBar控件拖动条,控制text和星级控件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//范围0-100转为0-5
ratingBar.setRating(progress/20);
//将int型转progress转为String型
text.setText(progress+"");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

//星级控件拖动条,控制text
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
text.setText(rating+"");
}
});

//apple和android按钮
appleBtn.setOnClickListener(v -> {
if(appleBtn.isChecked()){
//(R.drawable.图片名)
imageView.setImageDrawable(getResources().getDrawable(R.drawable.oip));
}
});
androidBtn.setOnClickListener(v -> {
if(androidBtn.isChecked()){
imageView.setImageDrawable(getResources().getDrawable(R.drawable.android));
}
});
}
}

总结

刚好能完成所有功能,并且暂无bug。