-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
MainActivity.java
202 lines (172 loc) · 8.22 KB
/
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.bigkoo.pickerviewdemo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.model.IPickerViewData;
import com.bigkoo.pickerviewdemo.bean.PickerViewData;
import com.bigkoo.pickerviewdemo.bean.ProvinceBean;
public class MainActivity extends Activity {
private ArrayList<ProvinceBean> options1Items = new ArrayList<>();
private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
private ArrayList<ArrayList<ArrayList<IPickerViewData>>> options3Items = new ArrayList<>();
private TextView tvTime, tvOptions;
TimePickerView pvTime;
OptionsPickerView pvOptions;
View vMasker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vMasker=findViewById(R.id.vMasker);
tvTime=(TextView) findViewById(R.id.tvTime);
tvOptions=(TextView) findViewById(R.id.tvOptions);
//时间选择器
pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
//控制时间范围
// Calendar calendar = Calendar.getInstance();
// pvTime.setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR));//要在setTime 之前才有效果哦
pvTime.setTime(new Date());
pvTime.setCyclic(false);
pvTime.setCancelable(true);
//时间选择后回调
pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date) {
tvTime.setText(getTime(date));
}
});
//弹出时间选择器
tvTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pvTime.show();
}
});
//选项选择器
pvOptions = new OptionsPickerView(this);
//选项1
options1Items.add(new ProvinceBean(0,"广东","广东省,以岭南东道、广南东路得名","其他数据"));
options1Items.add(new ProvinceBean(1,"湖南","湖南省地处中国中部、长江中游,因大部分区域处于洞庭湖以南而得名湖南","芒果TV"));
options1Items.add(new ProvinceBean(3,"广西","嗯~~",""));
//选项2
ArrayList<String> options2Items_01=new ArrayList<>();
options2Items_01.add("广州");
options2Items_01.add("佛山");
options2Items_01.add("东莞");
options2Items_01.add("阳江");
options2Items_01.add("珠海");
ArrayList<String> options2Items_02=new ArrayList<>();
options2Items_02.add("长沙");
options2Items_02.add("岳阳");
ArrayList<String> options2Items_03=new ArrayList<>();
options2Items_03.add("桂林");
options2Items.add(options2Items_01);
options2Items.add(options2Items_02);
options2Items.add(options2Items_03);
//选项3
ArrayList<ArrayList<IPickerViewData>> options3Items_01 = new ArrayList<>();
ArrayList<ArrayList<IPickerViewData>> options3Items_02 = new ArrayList<>();
ArrayList<ArrayList<IPickerViewData>> options3Items_03 = new ArrayList<>();
ArrayList<IPickerViewData> options3Items_01_01=new ArrayList<>();
options3Items_01_01.add(new PickerViewData("天河"));
options3Items_01_01.add(new PickerViewData("黄埔"));
options3Items_01_01.add(new PickerViewData("海珠"));
options3Items_01_01.add(new PickerViewData("越秀"));
options3Items_01.add(options3Items_01_01);
ArrayList<IPickerViewData> options3Items_01_02=new ArrayList<>();
options3Items_01_02.add(new PickerViewData("南海"));
options3Items_01_02.add(new PickerViewData("明高"));
options3Items_01_02.add(new PickerViewData("禅城"));
options3Items_01_02.add(new PickerViewData("桂城"));
options3Items_01.add(options3Items_01_02);
ArrayList<IPickerViewData> options3Items_01_03=new ArrayList<>();
options3Items_01_03.add(new PickerViewData("其他"));
options3Items_01_03.add(new PickerViewData("常平"));
options3Items_01_03.add(new PickerViewData("虎门"));
options3Items_01.add(options3Items_01_03);
ArrayList<IPickerViewData> options3Items_01_04=new ArrayList<>();
options3Items_01_04.add(new PickerViewData("其他"));
options3Items_01_04.add(new PickerViewData("其他"));
options3Items_01_04.add(new PickerViewData("其他"));
options3Items_01.add(options3Items_01_04);
ArrayList<IPickerViewData> options3Items_01_05=new ArrayList<>();
options3Items_01_05.add(new PickerViewData("其他1"));
options3Items_01_05.add(new PickerViewData("其他2"));
options3Items_01.add(options3Items_01_05);
ArrayList<IPickerViewData> options3Items_02_01=new ArrayList<>();
options3Items_02_01.add(new PickerViewData("长沙1"));
options3Items_02_01.add(new PickerViewData("长沙2"));
options3Items_02_01.add(new PickerViewData("长沙3"));
options3Items_02_01.add(new PickerViewData("长沙4"));
options3Items_02_01.add(new PickerViewData("长沙5"));
options3Items_02.add(options3Items_02_01);
ArrayList<IPickerViewData> options3Items_02_02=new ArrayList<>();
options3Items_02_02.add(new PickerViewData("岳阳"));
options3Items_02_02.add(new PickerViewData("岳阳1"));
options3Items_02_02.add(new PickerViewData("岳阳2"));
options3Items_02_02.add(new PickerViewData("岳阳3"));
options3Items_02_02.add(new PickerViewData("岳阳4"));
options3Items_02_02.add(new PickerViewData("岳阳5"));
options3Items_02.add(options3Items_02_02);
ArrayList<IPickerViewData> options3Items_03_01=new ArrayList<>();
options3Items_03_01.add(new PickerViewData("好山水"));
options3Items_03.add(options3Items_03_01);
options3Items.add(options3Items_01);
options3Items.add(options3Items_02);
options3Items.add(options3Items_03);
//三级联动效果
pvOptions.setPicker(options1Items, options2Items, options3Items, true);
//设置选择的三级单位
// pwOptions.setLabels("省", "市", "区");
pvOptions.setTitle("选择城市");
pvOptions.setCyclic(false, true, true);
//设置默认选中的三级项目
//监听确定选择按钮
pvOptions.setSelectOptions(1, 1, 1);
pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int option2, int options3) {
//返回的分别是三个级别的选中位置
String tx = options1Items.get(options1).getPickerViewText()
+ options2Items.get(options1).get(option2)
+ options3Items.get(options1).get(option2).get(options3).getPickerViewText();
tvOptions.setText(tx);
vMasker.setVisibility(View.GONE);
}
});
//点击弹出选项选择器
tvOptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvOptions.show();
}
});
}
public static String getTime(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(date);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(pvOptions.isShowing()||pvTime.isShowing()){
pvOptions.dismiss();
pvTime.dismiss();
return true;
}
if(pvTime.isShowing()){
pvTime.dismiss();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}