撸一个最简单的条件选择器picker(不联动)
来源:互联网 发布:中超数据服务商 编辑:程序博客网 时间:2024/06/01 21:30
用过各种时间选择器,地址选择器等等,但是有的时候需要一个类似身高体重,性别年龄的选择器,这种就是我下面所要说的不联动选择器,网上很多demo也有这种效果但是大多是封装完善的万能选择器,你如果抽取出来的话代码量就太多了,对于项目来说得不偿失,于是我写一个最简单的条件选择器。
思路很简单,底部我们使用一个dialog上面加上两个WheelView就可以了,看代码:
public class BottomDialog extends DialogFragment implements View.OnClickListener{ WheelView npHeight; WheelView npWeight; TextView mCancel; TextView mSure; private static final List<String> HEIGHTS = new ArrayList<String>(); private static final List<String> WEIGHTS = new ArrayList<String>(); static { for (int i=150;i<=200;i++){ HEIGHTS.add(i+"cm"); } for (int i=40;i<=150;i++){ WEIGHTS.add(i+"kg"); } } public static BottomDialog newInstance() { Bundle args = new Bundle(); BottomDialog fragment = new BottomDialog(); fragment.setArguments(args); return fragment; } @Override public void onStart() { super.onStart(); Window window = getDialog().getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.gravity = Gravity.BOTTOM; params.width = WindowManager.LayoutParams.MATCH_PARENT; window.setAttributes(params); window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); View view = inflater.inflate(R.layout.fragment_bottom_sheet,container,false); mCancel=(TextView) view.findViewById(R.id.tv_cancel); mSure=(TextView) view.findViewById(R.id.tv_sure); mCancel.setOnClickListener(this); mSure.setOnClickListener(this); npHeight=(WheelView)view.findViewById(R.id.wl_cm); npWeight=(WheelView)view.findViewById(R.id.wl_kg); initValue(); slideToUp(view); return view; } private String mHight=150+"cm"; private String mWight=40+"kg"; private void initValue() { npHeight.setOffset(1); npHeight.setItems(HEIGHTS); npHeight.setOnWheelViewListener(new WheelView.OnWheelViewListener() { @Override public void onSelected(int selectedIndex, String item) { mHight=item; } }); npWeight.setOffset(1); npWeight.setItems(WEIGHTS); npWeight.setOnWheelViewListener(new WheelView.OnWheelViewListener() { @Override public void onSelected(int selectedIndex, String item) { mWight=item; } }); } public void slideToUp(View view){ Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); slide.setDuration(400); slide.setFillAfter(true); slide.setFillEnabled(true); view.startAnimation(slide); slide.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } }); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_cancel: this.dismiss(); break; case R.id.tv_sure: EventBus.getDefault().post(new TestBean(mHight,mWight)); this.dismiss(); break; } }}
布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp"> <TextView android:id="@+id/tv_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="14dp" android:textSize="15sp" android:textColor="@color/blue" android:text="取消" /> <TextView android:id="@+id/tv_sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="14dp" android:textSize="15sp" android:textColor="@color/blue" android:text="确定" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <WheelView android:id="@+id/wl_cm" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <WheelView android:id="@+id/wl_kg" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout></LinearLayout>
实现就是这么简单O(∩_∩)O哈哈~
0 0
- 撸一个最简单的条件选择器picker(不联动)
- 一个Android图片(视频)选择器:android-media-picker
- Xcode 选择器 Picker 的应用
- 一个三级联动的地址选择器
- Jquery简单过滤选择器(应用最广泛的选择器)
- vue学习之mintui picker选择器实现省市二级联动
- vue学习之mintui picker选择器实现省市二级联动
- 自定义时间选择器的实现(非picker)
- 一个日期联动选择器
- iOS Date Picker控件的简单使用(点击一个input框,弹出)
- 一个简单的二级联动
- 最简单的两个ViewPager的联动
- 不连数据库的简单二级联动
- 微信小程序组件(七)picker选择器
- city-picker的简单使用
- 使用listview实现简单的三级地址联动选择器
- 一个简单的日期选择器
- 一个简单的图片选择器
- Python 设置系统默认编码
- HTML5之WebSocket
- eclipse3.6下webRoot目录设置说明
- ZooKeeper的安装与部署
- Hive实现从表中随机抽样得到一个不重复的数据样本
- 撸一个最简单的条件选择器picker(不联动)
- Unity MMO游戏架构设计之角色设计一
- 图片懒加载
- 【京东商城首页实战8】热词菜单
- php中关于mysqli和mysql区别的一些知识点分析
- 数据库根据时间后一条数据减去前一条数据的一种简单处理方式
- 计算机视觉需要更多几何洞察
- oracle-sql统计列m下的不重复列n的数目
- 一致性hash算法