这周主要的任务是把上传的功能集合到原来的工程上面,然后就是在Android界面上实现《学生综合实践模块积分申请表》。下面我就先展示一下界面效果。
这次的任务让我体会了Spinner的作用。
一、使用Spinner相当于从下拉列表中选择项目,这是也为了解决手机屏幕小但内容广做出了很大的贡献。下面我来说一下我是如何使用Spinner的
(1)首先定义一个布局文件
(2)在程序中建立下拉框显示的数据,使用数组,这些数据将会在spinner下拉列表中进行显示
(3)接着在Activity中加入如下代码(使用了系统定义的下拉列表的布局文件,当然也可以自定义)
// 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 建立数据源
String[] mItems = getResources().getStringArray(R.array.spinnername);
// 建立Adapter并且绑定数据源
ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
//绑定 Adapter到控件
mSpinner.setAdapter(_Adapter);
(4)接着写Spinner的点击事件
1 mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
2 @Override
3 public void onItemSelected(AdapterView<?> parent, View view,
4 int position, long id) {
5 String str=parent.getItemAtPosition(position).toString();
6 Toast.makeText(SpinnerActivity.this, "你点击的是:"+str, 2000).show();
7 }
8 @Override
9 public void onNothingSelected(AdapterView<?> parent) {
10 // TODO Auto-generated method stub
11 }
12 });
这是主界面的布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="fill_parent"
3 android:layout_height="fill_parent"
4 android:orientation="vertical"
5 >
6 <RelativeLayout
7 android:layout_width="fill_parent"
8 android:layout_height="50dp"
9 android:background="#000000">
10 <TextView
11 android:layout_width="match_parent"
12 android:layout_height="wrap_content"
13 android:textSize="24sp"
14 android:layout_centerInParent="true"
15 android:layout_centerHorizontal="true"
16 android:layout_centerVertical="true"
17 android:textColor="#FFFFFF"
18 android:text="学生综合实践模块积分申请表"/>
19 </RelativeLayout>
20 <View
21 android:layout_width="match_parent"
22 android:layout_height="8dp"/>
23 <LinearLayout
24 android:layout_width="match_parent"
25 android:layout_height="wrap_content"
26 android:padding="8dp"
27 android:orientation="vertical">
28 <Spinner
29 android:id="@+id/spin_projects"
30 android:layout_width="150dp"
31 android:layout_height="wrap_content" />
32 <Spinner
33 android:id="@+id/spin_kinds"
34 android:layout_width="200dp"
35 android:layout_height="wrap_content" />
36 <Spinner
37 android:id="@+id/spin_examines"
38 android:layout_width="300dp"
39 android:layout_height="wrap_content" />
40 </LinearLayout>
41 <RelativeLayout
42 android:layout_width="match_parent"
43 android:layout_height="wrap_content" >
44 <Button
45 android:layout_width="match_parent"
46 android:layout_height="wrap_content"
47 android:layout_alignParentBottom="true"
48 android:onClick="pitcure"
50 android:text="截图"/>
51 </RelativeLayout>
52
53 </LinearLayout>
这是我为了知道界面展示的效果,点击截图的按钮会跳转到这个界面。这个界面我是采用微信上传朋友圈的样式写的一个布局
目的就是想把学生的基本信息和申请的积分一起发送给审核积分的老师。
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:padding="8dp" >
7 <RelativeLayout
8 android:layout_width="match_parent"
9 android:layout_height="50dp"
10 android:background="#000000">
11 <Button
12 android:id="@+id/upload"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:layout_alignParentRight="true"
16 android:layout_centerHorizontal="true"
17 android:layout_centerVertical="true"
18 android:textColor="#FFFFFF"
19 android:background="#000000"
20 android:textSize="18sp"
21 android:text="上传" />
22 </RelativeLayout>
23 <LinearLayout
24 android:layout_width="match_parent"
25 android:layout_height="wrap_content">
26 <ImageView
27 android:id="@+id/imageView1"
28 android:layout_width="100dp"
29 android:layout_height="100dp"
30 android:layout_alignParentLeft="true"
31 android:src="@drawable/icon_addpic_unfocused" />
32 </LinearLayout>
33 <LinearLayout
34 android:id="@+id/layout"
35 android:layout_width="match_parent"
36 android:layout_height="wrap_content"
37 android:layout_margin="10dp"
38 android:background="#FFFFFF"
39 android:orientation="vertical">
40 <RelativeLayout
41 android:id="@+id/rl_username"
42 android:layout_width="match_parent"
43 android:layout_height="wrap_content"
44 android:layout_margin="10dp">
45 <TextView
46 android:id="@+id/tv_name"
47 android:layout_width="wrap_content"
48 android:layout_height="wrap_content"
49 android:layout_centerVertical="true"
50 android:textColor="#000000"
51 android:text="姓名:"/>
52 <EditText
53 android:id="@+id/et_name"
54 android:layout_width="match_parent"
55 android:layout_height="wrap_content"
56 android:layout_marginLeft="5dp"
57 android:layout_toRightOf="@+id/tv_name"
58 android:inputType="text"
59 android:background="@null"/>
60 </RelativeLayout>
61 <View
62 android:layout_width="300dp"
63 android:layout_height="2dp"
64 android:layout_marginLeft="50dp"
65 android:background="#E6E6E6"/>
66 <RelativeLayout
67 android:id="@+id/rl_userpsd"
68 android:layout_width="match_parent"
69 android:layout_height="wrap_content"
70 android:layout_margin="10dp">
71 <TextView
72 android:id="@+id/tv_psw"
73 android:layout_width="wrap_content"
74 android:layout_height="wrap_content"
75 android:layout_centerVertical="true"
76 android:textColor="#000000"
77 android:text="学号:"/>
78 <EditText
79 android:id="@+id/et_password"
80 android:layout_width="match_parent"
81 android:layout_height="wrap_content"
82 android:layout_marginLeft="5dp"
83 android:layout_toRightOf="@+id/tv_psw"
84 android:inputType="number"
85 android:background="@null"/>
86 </RelativeLayout>
87 <View
88 android:layout_width="300dp"
89 android:layout_height="2dp"
90 android:layout_marginLeft="50dp"
91 android:background="#E6E6E6"/>
92 <RelativeLayout
93 android:id="@+id/rl_userclass"
94 android:layout_width="match_parent"
95 android:layout_height="wrap_content"
96 android:layout_margin="10dp">
97 <TextView
98 android:id="@+id/tv_class"
99 android:layout_width="wrap_content"
100 android:layout_height="wrap_content"
101 android:layout_centerVertical="true"
102 android:textColor="#000000"
103 android:text="年级:"/>
104 <EditText
105 android:id="@+id/et_password"
106 android:layout_width="match_parent"
107 android:layout_height="wrap_content"
108 android:layout_marginLeft="5dp"
109 android:layout_toRightOf="@+id/tv_class"
110 android:background="@null"/>
111 </RelativeLayout>
112 <View
113 android:layout_width="300dp"
114 android:layout_height="2dp"
115 android:layout_marginLeft="50dp"
116 android:background="#E6E6E6"/>
117 <RelativeLayout
118 android:id="@+id/rl_usermajor"
119 android:layout_width="match_parent"
120 android:layout_height="wrap_content"
121 android:layout_margin="10dp">
122 <TextView
123 android:id="@+id/tv_major"
124 android:layout_width="wrap_content"
125 android:layout_height="wrap_content"
126 android:layout_centerVertical="true"
127 android:textColor="#000000"
128 android:text="专业:"/>
129 <EditText
130 android:id="@+id/et_password"
131 android:layout_width="match_parent"
132 android:layout_height="wrap_content"
133 android:layout_marginLeft="5dp"
134 android:layout_toRightOf="@+id/tv_major"
135 android:background="@null"/>
136 </RelativeLayout>
137 <View
138 android:layout_width="300dp"
139 android:layout_height="2dp"
140 android:layout_marginLeft="50dp"
141 android:background="#E6E6E6"/>
142 <RelativeLayout
143 android:id="@+id/rl_userpsd"
144 android:layout_width="match_parent"
145 android:layout_height="wrap_content"
146 android:layout_margin="10dp">
147 <TextView
148 android:id="@+id/tv_score"
149 android:layout_width="wrap_content"
150 android:layout_height="wrap_content"
151 android:layout_centerVertical="true"
152 android:textColor="#000000"
153 android:text="积分:"/>
154 <EditText
155 android:id="@+id/et_score"
156 android:layout_width="match_parent"
157 android:layout_height="wrap_content"
158 android:layout_marginLeft="5dp"
159 android:layout_toRightOf="@+id/tv_score"
160 android:inputType="number"
161 android:background="@null"/>
162 </RelativeLayout>
163 <View
164 android:layout_width="300dp"
165 android:layout_height="2dp"
166 android:layout_marginLeft="50dp"
167 android:background="#E6E6E6"/>
168 <RelativeLayout
169 android:id="@+id/rl_usermajor"
170 android:layout_width="match_parent"
171 android:layout_height="wrap_content"
172 android:layout_margin="10dp">
173 <TextView
174 android:id="@+id/tv_date"
175 android:layout_width="wrap_content"
176 android:layout_height="wrap_content"
177 android:layout_centerVertical="true"
178 android:textColor="#000000"
179 android:text="日期:"/>
180 <EditText
181 android:id="@+id/et_date"
182 android:layout_width="match_parent"
183 android:layout_height="wrap_content"
184 android:layout_marginLeft="5dp"
185 android:layout_toRightOf="@+id/tv_date"
186 android:background="@null"/>
187 </RelativeLayout>
188 <View
189 android:layout_width="300dp"
190 android:layout_height="2dp"
191 android:layout_marginLeft="50dp"
192 android:background="#E6E6E6"/>
193 </LinearLayout>
194
195
196 </LinearLayout>
1 package com.itcast.choosepicture;
2
3 import android.os.Bundle;
4 import android.app.Activity;
5 import android.content.Intent;
6 import android.view.View;
7 import android.widget.AdapterView;
8 import android.widget.ArrayAdapter;
9 import android.widget.Spinner;
10
11 public class MainActivity extends Activity
12 {
13 private Spinner projectsSpinner = null;
14 private Spinner kindsSpinner = null;
15 private Spinner examinesSpinner = null;
16 ArrayAdapter<String> projectsAdapter = null;
17 ArrayAdapter<String> kindsAdapter = null;
18 ArrayAdapter<String> examinesAdapter = null;
19 static int provincePosition = 3;
20
21
22
23 private String[] projects = new String[] {"科研训练","素质拓展","社会实践"};
24
25 private String[][] kinds = new String[][]
26 {
27 { "项目类", "成果类", "学术活动"},
28 { "专业素质", "综合素质"},
29 { "无"}
30 };
31
32
33 private String[][][] examines = new String[][][]
34 {
35 { //科研训练
36 {"主持或参加科研项目","校大学生创新基金重点项目","校大学生创新基金一般项目"},
37 {"获奖","著作","专利","论文"},
38 {"学术交流","学术讲座","课外读书"}
39 },
40 { //素质拓展
41 {"学科竞赛(含挑战杯)","证书"},
42 {"开放实验","参加文体活动","组织活动"}
43 },
44 { //社会实践
45 {"社会实践活动","社团活动","公益劳动","自主创业"}
46 }
47 };
48
49
50 @Override
51 protected void onCreate(Bundle savedInstanceState)
52 {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.activity_main);
55 setSpinner();
56 }
57 public void pitcure(View view){
58 Intent intent = new Intent(this,WriteMainActivity.class);//这里的WriteMainActivity是我为了看第二个布局显示的效果
59 startActivity(intent);
60 }
61
62 /*
63 * 设置下拉框
64 */
65 private void setSpinner()
66 {
67 projectsSpinner = (Spinner)findViewById(R.id.spin_projects);
68 kindsSpinner = (Spinner)findViewById(R.id.spin_kinds);
69 examinesSpinner = (Spinner)findViewById(R.id.spin_examines);
70
71 //绑定适配器和值
72 projectsAdapter = new ArrayAdapter<String>(MainActivity.this,
73 android.R.layout.simple_spinner_item, projects);
74 projectsSpinner.setAdapter( projectsAdapter);
75 projectsSpinner.setSelection(2,true); //设置默认选中项,此处为默认选中第3个值
76
77 kindsAdapter = new ArrayAdapter<String>(MainActivity.this,
78 android.R.layout.simple_spinner_item, kinds[2]);
79 kindsSpinner.setAdapter(kindsAdapter);
80 kindsSpinner.setSelection(0,true); //默认选中第0个
81
82 examinesAdapter = new ArrayAdapter<String>(MainActivity.this,
83 android.R.layout.simple_spinner_item, examines[2][0]);
84 examinesSpinner.setAdapter(examinesAdapter);
85 examinesSpinner.setSelection(0, true);
86
87
88
89 projectsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
90 {
91 // 表示选项被改变的时候触发此方法,主要实现办法:动态改变地级适配器的绑定值
92 @Override
93 public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
94 {
95
96 kindsAdapter = new ArrayAdapter<String>(
97 MainActivity.this, android.R.layout.simple_spinner_item,kinds[position]);
98 kindsSpinner.setAdapter(kindsAdapter);
99 provincePosition = position; //记录当前省级序号,留给下面修改县级适配器时用
100 }
101 @Override
102 public void onNothingSelected(AdapterView<?> arg0)
103 {
104
105 }
106
107 });
108
109
110 //种类下拉监听
111 kindsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
112 {
113 @Override
114 public void onItemSelected(AdapterView<?> arg0, View arg1,
115 int position, long arg3)
116 {
117 examinesAdapter = new ArrayAdapter<String>(MainActivity.this,
118 android.R.layout.simple_spinner_item, examines[provincePosition][position]);
119 examinesSpinner.setAdapter(examinesAdapter);
120 }
121 @Override
122 public void onNothingSelected(AdapterView<?> arg0)
123 {
124
125 }
126 });
127 }
128 }