移动应用开发技术实验代码

实验二

实验2.1

main_activity.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
package com.example.text2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private EditText name = null;
private Button tixing = null;
private CheckBox box1 , box2 , box3, box4;

private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.name = (EditText)super.findViewById(R.id.edit1);
this.tixing = (Button)super.findViewById((R.id.tixing));
box1 = (CheckBox)super.findViewById((R.id.box1));
box2 = (CheckBox)super.findViewById((R.id.box2));
box3 = (CheckBox)super.findViewById((R.id.box3));
box4 = (CheckBox)super.findViewById((R.id.box4));

checkBoxList.add(box1);
checkBoxList.add(box2);
checkBoxList.add(box3);
checkBoxList.add(box4);

tixing.setOnClickListener(new ShowListerer());

}
private class ShowListerer implements View.OnClickListener {
public void onClick(View v)
{
StringBuffer sb = new StringBuffer();
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()){
sb.append(checkbox.getText().toString() + " ");
}
}
String info = name.getText().toString();
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);//MainActivity.this为当前环境



if(info.trim().equals(""))
{
builder.setMessage("请输入姓名");//提示内容
}
else
{
builder.setMessage("姓名是: "+info+"\n"+"性别"+"\n"+"专业"+"\n"+"兴趣爱好 "+""+sb.toString());//提示内容

}

builder.setPositiveButton("确认", null);//确定按钮

builder.create().show();

}
}
}

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
<?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"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20dp"
android:layout_weight="1"/>

<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="李坤松 2012020047"
android:textSize="20dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:id="@+id/gender"
android:layout_marginLeft="10dp"
android:layout_marginTop="19dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/Radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"

android:orientation="horizontal">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="男"
android:textSize="20dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="女"
android:textSize="20dp"/>
</RadioGroup>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="专业"
android:textSize="20dp"/>
<Spinner
android:id="@+id/chose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:prompt="@string/eduprompt"
android:entries="@array/edu"
android:spinnerMode="dialog"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="兴趣爱好"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="音乐"/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="运动"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="游泳"/>
<CheckBox
android:id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="阅读"/>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/tixing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="提交"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="取消"/>
</LinearLayout>

</LinearLayout>

strings.xml

1
2
3
4
<resources>
<string name="app_name">text2</string>
<string name="eduprompt">请选择你的专业</string>
</resources>

edu.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="edu">
<item>计算机科学与技术</item>
<item>大数据开发</item>
<item>信息安全</item>
<item>人工智能</item>
</string-array>
</resources>

实验2.2-1

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
package com.example.text22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
private String listdata[] = {
"移动应用开发技术","计算机组成原理","数据结构","操作系统","面向对象技术","逻辑设计"
};
private ListView listview;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.textview = (TextView) super.findViewById(R.id.title) ;
this.listview = (ListView) super.findViewById(R.id.Mylistview);


this.listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,
this.listdata));

listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
String text = (String) listview.getItemAtPosition(arg2);
textview.setText(text);
}
});
}
}

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
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MenuDemo"
android:layout_marginLeft="20dp"
android:gravity="left"
android:textSize="20dp"/>

<ListView
android:id="@+id/Mylistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>




</LinearLayout>

实验效果

image.png


实验2.2-2

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
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
package com.example.test22_2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<Map<String,Object>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<Map<String,Object>>();
listView = (ListView) findViewById(R.id.listView);

Map<String,Object> map1 = new HashMap<String,Object>();
Map<String,Object> map2 = new HashMap<String,Object>();
Map<String,Object> map3 = new HashMap<String,Object>();
Map<String,Object> map4 = new HashMap<String,Object>();
Map<String,Object> map5 = new HashMap<String,Object>();
Map<String,Object> map6 = new HashMap<String,Object>();

map1.put("title","姓名");
map1.put("info","李坤松");
map1.put("picture",R.drawable.img);
list.add(map1);
map2.put("title","性别");
map2.put("info","男");
map2.put("picture",R.drawable.img);
list.add(map2);

map3.put("title","学号");
map3.put("info","2012020047");
map3.put("picture",R.drawable.img);
list.add(map3);
map4.put("title","年龄");
map4.put("info","19");
map4.put("picture",R.drawable.img);
list.add(map4);
map5.put("title","居住地");
map5.put("info","开封");
map5.put("picture",R.drawable.img);
list.add(map5);
map6.put("title","邮箱");
map6.put("info","2012020047@henu.edu.cn");
map6.put("picture",R.drawable.img);
list.add(map6);


// SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.user,new String[]
// {"title","info","picture"},
// new int[] {R.id.title,R.id.info,R.id.picture});
MyAdapter adapter = new MyAdapter(this,list,R.layout.user);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setCurrentItem(i);
adapter.setClick(true);
adapter.notifyDataSetChanged();
}
});

}
class MyAdapter extends BaseAdapter {
private Context context;
private int resource;
private List<Map<String,Object>> list;
private int mCurrentItem=0;
private boolean isClick=false;

/**
* 有参构造
*
* @param context
* 界面
* @param list
* 数据
* @param resource
* 列表项资源文件
*/

public MyAdapter(Context context, List<Map<String, Object>> list, int resource) {
this.context = context;
this.list = list;
this.resource = resource;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int i) {
return list.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// 声明内部类
Util util = null;

final int flag = i;

if(view == null){
util = new Util();
// 给xml布局文件创建java对象
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(resource, null);
// 指向布局文件内部组件
util.picture = (ImageView) view.findViewById(R.id.picture);
util.info = (TextView) view.findViewById(R.id.info);
util.title = (TextView) view.findViewById(R.id.title);
// 增加额外变量
view.setTag(util);
}
else {
util = (Util) view.getTag();
}
// 获取数据显示在各组件
Map<String, Object> map = list.get(i);
util.info.setText((String) map.get("info"));
util.title.setText((String) map.get("title"));
util.picture.setImageResource((Integer) map.get("picture"));


LinearLayout parent= (LinearLayout) view.findViewById(R.id.parent1);

if (mCurrentItem==i&&isClick){
parent.setBackgroundColor(Color.parseColor("#708090"));
}else{
parent.setBackgroundColor(Color.parseColor("#FFB6C1"));
}

return view;
}



public void setCurrentItem(int currentItem){
this.mCurrentItem=currentItem;
}

public void setClick(boolean click){
this.isClick=click;
}
/**
* 内部类,用于辅助适配
*
* @author qiangzi
*
*/
class Util{
TextView title;
TextView info;
ImageView picture;
}
}
}

activity.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="个人信息"
android:textSize="20dp"/>

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

user.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent1"
android:cacheColorHint = "#2BE4D3">

<ImageView
// 图片在res/drawabale里面,自己放
android:id="@+id/picture"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_margin="5dip"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>

</LinearLayout>


</LinearLayout>

实验效果

image.png

实验2.3

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
package com.example.a23;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button fir = null;
private Button sec = null;
private Button thr = null;
private Button fou = null;
private Button fif = null;

private Button round = null;
private Button stra = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.fir = (Button)super.findViewById(R.id.fir);
this.sec = (Button)super.findViewById(R.id.sec);
this.thr = (Button)super.findViewById(R.id.thr);
this.fou = (Button)super.findViewById(R.id.fou);
this.fif = (Button)super.findViewById(R.id.fif);


// 进度条
this.round = (Button)super.findViewById(R.id.round);
this.stra = (Button)super.findViewById(R.id.stra);



// 调用buclick 里面的alertdialog方法
fir.setOnClickListener(new buclick.click());
sec.setOnClickListener(new buclick.click());
thr.setOnClickListener(new buclick.click());
fou.setOnClickListener(new buclick.click());
fif.setOnClickListener(new buclick.click());

round.setOnClickListener(new buclick.click());
stra.setOnClickListener(new buclick.click());


}



}

buclick.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
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
package com.example.a23;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class buclick {

private Context context;

public Context getContxt() {
return context;
}
public void setContxt(Context contxt) {
this.context = (MainActivity) contxt;
}

public static class click implements View.OnClickListener{

@Override
public void onClick(View v) {
switch (v.getId()) {
// 分类对每个按钮的alertdialog进行设置
case R.id.fir:
AlertDialog.Builder dialog = new AlertDialog.Builder (v.getContext());
dialog.setTitle("删除对话框");
dialog.setMessage("确定要删除对话框嘛");
dialog.setIcon(R.drawable.fir);
dialog.setCancelable(false);
dialog.setPositiveButton("是", new DialogInterface.
OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("否", new DialogInterface.
OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNeutralButton("查看详情",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});
dialog.show();
break;
case R.id.sec:
AlertDialog.Builder dialog2 = new AlertDialog.Builder (v.getContext());
dialog2.setTitle("你喜欢吃哪种水果");
dialog2.setIcon(R.drawable.fir);
dialog2.setCancelable(false);

dialog2.setItems(new String[]{"苹果","芒果","草莓","香蕉"},new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});

dialog2.setNeutralButton("取消",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});
dialog2.show();
break;
case R.id.thr:
AlertDialog.Builder dialog3 = new AlertDialog.Builder (v.getContext());
dialog3.setTitle("你喜欢吃哪种水果");
dialog3.setIcon(R.drawable.fir);
dialog3.setCancelable(false);

// 单选框
dialog3.setSingleChoiceItems(new String[]{"苹果","芒果","草莓","香蕉"},0,
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});

dialog3.setPositiveButton("确认",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});
dialog3.setNegativeButton("取消",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});
dialog3.show();
break;
case R.id.fou:
AlertDialog.Builder dialog4 = new AlertDialog.Builder (v.getContext());
dialog4.setTitle("你喜欢吃哪种水果");
dialog4.setIcon(R.drawable.fir);
dialog4.setCancelable(false);

// 多选框
dialog4.setMultiChoiceItems(new String[]{"苹果","芒果","草莓","香蕉"},
null,null);

dialog4.setPositiveButton("确认",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});
dialog4.setNegativeButton("取消",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){

}
});
dialog4.show();
break;
case R.id.fif:
AlertDialog.Builder dialog5 = new AlertDialog.Builder (v.getContext());
dialog5.setCancelable(false);
dialog5.setIcon(R.drawable.fir);
dialog5.setTitle("用户登录");
dialog5.setView(R.layout.dialog);
dialog5.setPositiveButton("登录",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog , int which){

}
});
dialog5.setNegativeButton("取消",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog , int which){

}
});
dialog5.show();
break;
case R.id.round:
ProgressDialog round = new ProgressDialog(v.getContext());
round.setIcon(R.drawable.fir);
round.setTitle("提示");
round.setMessage("这是一个圆形进度条");
round.setCancelable(true);
round.show();
break;
case R.id.stra:
ProgressDialog stra = new ProgressDialog(v.getContext());
stra.setIcon(R.drawable.fir);
stra.setTitle("提示");
stra.setMessage("这是一个长条形进度对话框");
stra.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
stra.setMax(100);
stra.setCancelable(true);
stra.show();
Task t = new Task(stra);
t.start();
break;
default:
break;
}
}
}
// 为条形进度条添加run方法
private static class Task extends Thread {
//为了避免死循环,定义一个控制变量,使得程序不会死掉
boolean run = true;
ProgressDialog stra;

Task(ProgressDialog p) {
stra = p;
}

@Override
public void run() {
while (run) {
try {

stra.setProgress(stra.getProgress() + 1);
if (stra.getProgress() >= 100) {
stra.cancel();
run = false;
}
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
run = false;//产生异常的时候,让他停止
}
}
}
}
}

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
<?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"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/fir"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:layout_gravity="center"
android:text="确认对话框"/>

<Button
android:id="@+id/sec"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="列表对话框"/>

<Button
android:id="@+id/thr"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="单选对话框"/>

<Button
android:id="@+id/fou"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="多选对话框"/>

<Button
android:id="@+id/fif"

android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="自定义对话框"/>

<Button
android:id="@+id/round"

android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="圆形进度条对话框"/>

<Button
android:id="@+id/stra"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="条形进度条对话框"/>



</LinearLayout>

dialog.xml

(在layout文件夹里面)

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/dialog"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "用户名:"
android:id="@+id/name"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mname"/>

</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_marginLeft="3dp"
android:layout_marginRight="9dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "密码: "
android:id="@+id/password"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mpassword"/>

</LinearLayout>


</LinearLayout>

修改button的背景颜色

  1. 打开values文件夹里面的themes里面第一个themes.xml文件

  2. 修改里面的第二行

    1
    2
    3
    <style name="Theme.23" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    改为:
    <style name="Theme.23" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
  3. 修改后初始界面就变成灰色了,而不是一开始的蓝紫色了

效果图

image-20220420170618555

TabHost

注释:此次并非实验指导书上要求的代码,是书上的源码,只是为了方便理解,2.4实验就不再重新写

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
<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--顶部导航-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- TabWidget 每个Tab的选项,即Tab标签-->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Tab页1-->
<LinearLayout
android:id="@+id/file"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<Button
android:id="@+id/open"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="打开文件" />

<Button
android:id="@+id/save"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="保存文件" />

<Button
android:id="@+id/saveAs"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="文件另存为" />
</LinearLayout>
<!--Tab页2-->
<LinearLayout
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<Button
android:id="@+id/copy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="复制" />

<Button
android:id="@+id/paste"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="粘贴" />
</LinearLayout>
<!--Tab页3-->
<LinearLayout
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入检索关键字..."
android:textSize="18sp" />

<Button
android:id="@+id/seekbut1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<!--Tab页4-->
<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<TimePicker
android:id="@+id/seekbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置时间" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

activity_three.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
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--实现底部导航-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<LinearLayout
android:id="@+id/file"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<Button
android:id="@+id/open"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="打开文件" />

<Button
android:id="@+id/save"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="保存文件" />

<Button
android:id="@+id/saveAs"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="文件另存为" />
</LinearLayout>

<LinearLayout
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<Button
android:id="@+id/copy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="复制" />

<Button
android:id="@+id/paste"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="粘贴" />
</LinearLayout>

<LinearLayout
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入检索关键字..."
android:textSize="18sp" />

<Button
android:id="@+id/seekbut1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>

<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<TimePicker
android:id="@+id/seekbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置时间" />
</LinearLayout>
</FrameLayout>

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="0" />
</LinearLayout>
</TabHost>


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
package com.example.a31;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private TabHost myTabHost; //定义TabHost
private int[] layRes = {R.id.file, R.id.edit, R.id.seek, R.id.time};//定义内嵌布局管理器ID
private Button openButton;

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.open:
Toast.makeText(MainActivity.this, "这是打open按钮", Toast.LENGTH_LONG).show();
break;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
myTabHost = findViewById(R.id.tabhost); //取得TabHost对象

myTabHost.setup();
//建立TabHost对象
TabHost.TabSpec myTab1 = myTabHost.newTabSpec("文件"); //定义TabSpec
myTab1.setIndicator("File"); //设置选项文字
myTab1.setContent(layRes[0]); //设置显示的组件
myTabHost.addTab(myTab1); //增加选项


TabHost.TabSpec myTab2 = myTabHost.newTabSpec("编辑"); //定义TabSpec
myTab2.setIndicator("Edit").setContent(layRes[1]); //设置显示的组件
myTabHost.addTab(myTab2); //增加选项

TabHost.TabSpec myTab3 = myTabHost.newTabSpec("查看"); //定义TabSpec
myTab3.setIndicator("View").setContent(layRes[2]); //设置显示的组件
myTabHost.addTab(myTab3); //增加选项

TabHost.TabSpec myTab4 = myTabHost.newTabSpec("设置") //定义TabSpec
.setIndicator("Set") //设置选项文字
.setContent(layRes[3]); //设置显示的组件
myTabHost.addTab(myTab4); //增加选项

myTabHost.setCurrentTab(0); //设置开始默认选项
// myTabHost.setCurrentTabByTag("文件");
openButton = findViewById(R.id.open);
openButton.setOnClickListener(this);
//选项切换事件处理,setOnTabChangedListener
myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
}
});
}
}

效果图

image-20220503235657029

实验三

实验3.1

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
package com.example.a3_11;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private String listdata[] = {"李四","张三","王五","陈六","宋七"};
private ListView listView;
private List<String> list;
private ArrayAdapter adpter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView)findViewById(R.id.listview);
// 将listdata的数据导入到list的 数组中,通过删除数组的元素,来实现删除操作
list = new ArrayList<String>(Arrays.asList(listdata));

// 使用ArrayAdapter的适配器
adpter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1
,this.list);
this.listView.setAdapter(adpter);

// listview 的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// 向 MainActivity.java传入数据
String name = (String)listView.getItemAtPosition(i);
Intent it = new Intent(MainActivity.this,MainActivity2.class);
it.putExtra("sendinfo",name);
it.putExtra("value",i);
startActivityForResult(it,1);
adpter.notifyDataSetChanged();
}
});
}

// 定义一个方法,用来删除数据
public void delete(ArrayAdapter adapterView, int i)
{
String text = (String)listView.getItemAtPosition(i);
// 删除listview 中第i个数据
adpter.remove(listView.getItemAtPosition(i));
Toast.makeText(getApplicationContext(),text+"的记录已被删除",Toast.LENGTH_LONG).show();
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK)
{
// 返回点击该项的索引
int h = data.getIntExtra("return",1);
// 调用删除的方法
delete(adpter,h);
}
else {
String text = "取消操作";
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}
default:
break;
}
}
}

MainActivity2.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
package com.example.a3_11;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

// 获得Mainactivity.java提供过来的参数
Intent it = super.getIntent();
String info = it.getStringExtra("sendinfo"); // listview的内容
int position = it.getIntExtra("value",1); // 点击的数组索引

// 对xml中的布局变量进行注册
Button btn1= (Button) findViewById(R.id.fir);
Button btn2 = (Button) findViewById(R.id.sec);
TextView txt = (TextView) findViewById(R.id.txt);


txt.setText("删除 "+info+" 的记录吗?");

// 调用两个按钮的点击事件
// 确定按钮
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 向main activity 返回点击的状态,从而执行删除操作
getIntent().putExtra("return",position);
setResult(RESULT_OK,MainActivity2.this.getIntent());
finish();
}
});
// 取消按钮
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 按钮2 是取消操作
Intent it2 = new Intent();
setResult(RESULT_CANCELED,it2);
finish();
}
});
}
}

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
<?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"
android:orientation="vertical"
tools:context=".MainActivity">


<!-- 测试返回的值-->
<!-- <EditText-->
<!-- android:id="@+id/mag"-->
<!-- android:text="nihao"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"/>-->

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



</LinearLayout>

activity_main2.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
<?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"
android:orientation="vertical"
tools:context=".MainActivity2">

<TextView
android:id="@+id/txt"
android:text=""
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<LinearLayout
android:id="@+id/text"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<Button
android:id="@+id/fir"
android:layout_width="150dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:text="确认"/>

<Button
android:id="@+id/sec"
android:layout_marginLeft="10dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="取消"/>

</LinearLayout>

</LinearLayout>

实验结果:

image-20220504183521712

image-20220504183531073

image-20220504183542615

HashMap 和Map区别

Map

  1. Map集合一次存储两个对象,一个键对象,一个值对象
  2. 键对象在集合中是唯一的,可以通过键来查找值

HashMap

  1. 使用哈希算法对键去重复,效率高,但无序
  2. HashMap是Map接口的主要实现类

ArrayList

List是一个接口,而ArrayList是List接口的一个实现类

1
2
3
4
5
6
7
8
9
10
ArrayList<E> objectName =new ArrayList<>();  // 初始化

ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Weibo");
sites.set(2, "Wiki"); // 第一个参数为索引位置,第二个为要修改的值
sites.remove(3); // 删除第四个元素
System.out.println(sites.size());
  • E: 泛型数据类型,用于设置 objectName 的数据类型,只能为引用数据类型
  • objectName: 对象名。

实验3.2

MainActivity

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
package com.henu.a3_2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.nio.channels.Channel;

public class MainActivity extends AppCompatActivity {

private int id = 1;
private static final String TAG ="" ;
private static final int MAX = 1000;
private Context context;
private NotificationManager notificationManager;
private Notification notification;
private String Channeld;
private CharSequence ChannelName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


String id = "my_channel_01";
String name="我是渠道名字";
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(this,"chat")
.setChannelId(id)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentTitle("5 new messages")
.setWhen(System.currentTimeMillis())
.setContentText("2012020047 - lks")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.fir).build();
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"subscribe")
.setContentTitle("5 new messages")
.setContentText("hahaha")
.setSmallIcon(R.drawable.fir)
.setOngoing(true);
notification = notificationBuilder.build();
}



}

public void send(View view){
notificationManager.notify(id ++, notification);
}

public void clean(View view){
for(int i = 1;i <= id ;i ++){
notificationManager.cancel(i);
}
}

}

MyRecevier.java

新建一个广播类

image-20220518110309259

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
package com.henu.a3_2;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class MyReceiver extends BroadcastReceiver {

private NotificationManager notificationManager;
private Notification notification;

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "111", Toast.LENGTH_SHORT).show();
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// throw new UnsupportedOperationException("Not yet implemented");
// Intent service = new Intent(context,MyService.class);
// context.startService(service);
String id = "my_channel_01";
String name="我是渠道名字";
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = null;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(context)
.setChannelId(id)
.setContentTitle("5 new messages")
.setWhen(System.currentTimeMillis())
.setContentText("2012020047 - lks")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.fir).build();

notificationManager.notify(1,notification);
}

}

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
<?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"
android:orientation="vertical"
android:layout_centerHorizontal="true"
tools:context=".MainActivity">

<Button
android:onClick="send"
android:text="发送广播消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:onClick="clean"
android:text="清除通知图标"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

AndroidManifest.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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.henu.a3_2">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!--接收开机的广播-->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.32">
<!--接收开机的广播-->
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

实验结果

正常运行

image-20220518110526138

开机启动后

image-20220518110540026

点击发送广播后

image-20220518110604196

实验四

实验4.1

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
package com.henu.a4_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button start1 = (Button) super.findViewById(R.id.fir);
Button end1 = (Button) super.findViewById(R.id.sec);

start1.setOnClickListener(this);
end1.setOnClickListener(this);

}

@Override
public void onClick(View v){
switch (v.getId()){
case R.id.fir:
Intent start = new Intent(this,MyService.class);
startService(start);
break;
case R.id.sec:
Intent stop = new Intent(this,MyService.class);
stopService(stop);
break;
default:
break;
}
}
}

MyService.java

新建一个Service类,选第一个service,会自动帮你在 AndroidManifest.xml 中注册好

image-20220518210756830

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
package com.henu.a4_1;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class MyService extends Service {

private static final String TAG = "MyService";
private NotificationManager notificationManager;
private Notification notification;
private boolean flag = false;
private boolean time = false;
private int id = 0;

public MyService() {
}

@RequiresApi(api = Build.VERSION_CODES.O)
// 设置一个线程,用来循环显示两个通知
Runnable notice = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void run() {
Log.d(TAG,"启动成功");
// 当time不为空,就一直循环
while(!time)
{
// 利用flag 进行两个通知之间的切换
if(!flag){
flag = true;
String id = "my_channel_01";
String name="我是渠道名字";
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification = null;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(getApplicationContext())
.setChannelId(id)
.setContentTitle("fir")
.setWhen(System.currentTimeMillis())
.setContentText("2012020047 - lks")
.setSmallIcon(R.drawable.fir).build();
notificationManager.notify(1,notification);


}else if(flag){
flag = false;
String id = "my_channel_01";
String name="我是渠道名字";
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification = null;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(getApplicationContext())
.setChannelId(id)
.setContentTitle("sec")
.setWhen(System.currentTimeMillis())
.setContentText("2012020047 - lks")
.setSmallIcon(R.drawable.time).build();
notificationManager.notify(1,notification);
}
// 设置延迟时间
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};


@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onstart ");
Thread thread = new Thread(notice);
if(!thread.isAlive()){
thread.start();
}
return super.onStartCommand(intent, flags, startId);
}

// 关闭服务
@Override
public void onDestroy() {
Log.d(TAG,"destroy");
// time = true 关闭线程
time = true;
// q
notificationManager.cancel(1);
super.onDestroy();
}

@Override
public void onCreate() {
Log.d(TAG,"oncreate ");
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
}

antivity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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"
tools:context=".MainActivity">

<Button
android:id="@+id/fir"
android:text="启动服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/sec"
android:text="关闭服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

实验结果:

点击启动之后,每个一秒更新通知栏中的通知,交替显示两个不同的图标,点击关闭服务,服务停止,通知栏图标被关闭

image-20220518211114055

image-20220518211128192

实验4.2

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
package com.henu.a4_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = "MainActivity";
private MyService.MyBinder mybinder;
// 获取服务
private MyService myService = new MyService();

private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i("MainActivity","执行了onServiceConnected(),当前Acitivity与服务连接成功");
mybinder =(MyService.MyBinder) iBinder;

}

@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i("MainActivity","执行了onServiceDisconnected(),当前Acitivity与服务断开连接");
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button fir = (Button) super.findViewById(R.id.fir);
Button sec = (Button) super.findViewById(R.id.sec);
Button thr = (Button) super.findViewById(R.id.thr);
Button fou = (Button) super.findViewById(R.id.fou);

fir.setOnClickListener(this);
sec.setOnClickListener(this);
thr.setOnClickListener(this);
fou.setOnClickListener(this);

}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.fir:
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
contant.nh = 1000;
break;
case R.id.sec:
unbindService(connection);
break;
case R.id.thr:
myService.fast();
break;
case R.id.fou:
myService.slow();
break;
default:
break;
}
}

}

MyService.java

创建一个service,方法参考上文4.1MyService的创建方法

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
package com.henu.a4_2;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class MyService extends Service {
private static final String TAG = "MyService";
private MyBinder mBinder = new MyBinder();

private NotificationManager notificationManager;
private Notification notification;
private boolean flag = false;
private boolean time = false;

// 新建一个线程
Runnable notice = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.O)
public void run() {
Log.d(TAG,"启动成功");
// 当time不为空,就一直循环
while(!time)
{
// 利用flag 进行两个通知之间的切换
if(!flag){
flag = true;
String id = "my_channel_01";
String name="我是渠道名字";
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification = null;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(getApplicationContext())
.setChannelId(id)
.setContentTitle("fir")
.setWhen(System.currentTimeMillis())
.setContentText("2012020047 - lks")
.setSmallIcon(R.drawable.fir).build();
notificationManager.notify(1,notification);
}else if(flag){
flag = false;
String id = "my_channel_01";
String name="我是渠道名字";
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification = null;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(getApplicationContext())
.setChannelId(id)
.setContentTitle("sec")
.setWhen(System.currentTimeMillis())
.setContentText("2012020047 - lks")
.setSmallIcon(R.drawable.time).build();
notificationManager.notify(1,notification);
}
// 设置延迟时间
int h = contant.nh;
Log.i(TAG,""+h);
try {
Thread.sleep(h);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};

// 用来改变contant中的nh值,从而改变thread的睡眠时间
public void fast(){
contant.nh = 333;
}
public void slow(){
contant.nh = 2000;
}


public class MyBinder extends Binder{

}


public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
Log.i("MyService","执行了onUnbind(),服务解绑成功");
return super.onUnbind(intent);
}

@Override
public void onCreate() {
Log.i("MyService","执行了onCreste(),服务创建成功");
Thread thread = new Thread(notice);
if(!thread.isAlive()){
thread.start();
}
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("MyService","执行了onStartCommand(),服务启动成功");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
time = true;
notificationManager.cancel(1);
Log.i("MyService","执行了onDestroy(),服务被销");
super.onDestroy();
}

}

contant.java

1
2
3
4
5
6
7
8
package com.henu.a4_2;

// 设置静态类,用来设置thread的sleep速率
public class contant {
public static int nh ;

}

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
<?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"
tools:context=".MainActivity">

<Button
android:id="@+id/fir"
android:text="绑定服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/sec"
android:text="取消绑定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/thr"
android:text="加速"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/fou"
android:text="减速"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>


</LinearLayout>

实验效果

image-20220519171434213

实验五

实验5.1

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
package com.example.a5_1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Database dbt; // 数据库操作
private String info; // 姓名
private Spinner textspinner;
private String getContent; // 获取下拉框的内容
private String gender1; // 获取性别
private StringBuffer sbt; // 获取爱好
private EditText name = null;
private Button tixing = null;
private RadioGroup rex;// 单选框的组
private RadioButton man,women;
private CheckBox box1 , box2 , box3, box4;

private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dbt = new Database(this,"people.db",null,1);

this.name = (EditText)super.findViewById(R.id.edit1);

man = (RadioButton) super.findViewById(R.id.man);
women = (RadioButton) super.findViewById(R.id.women);
rex = (RadioGroup) super.findViewById(R.id.Radio1);
textspinner = (Spinner) super.findViewById(R.id.chose) ;
tixing = (Button)super.findViewById((R.id.tixing));
box1 = (CheckBox)super.findViewById((R.id.box1));
box2 = (CheckBox)super.findViewById((R.id.box2));
box3 = (CheckBox)super.findViewById((R.id.box3));
box4 = (CheckBox)super.findViewById((R.id.box4));

checkBoxList.add(box1);
checkBoxList.add(box2);
checkBoxList.add(box3);
checkBoxList.add(box4);

tixing.setOnClickListener(new ShowListerer());
man.setOnClickListener(this);
women.setOnClickListener(this);
textspinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
/*public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}*/
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
getContent = MainActivity.this.getResources().getStringArray(R.array.edu)[arg2];
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.man:
gender1 = man.getText().toString().trim();
break;
case R.id.women:
gender1 = women.getText().toString().trim();
break;
}
}


private class ShowListerer implements View.OnClickListener {
public void onClick(View v)
{
sbt = new StringBuffer();
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()){
sbt.append(checkbox.getText().toString() + " ");
}
}
info = name.getText().toString();

if(info.trim().equals(""))
{
Toast.makeText(MainActivity.this,"请输入姓名",Toast.LENGTH_LONG).show();
}
else
{
add(info,gender1,getContent,sbt.toString());
Toast.makeText(MainActivity.this,"姓名是: "+info+"\n"+"性别: " +gender1+"\n"+"专业"+getContent+"\n"+"兴趣爱好 "+sbt.toString(),Toast.LENGTH_LONG).show();
clean();
}


}
}

// 对数据库进行插入操作
public void add(String name,String gender,String major,String hobby)
{
SQLiteDatabase db = dbt.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",name);
values.put("gender",gender);
values.put("major",major);
values.put("hobby",hobby);
db.insert("peopleinfo",null,values);
}

// 清楚所有的状态
public void clean(){
// 设置名字为空
name.setText("");
// 设置下拉菜单不显示

// 设置单选框不选中
rex.clearCheck();
// 设置多选框取消选中
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()){
checkbox.setChecked(false);
}
}
}
}

Database.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
// database操作
package com.example.a5_1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class Database extends SQLiteOpenHelper {

public Database(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库sql语句并执行
String sql="create table peopleinfo (id integer primary key autoincrement, name text not null, gender text, major text, hobby text )";
db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

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
<?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"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20dp"
android:layout_weight="1"/>

<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="李坤松 2012020047"
android:textSize="20dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:id="@+id/gender"
android:layout_marginLeft="10dp"
android:layout_marginTop="19dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/Radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"

android:orientation="horizontal">

<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="男"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/women"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="女"
android:textSize="20dp"/>
</RadioGroup>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="专业"
android:textSize="20dp"/>
<Spinner
android:id="@+id/chose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:prompt="@string/eduprompt"
android:entries="@array/edu"
android:spinnerMode="dialog"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="兴趣爱好"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="音乐"/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="运动"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="游泳"/>
<CheckBox
android:id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="18dp"
android:text="阅读"/>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/tixing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="提交"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="取消"/>
</LinearLayout>

</LinearLayout>

strings.xml

1
2
3
4
<resources>
<string name="app_name">5_1</string>
<string name="eduprompt">请选择你的专业</string>
</resources>

edu.xml(在res中的values中新建一个edu.xml文件)

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="edu">
<item>计算机科学与技术</item>
<item>大数据开发</item>
<item>信息安全</item>
<item>人工智能</item>
</string-array>
</resources>

实验效果

img

img

img