您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)IOS 滾輪選擇控件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
代碼如下:
public class Util { /** * 時(shí)間選擇回調(diào) */ public interface TimerPickerCallBack { void onTimeSelect(String date); } /** * 彈出時(shí)間選擇 * * @param context * @param type TimerPickerView 中定義的 選擇時(shí)間類型 * @param format 時(shí)間格式化 * @param callBack 時(shí)間選擇回調(diào) */ public static void alertTimerPicker(Context context, TimePickerView.Type type, final String format, final TimerPickerCallBack callBack) { TimePickerView pvTime = new TimePickerView(context, type); //控制時(shí)間范圍 // Calendar calendar = Calendar.getInstance(); // pvTime.setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR)); pvTime.setTime(new Date()); pvTime.setCyclic(false); pvTime.setCancelable(true); //時(shí)間選擇后回調(diào) pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() { @Override public void onTimeSelect(Date date) { // tvTime.setText(getTime(date)); SimpleDateFormat sdf = new SimpleDateFormat(format); callBack.onTimeSelect(sdf.format(date)); } }); pvTime.setTextSize(16); //彈出時(shí)間選擇器 pvTime.show(); } /** * 底部滾輪點(diǎn)擊事件回調(diào) */ public interface OnWheelViewClick { void onClick(View view, int postion); } /** * 彈出底部滾輪選擇 * * @param context * @param list * @param click */ public static void alertBottomWheelOption(Context context, ArrayList<?> list, final OnWheelViewClick click) { final PopupWindow popupWindow = new PopupWindow(); View view = LayoutInflater.from(context).inflate(R.layout.layout_bottom_wheel_option, null); TextView tv_confirm = (TextView) view.findViewById(R.id.btnSubmit); final WheelView wv_option = (WheelView) view.findViewById(R.id.wv_option); wv_option.setAdapter(new ArrayWheelAdapter(list)); wv_option.setCyclic(false); wv_option.setTextSize(16); tv_confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); click.onClick(view, wv_option.getCurrentItem()); } }); view.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO: 2016/8/11 0011 取消 popupWindow.dismiss(); } }); view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { int top = view.findViewById(R.id.ll_container).getTop(); if (motionEvent.getAction() == MotionEvent.ACTION_UP) { int y = (int) motionEvent.getY(); if (y < top) { popupWindow.dismiss(); } } return true; } }); popupWindow.setContentView(view); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT); popupWindow.showAtLocation(((ViewGroup) ((Activity) context).findViewById(android.R.id.content)).getChildAt(0), Gravity.CENTER, 0, 0); } }
單項(xiàng)選擇
這里是模擬傳入 ArrayList 形式的 String 類型 :
// 單項(xiàng)選擇 for (int i = 0; i <= 10; i++) { mList.add("模擬數(shù)據(jù)" + i); } tv_single_option.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Util.alertBottomWheelOption(MainActivity.this, mList, new Util.OnWheelViewClick() { @Override public void onClick(View view, int postion) { Toast.makeText(MainActivity.this, mList.get(postion), Toast.LENGTH_SHORT).show(); } }); } });
補(bǔ)充:我們實(shí)際項(xiàng)目中用法可能是傳入一個(gè)實(shí)體對(duì)象,那么我們到 WheelView 中找到設(shè)置顯示內(nèi)容的方法:
/** * 根據(jù)傳進(jìn)來的對(duì)象反射出getPickerViewText()方法,來獲取需要顯示的值 * @param item * @return */ private String getContentText(Object item) { String contentText = item.toString(); try { Class<?> clz = item.getClass(); Method m = clz.getMethod(GETPICKERVIEWTEXT); contentText = m.invoke(item, new Object[0]).toString(); } catch (NoSuchMethodException e) { } catch (InvocationTargetException e) { } catch (IllegalAccessException e) { } catch (Exception e){ } return contentText; }
根據(jù)以上代碼,可以看到如果是一個(gè)實(shí)體對(duì)象,那么就是通過對(duì)象內(nèi)部定義的一個(gè)方法名為 GETPICKERVIEWTEXT(靜態(tài)常量=”getPickerViewText”)的返回值來作為顯示內(nèi)容,
所以在創(chuàng)建對(duì)象的時(shí)候,要注意在對(duì)象內(nèi)部添加一個(gè) getPickerViewText()方法,代碼如下:
public class TypeBean { private int id; private String name; public TypeBean(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } //這個(gè)用來顯示在PickerView上面的字符串,PickerView會(huì)通過反射獲取getPickerViewText方法顯示出來。 public String getPickerViewText() { //這里還可以判斷文字超長(zhǎng)截?cái)嘣偬峁╋@示 return name; } }
日期選擇
這里是傳入 選擇日期類型,和 回調(diào)時(shí)間格式 就能直接得到想要的結(jié)果,
@Override public void onClick(View v) { String format = ""; TimePickerView.Type type = null; switch (v.getId()) { case R.id.btn_ymdhm: type = TimePickerView.Type.ALL; format = "yyyy-MM-dd HH:mm"; break; case R.id.btn_ymdh: type = TimePickerView.Type.YEAR_MONTH_DAY_HOUR; format = "yyyy-MM-dd HH"; break; case R.id.btn_ymd: type = TimePickerView.Type.YEAR_MONTH_DAY; format = "yyyy-MM-dd"; break; case R.id.btn_mdhm: type = TimePickerView.Type.MONTH_DAY_HOUR_MIN; format = "MM-dd HH:mm"; break; case R.id.btn_hm: type = TimePickerView.Type.HOURS_MINS; format = "HH:mm"; break; case R.id.btn_ym: type = TimePickerView.Type.YEAR_MONTH; format = "yyyy-MM"; break; } Util.alertTimerPicker(this, type, format, new Util.TimerPickerCallBack() { @Override public void onTimeSelect(String date) { Toast.makeText(TestActivity.this, date, Toast.LENGTH_SHORT).show(); } }); }
條件選擇
private ArrayList<ProvinceBean> options1Items = new ArrayList<ProvinceBean>(); private ArrayList<ArrayList<String>> options2Items = new ArrayList<ArrayList<String>>(); private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<ArrayList<ArrayList<String>>>(); OptionsPickerView pvOptions; private void showOptions(){ //選項(xiàng)選擇器 pvOptions = new OptionsPickerView(this); // 初始化三個(gè)列表數(shù)據(jù) DataModel.initData(options1Items, options2Items, options3Items); //三級(jí)聯(lián)動(dòng)效果 pvOptions.setPicker(options1Items, options2Items, options3Items, true); //設(shè)置選擇的三級(jí)單位 // pwOptions.setLabels("省", "市", "區(qū)"); pvOptions.setTitle("選擇城市"); pvOptions.setCyclic(false, false, false); //設(shè)置默認(rèn)選中的三級(jí)項(xiàng)目 //監(jiān)聽確定選擇按鈕 pvOptions.setSelectOptions(1, 1, 1); pvOptions.setTextSize(18); pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() { @Override public void onOptionsSelect(int options1, int option2, int options3) { //返回的分別是三個(gè)級(jí)別的選中位置 String tx = options1Items.get(options1).getPickerViewText() + options2Items.get(options1).get(option2) + options3Items.get(options1).get(option2).get(options3); tvOptions.setText(tx); vMasker.setVisibility(View.GONE); } }); //點(diǎn)擊彈出選項(xiàng)選擇器 tvOptions.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pvOptions.show(); } }); }
看完上述內(nèi)容,你們對(duì)怎么在Android應(yīng)用中實(shí)現(xiàn)一個(gè)IOS 滾輪選擇控件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。