-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
MainActivity.java
512 lines (454 loc) · 22.8 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package com.bigkoo.pickerviewdemo;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bigkoo.pickerviewdemo.bean.CardBean;
import com.bigkoo.pickerviewdemo.bean.ProvinceBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ArrayList<ProvinceBean> options1Items = new ArrayList<>();
private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
private Button btn_Options;
private Button btn_CustomOptions;
private Button btn_CustomTime;
private TimePickerView pvTime, pvCustomTime, pvCustomLunar;
private OptionsPickerView pvOptions, pvCustomOptions, pvNoLinkOptions;
private ArrayList<CardBean> cardItem = new ArrayList<>();
private ArrayList<String> food = new ArrayList<>();
private ArrayList<String> clothes = new ArrayList<>();
private ArrayList<String> computer = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//等数据加载完毕再初始化并显示Picker,以免还未加载完数据就显示,造成APP崩溃。
getOptionData();
initTimePicker();
initCustomTimePicker();
initLunarPicker();
initOptionPicker();
initCustomOptionPicker();
initNoLinkOptionsPicker();
Button btn_Time = (Button) findViewById(R.id.btn_Time);
btn_Options = (Button) findViewById(R.id.btn_Options);
btn_CustomOptions = (Button) findViewById(R.id.btn_CustomOptions);
btn_CustomTime = (Button) findViewById(R.id.btn_CustomTime);
Button btn_no_linkage = (Button) findViewById(R.id.btn_no_linkage);
Button btn_to_Fragment = (Button) findViewById(R.id.btn_fragment);
Button btn_circle = (Button) findViewById(R.id.btn_circle);
btn_Time.setOnClickListener(this);
btn_Options.setOnClickListener(this);
btn_CustomOptions.setOnClickListener(this);
btn_CustomTime.setOnClickListener(this);
btn_no_linkage.setOnClickListener(this);
btn_to_Fragment.setOnClickListener(this);
btn_circle.setOnClickListener(this);
findViewById(R.id.btn_GotoJsonData).setOnClickListener(this);
findViewById(R.id.btn_lunar).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_Time && pvTime != null) {
// pvTime.setDate(Calendar.getInstance());
/* pvTime.show(); //show timePicker*/
pvTime.show(v);//弹出时间选择器,传递参数过去,回调的时候则可以绑定此view
} else if (v.getId() == R.id.btn_Options && pvOptions != null) {
pvOptions.show(); //弹出条件选择器
} else if (v.getId() == R.id.btn_CustomOptions && pvCustomOptions != null) {
pvCustomOptions.show(); //弹出自定义条件选择器
} else if (v.getId() == R.id.btn_CustomTime && pvCustomTime != null) {
pvCustomTime.show(); //弹出自定义时间选择器
} else if (v.getId() == R.id.btn_no_linkage && pvNoLinkOptions != null) {//不联动数据选择器
pvNoLinkOptions.show();
} else if (v.getId() == R.id.btn_GotoJsonData) {//跳转到 省市区解析示例页面
startActivity(new Intent(MainActivity.this, JsonDataActivity.class));
} else if (v.getId() == R.id.btn_fragment) {//跳转到 fragment
startActivity(new Intent(MainActivity.this, FragmentTestActivity.class));
} else if (v.getId() == R.id.btn_lunar) {
pvCustomLunar.show();
} else if (v.getId() == R.id.btn_circle) {
startActivity(new Intent(MainActivity.this, TestCircleWheelViewActivity.class));
}
}
/**
* 农历时间已扩展至 : 1900 - 2100年
*/
private void initLunarPicker() {
Calendar selectedDate = Calendar.getInstance();//系统当前时间
Calendar startDate = Calendar.getInstance();
startDate.set(2014, 1, 23);
Calendar endDate = Calendar.getInstance();
endDate.set(2069, 2, 28);
//时间选择器 ,自定义布局
pvCustomLunar = new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {//选中事件回调
Toast.makeText(MainActivity.this, getTime(date), Toast.LENGTH_SHORT).show();
}
})
.setDate(selectedDate)
.setRangDate(startDate, endDate)
.setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {
@Override
public void customLayout(final View v) {
final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
tvSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomLunar.returnData();
pvCustomLunar.dismiss();
}
});
ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomLunar.dismiss();
}
});
//公农历切换
CheckBox cb_lunar = (CheckBox) v.findViewById(R.id.cb_lunar);
cb_lunar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
pvCustomLunar.setLunarCalendar(!pvCustomLunar.isLunarCalendar());
//自适应宽
setTimePickerChildWeight(v, isChecked ? 0.8f : 1f, isChecked ? 1f : 1.1f);
}
});
}
/**
* 公农历切换后调整宽
* @param v
* @param yearWeight
* @param weight
*/
private void setTimePickerChildWeight(View v, float yearWeight, float weight) {
ViewGroup timePicker = (ViewGroup) v.findViewById(R.id.timepicker);
View year = timePicker.getChildAt(0);
LinearLayout.LayoutParams lp = ((LinearLayout.LayoutParams) year.getLayoutParams());
lp.weight = yearWeight;
year.setLayoutParams(lp);
for (int i = 1; i < timePicker.getChildCount(); i++) {
View childAt = timePicker.getChildAt(i);
LinearLayout.LayoutParams childLp = ((LinearLayout.LayoutParams) childAt.getLayoutParams());
childLp.weight = weight;
childAt.setLayoutParams(childLp);
}
}
})
.setType(new boolean[]{true, true, true, false, false, false})
.isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
.setDividerColor(Color.RED)
.build();
}
private void initTimePicker() {//Dialog 模式下,在底部弹出
pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
Toast.makeText(MainActivity.this, getTime(date), Toast.LENGTH_SHORT).show();
Log.i("pvTime", "onTimeSelect");
}
})
.setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
@Override
public void onTimeSelectChanged(Date date) {
Log.i("pvTime", "onTimeSelectChanged");
}
})
.setType(new boolean[]{true, true, true, true, true, true})
.isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。
.addOnCancelClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("pvTime", "onCancelClickListener");
}
})
.setItemVisibleCount(5) //若设置偶数,实际值会加1(比如设置6,则最大可见条目为7)
.setLineSpacingMultiplier(2.0f)
.isAlphaGradient(true)
.build();
Dialog mDialog = pvTime.getDialog();
if (mDialog != null) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM);
params.leftMargin = 0;
params.rightMargin = 0;
pvTime.getDialogContainerLayout().setLayoutParams(params);
Window dialogWindow = mDialog.getWindow();
if (dialogWindow != null) {
dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
dialogWindow.setDimAmount(0.3f);
}
}
}
private void initCustomTimePicker() {
/**
* @description
*
* 注意事项:
* 1.自定义布局中,id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有,否则会报空指针.
* 具体可参考demo 里面的两个自定义layout布局。
* 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
* setRangDate方法控制起始终止时间(如果不设置范围,则使用默认时间1900-2100年,此段代码可注释)
*/
Calendar selectedDate = Calendar.getInstance();//系统当前时间
Calendar startDate = Calendar.getInstance();
startDate.set(2014, 1, 23);
Calendar endDate = Calendar.getInstance();
endDate.set(2027, 2, 28);
//时间选择器 ,自定义布局
pvCustomTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {//选中事件回调
btn_CustomTime.setText(getTime(date));
}
})
/*.setType(TimePickerView.Type.ALL)//default is all
.setCancelText("Cancel")
.setSubmitText("Sure")
.setContentTextSize(18)
.setTitleSize(20)
.setTitleText("Title")
.setTitleColor(Color.BLACK)
/*.setDividerColor(Color.WHITE)//设置分割线的颜色
.setTextColorCenter(Color.LTGRAY)//设置选中项的颜色
.setLineSpacingMultiplier(1.6f)//设置两横线之间的间隔倍数
.setTitleBgColor(Color.DKGRAY)//标题背景颜色 Night mode
.setBgColor(Color.BLACK)//滚轮背景颜色 Night mode
.setSubmitColor(Color.WHITE)
.setCancelColor(Color.WHITE)*/
/*.animGravity(Gravity.RIGHT)// default is center*/
.setDate(selectedDate)
.setRangDate(startDate, endDate)
.setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {
@Override
public void customLayout(View v) {
final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
tvSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomTime.returnData();
pvCustomTime.dismiss();
}
});
ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomTime.dismiss();
}
});
}
})
.setContentTextSize(18)
.setType(new boolean[]{false, false, false, true, true, true})
.setLabel("年", "月", "日", "时", "分", "秒")
.setLineSpacingMultiplier(1.2f)
.setTextXOffset(0, 0, 0, 40, 0, -40)
.isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
.setDividerColor(0xFF24AD9D)
.build();
}
private void initOptionPicker() {//条件选择器初始化
/**
* 注意 :如果是三级联动的数据(省市区等),请参照 JsonDataActivity 类里面的写法。
*/
pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
//返回的分别是三个级别的选中位置
String tx = options1Items.get(options1).getPickerViewText()
+ options2Items.get(options1).get(options2)
/* + options3Items.get(options1).get(options2).get(options3).getPickerViewText()*/;
btn_Options.setText(tx);
}
})
.setTitleText("城市选择")
.setContentTextSize(20)//设置滚轮文字大小
.setDividerColor(Color.LTGRAY)//设置分割线的颜色
.setSelectOptions(0, 1)//默认选中项
.setBgColor(Color.BLACK)
.setTitleBgColor(Color.DKGRAY)
.setTitleColor(Color.LTGRAY)
.setCancelColor(Color.YELLOW)
.setSubmitColor(Color.YELLOW)
.setTextColorCenter(Color.LTGRAY)
.isRestoreItem(true)//切换时是否还原,设置默认选中第一项。
.isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
.setLabels("省", "市", "区")
.setOutSideColor(0x00000000) //设置外部遮罩颜色
.setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
@Override
public void onOptionsSelectChanged(int options1, int options2, int options3) {
String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
})
.build();
// pvOptions.setSelectOptions(1,1);
/*pvOptions.setPicker(options1Items);//一级选择器*/
pvOptions.setPicker(options1Items, options2Items);//二级选择器
/*pvOptions.setPicker(options1Items, options2Items,options3Items);//三级选择器*/
}
private void initCustomOptionPicker() {//条件选择器初始化,自定义布局
/**
* @description
*
* 注意事项:
* 自定义布局中,id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有,否则会报空指针。
* 具体可参考demo 里面的两个自定义layout布局。
*/
pvCustomOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int option2, int options3, View v) {
//返回的分别是三个级别的选中位置
String tx = cardItem.get(options1).getPickerViewText();
btn_CustomOptions.setText(tx);
}
})
.setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
@Override
public void customLayout(View v) {
final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
final TextView tvAdd = (TextView) v.findViewById(R.id.tv_add);
ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
tvSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomOptions.returnData();
pvCustomOptions.dismiss();
}
});
ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pvCustomOptions.dismiss();
}
});
tvAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCardData();
pvCustomOptions.setPicker(cardItem);
}
});
}
})
.isDialog(true)
.setOutSideCancelable(false)
.build();
pvCustomOptions.setPicker(cardItem);//添加数据
}
private void initNoLinkOptionsPicker() {// 不联动的多级选项
pvNoLinkOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
String str = "food:" + food.get(options1)
+ "\nclothes:" + clothes.get(options2)
+ "\ncomputer:" + computer.get(options3);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
})
.setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
@Override
public void onOptionsSelectChanged(int options1, int options2, int options3) {
String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
})
.setItemVisibleCount(5)
// .setSelectOptions(0, 1, 1)
.build();
pvNoLinkOptions.setNPicker(food, clothes, computer);
pvNoLinkOptions.setSelectOptions(0, 1, 1);
}
private String getTime(Date date) {//可根据需要自行截取数据显示
Log.d("getTime()", "choice date millis: " + date.getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
private void getOptionData() {
/*
* 注意:如果是添加JavaBean实体数据,则实体类需要实现 IPickerViewData 接口,
* PickerView会通过getPickerViewText方法获取字符串显示出来。
*/
getCardData();
getNoLinkData();
//选项1
options1Items.add(new ProvinceBean(0, "广东", "描述部分", "其他数据"));
options1Items.add(new ProvinceBean(1, "湖南", "描述部分", "其他数据"));
options1Items.add(new ProvinceBean(2, "广西", "描述部分", "其他数据"));
//选项2
ArrayList<String> options2Items_01 = new ArrayList<>();
options2Items_01.add("广州");
options2Items_01.add("佛山");
options2Items_01.add("东莞");
options2Items_01.add("珠海");
ArrayList<String> options2Items_02 = new ArrayList<>();
options2Items_02.add("长沙");
options2Items_02.add("岳阳");
options2Items_02.add("株洲");
options2Items_02.add("衡阳");
ArrayList<String> options2Items_03 = new ArrayList<>();
options2Items_03.add("桂林");
options2Items_03.add("玉林");
options2Items.add(options2Items_01);
options2Items.add(options2Items_02);
options2Items.add(options2Items_03);
/*--------数据源添加完毕---------*/
}
private void getCardData() {
for (int i = 0; i < 5; i++) {
cardItem.add(new CardBean(i, "No.ABC12345 " + i));
}
for (int i = 0; i < cardItem.size(); i++) {
if (cardItem.get(i).getCardNo().length() > 6) {
String str_item = cardItem.get(i).getCardNo().substring(0, 6) + "...";
cardItem.get(i).setCardNo(str_item);
}
}
}
private void getNoLinkData() {
food.add("KFC");
food.add("MacDonald");
food.add("Pizza hut");
clothes.add("Nike");
clothes.add("Adidas");
clothes.add("Armani");
computer.add("ASUS");
computer.add("Lenovo");
computer.add("Apple");
computer.add("HP");
}
}