先吐槽一下 相關(guān)ui設(shè)計(jì) 一些ui設(shè)計(jì) 跟風(fēng)設(shè)計(jì)很嚴(yán)重 目前的android 設(shè)計(jì)風(fēng)格已經(jīng)出現(xiàn)好幾年了 但是仍然不入市場(chǎng) 一些產(chǎn)品 和 ui 設(shè)計(jì) 更是對(duì)ios 設(shè)計(jì)風(fēng)格喜愛(ài)無(wú)比 目前一些主流的設(shè)計(jì)風(fēng)格 甚至是資源文件都是以ios 為基數(shù)的 ?導(dǎo)致市場(chǎng)上一些app 越來(lái)越ios 風(fēng)格化 傷心五秒鐘 不過(guò)不得不承認(rèn) ios的設(shè)計(jì)風(fēng)格簡(jiǎn)約大方 確實(shí)很美觀 好了 吐槽完畢?
項(xiàng)目開(kāi)發(fā)中 如果你需要一個(gè)年月日時(shí)分秒類型時(shí)間選擇器 用DataPicker和TimePicker聯(lián)動(dòng)去實(shí)現(xiàn)得話 會(huì)非常得不方便 會(huì)碰到布局不協(xié)調(diào)得問(wèn)題 靈活性很低 可實(shí)現(xiàn)的布局也就兩種 所以為了更好的效果 個(gè)人建議你使用 numberPicker 數(shù)字選擇器 去實(shí)現(xiàn) 本人思路是 numberpicker結(jié)合PopupWindow 去實(shí)現(xiàn) 簡(jiǎn)單實(shí)現(xiàn)代碼奉上 歡迎各位大神指正
先看下效果
好 下面上代碼?
public classTimeViewActivityextendsActivity{ private Context context;
? ? private TextView tv_time_view;
? ? ArrayList options1Items = new ArrayList<>();
? ? ArrayList> options2Items = new ArrayList<>();
? ? ArrayList>> options3Items = new ArrayList<>();
? ? private String city;
? ? @Override? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_time_view);
? ? ? ? ? ? initJsonData();
? ? ? ? context = this;
? ? ? ? tv_time_view = (TextView) findViewById(R.id.tv_time_view);
? ? ? ? tv_time_view.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? // showIOSDate(tv_time_view,TimePickerView.Type.YEAR_MONTH_DAY,"選擇時(shí)間",1);? ? ? ? ? ? ? ? showPickerView();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**? ? * 時(shí)間選擇器? ? *?
@paramtextView 顯示選擇時(shí)間的控件? ??
?* @paramtype 類型? ?
?* @paramtitle 選擇器彈窗的標(biāo)題? ??
?* @paramflag 返回時(shí)間的格式? ?
?*/? ??
public void showIOSDate(final TextView textView, TimePickerView.Type type, String title, final int flag){
? ? ? ? Calendar instance = Calendar.getInstance();
? ? ? ? instance.add(Calendar.DAY_OF_MONTH,+100);// 選擇時(shí)間的間隔? ? ? ??
TimePickerView pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
@Override? ? ? ? ? ?
?public void onTimeSelect(Date date2, View v) {//選中事件回調(diào)? ? ? ? ? ? ??
? String time = getTime(date2,flag);// 返回的時(shí)間? ? ? ? ? ? ? ? t
????????extView.setText(time);
? ? ? ? ? ? }
? ? ? ? })
? ? ? ? ? ? ? ? .setType(type)//默認(rèn)全部顯示?
?? ? ? ? ? ? ? .setCancelText("取消")//取消按鈕文字
? ? ? ? ? ? ? ? .setSubmitText("確定")//確認(rèn)按鈕文字
? ? ? ? ? ? ? ? .setContentSize(20)//滾輪文字大小?
?? ? ? ? ? ? ? .setTitleSize(20)//標(biāo)題文字大小??
? ? ? ? ? ? ? .setTitleText(title)//標(biāo)題文字?
?? ? ? ? ? ? ? .setOutSideCancelable(true)//點(diǎn)擊屏幕袖订,點(diǎn)在控件外部范圍時(shí)瀑踢,是否取消顯示? ? ? ? ? ? ? ? .isCyclic(false)//是否循環(huán)滾動(dòng)?
?.setTextColorCenter(Color.BLACK)//設(shè)置選中項(xiàng)的顏色? ? ? ? ? ? ? ? .setTitleColor(getResources().getColor(R.color.textcolors))//標(biāo)題文字顏色? ? ? ? ? ? ? ? .setSubmitColor(getResources().getColor(R.color.textcolorh))//確定按鈕文字顏色? ? ? ? ? ? ? ? .setCancelColor(getResources().getColor(R.color.textcolorh))//取消按鈕文字顏色? ? ? ? ? ? ? ? .setTitleBgColor(getResources().getColor(R.color.colorbg))//標(biāo)題背景顏色 Night mode//? ? ? ? ? ? ? ? ? ? ? ? .setBgColor(0xFF333333)//滾輪背景顏色 Night mode? ? ? ? ? ? ? ? // .setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR) + 20)//默認(rèn)是1900-2100年//? ? ? ? ? ? ? ? ? ? ? ? .setDate(selectedDate)// 如果不設(shè)置的話腐芍,默認(rèn)是系統(tǒng)時(shí)間? ? ? ? ? ? ? ? //calendar.add(Calendar.DAY_OF_MONTH,-1);? ? ? ? ? ? ? ? .setRangDate(Calendar.getInstance(),instance)//起始終止年月日設(shè)定//? ? ? ? ? ? ? ? ? ? ? ? .setLabel("年","月","日","時(shí)","分","秒") ? ? ? ? ? ??
? .isCenterLabel(false) //是否只顯示中間選中項(xiàng)的label文字助隧,false則每項(xiàng)item全部都帶有l(wèi)abel乓旗。? ? ? ? ? ? ? ? .build();
? ? ? ? pvTime.setDate(Calendar.getInstance());//注:根據(jù)需求來(lái)決定是否使用該方法(一般是精確到秒的情況),此項(xiàng)可以在彈出選擇器的時(shí)候重新設(shè)置當(dāng)前時(shí)間,避免在初始化之后由于時(shí)間已經(jīng)設(shè)定,導(dǎo)致選中時(shí)間與當(dāng)前時(shí)間不匹配的問(wèn)題嘱丢。? ? ? ? pvTime.show();
? ? }
? ? /**? ? * 格式化時(shí)間? ??
?* @paramdate 時(shí)間數(shù)據(jù)源? ??
?* @paramtype 時(shí)間返回格式? ??
* @return*/? ??
public String getTime(Date date ,int type) {//可根據(jù)需要自行截取數(shù)據(jù)顯示? ? ? ? SimpleDateFormat format = null;
? ? ? ? if (type==1){
? ? ? ? ? ? format = new SimpleDateFormat("yyyy-MM-dd");
? ? ? ? }else if (type==0){
? ? ? ? ? ? format = new SimpleDateFormat("HH:mm");
? ? ? ? }else {
? ? ? ? ? ? format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
? ? ? ? }
? ? ? ? return format.format(date);
? ? }
? ? public void showPickerView() {
? ? ? ? OptionsPickerView pvOptions=new OptionsPickerView.Builder(TimeViewActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
? ? ? ? ? ? @Override? ? ? ? ? ? public void onOptionsSelect(int options1, int options2, int options3, View v) {
? ? ? ? ? ? ? ? //返回的分別是三個(gè)級(jí)別的選中位置? ? ? ? ? ? ? ? city = options1Items.get(options1).getPickerViewText() +
? ? ? ? ? ? ? ? options2Items.get(options1).get(options2) +
? ? ? ? ? ? ? ? options3Items.get(options1).get(options2).get(options3);
? ? ? ? ? ? ? ? tv_time_view.setText(city);
? ? ? ? ? ? }
? ? ? ? }).setTitleText("")
? ? ? ? ? ? ? ? .setCancelText("取消")
? ? ? ? ? ? ? ? .setSubmitText("選定")
? ? ? ? ? ? ? ? .setTextColorCenter(Color.BLACK)
? ? ? ? ? ? ? ? .setDividerColor(Color.GRAY)
? ? ? ? ? ? ? ? .setOutSideCancelable(false)
? ? ? ? ? ? ? ? .setContentTextSize(25)
? ? ? ? ? ? ? ? .build();
? ? ? ? //? pvOptions.setPicker(options1Items);//一級(jí)選擇器? ? ?
?// pvOptions.setPicker(options1Items, options2Items);//二級(jí)選擇器? ? ? ? pvOptions.setPicker(options1Items, options2Items, options3Items);//三級(jí)選擇器? ? ?
?? pvOptions.show();
? ? }
? ? private void initJsonData() {? //解析數(shù)據(jù)? ? ? ? /**
? ? ? ? * 注意:assets 目錄下的Json文件僅供參考,實(shí)際使用可自行替換文件
? ? ? ? * 關(guān)鍵邏輯在于循環(huán)體
? ? ? ? *
? ? ? ? * */? ? ? ? //? 獲取json數(shù)據(jù)? ? ? ? String JsonData = JsonFileReader.getJson(TimeViewActivity.this, "province_data.json");
? ? ? ? ArrayList jsonBean = parseData(JsonData);//用Gson 轉(zhuǎn)成實(shí)體? ? ? ? /**
? ? ? ? * 添加省份數(shù)據(jù)
? ? ? ? *
? ? ? ? * 注意:如果是添加的JavaBean實(shí)體祠饺,則實(shí)體類需要實(shí)現(xiàn) IPickerViewData 接口越驻,
? ? ? ? * PickerView會(huì)通過(guò)getPickerViewText方法獲取字符串顯示出來(lái)。
? ? ? ? */? ? ? ? options1Items = jsonBean;
? ? ? ? for (int i = 0; i < jsonBean.size(); i++) {//遍歷省份?
?? ? ? ? ? ArrayList CityList = new ArrayList<>();//該省的城市列表(第二級(jí))? ? ? ?
?? ? ArrayList> Province_AreaList = new ArrayList<>();//該省的所有地區(qū)列表(第三極)? ? ?
?? ? ? for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍歷該省份的所有城市? ? ? ? ?
?? ? ? String CityName = jsonBean.get(i).getCityList().get(c).getName();
? ? ? ? ? ? ? ? CityList.add(CityName);//添加城市? ? ? ? ? ? ?
?? ArrayList City_AreaList = new ArrayList<>();//該城市的所有地區(qū)列表? ? ? ? ?
?? ? ? //如果無(wú)地區(qū)數(shù)據(jù)道偷,建議添加空字符串缀旁,防止數(shù)據(jù)為null 導(dǎo)致三個(gè)選項(xiàng)長(zhǎng)度不匹配造成崩潰? ? ? ? ? ? ? ? if (jsonBean.get(i).getCityList().get(c).getArea() == null? ? ? ? ? ? ? ? ? ? ? ? || jsonBean.get(i).getCityList().get(c).getArea().size() == 0) {
? ? ? ? ? ? ? ? ? ? City_AreaList.add("");
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? for (int d = 0; d < jsonBean.get(i).getCityList().get(c).getArea().size(); d++) {//該城市對(duì)應(yīng)地區(qū)所有數(shù)據(jù)? ? ? ? ? ? ? ? ? ? ? ? String AreaName = jsonBean.get(i).getCityList().get(c).getArea().get(d);
? ? ? ? ? ? ? ? ? ? ? ? City_AreaList.add(AreaName);//添加該城市所有地區(qū)數(shù)據(jù)? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Province_AreaList.add(City_AreaList);//添加該省所有地區(qū)數(shù)據(jù)? ? ? ? ? ? }
? ? ? ? ? ? /**
? ? ? ? ? ? * 添加城市數(shù)據(jù)
? ? ? ? ? ? */? ? ? ? ? ? options2Items.add(CityList);
? ? ? ? ? ? /**
? ? ? ? ? ? * 添加地區(qū)數(shù)據(jù)
? ? ? ? ? ? */? ? ? ? ? ? options3Items.add(Province_AreaList);
? ? ? ? }
? ? }
? ? public ArrayList parseData(String result) {//Gson 解析? ? ? ? ArrayList detail = new ArrayList<>();
? ? ? ? try {
? ? ? ? ? ? org.json.JSONArray data = new org.json.JSONArray(result);
? ? ? ? ? ? Gson gson = new Gson();
? ? ? ? ? ? for (int i = 0; i < data.length(); i++) {
? ? ? ? ? ? ? ? JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
? ? ? ? ? ? ? ? detail.add(entity);
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? // mHandler.sendEmptyMessage(MSG_LOAD_FAILED);? ? ? ? }
? ? ? ? return detail;
? ? }
}
城市三級(jí)選擇器 需要你找一個(gè)城市資源文件 稍后我會(huì)上傳一個(gè) 有需要可以下載 我會(huì)將代碼 資源 都放進(jìn)去 需要自己復(fù)制黏貼 就可以使用了 簡(jiǎn)單實(shí)用的功能
感謝 csdn nade_s 更多內(nèi)容?
資源下載?相關(guān)資源下載