Android WheelView(滑轮组件)使用
android前段组件中, 填表单,选择条目 的样式有很多, WheelView滚动组件为其中一种,如下图所示:
前两种大家很多都用过, 不过我修改了一下,弄出第三种,希望能帮助到大家:
代码如下:
EditText category = (EditText) findViewById(R.id.editCategory); category.setOnClickListener(listener); private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // 创建会话框 final AlertDialog dialog = new AlertDialog.Builder( Set_accountActivity.this).create(); dialog.setTitle("消费类别:"); // 创建布局 final LinearLayout ll = new LinearLayout(Set_accountActivity.this); } // 设置布局方式:水平 ll.setOrientation(LinearLayout.HORIZONTAL); final WheelView category1 = new WheelView(Set_accountActivity.this); category1.setVisibleItems(5); category1.setCyclic(true); category1.setAdapter(new ArrayWheelAdapter<String>(category_str1)); final WheelView category2 = new WheelView(Set_accountActivity.this); category2.setVisibleItems(5); category2.setCyclic(true); category2.setAdapter(new ArrayWheelAdapter<String>(category_str2[0])); // 创建参数 LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp1.gravity = Gravity.LEFT; //lp1.weight = (float) 0.6; LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lp2.weight = (float) 0.6; lp2.gravity = Gravity.RIGHT; lp2.leftMargin = 10; ll.addView(category1, lp1); ll.addView(category2, lp2); // 为category1添加监听 category1.addChangingListener(new OnWheelChangedListener() { public void onChanged(WheelView wheel, int oldValue, int newValue) { category2.setAdapter(new ArrayWheelAdapter<String>( category_str2[newValue])); category2.setCurrentItem(category_str2[newValue].length / 2); } }); // 为会话创建确定按钮 dialog.setButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String cat1 = category_str1[category1.getCurrentItem()]; String cat2 = category_str2[category1.getCurrentItem()][category2.getCurrentItem()]; category.setText(cat1 + ">>" + cat2); dialog.dismiss(); } }); dialog.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.setView(ll); dialog.show(); } }; }
public String category_str1[] = new String[] { " 餐饮 ", " 交通 ", " 购物 ", " 娱乐 ", " 医疗 ", " 教育 ", " 居家 ", " 投资 ", " 人情 " }; public String category_str2[][] = new String[][] { new String[] { " 早餐 ", " 午餐 ", " 晚餐 ", " 夜宵 ", "饮料水果", " 零食 ", "蔬菜原料", "油盐酱醋", "其他.." }, new String[] { "地铁", "公交", "打的", "加油", "停车", "过路过桥", "罚款", "包维修", "火车", "车款车贷", "车险", "航空", "船舶", "自行车", "其他.." }, new String[] { "服装鞋帽", "日用百货", "婴幼用品", "数码产品", "化妆护肤", "首饰", "烟酒", "电器", "家具", "书籍", "玩具", "摄影文印", "其他.." }, new String[] { "看电影", "KTV", "网游电玩", "运动健身", "洗浴足浴", "茶酒咖啡", "旅游度假", "演出", "其他.." }, new String[] { "求医", "买药", "体检", "化验", "医疗器材", "其他.." }, new String[] { "培训", "考试", "书籍", "学杂费", "家教", "补习", "助学贷款", "其他.." }, new String[] { "美容美发", "手机电话", "宽带", "房贷", "水电燃气", "物业", "住宿租房", "保险费", "贷款", "材料建材", "家政服务", "快递邮政", "漏记款", "其他.." }, new String[] { "证券期货", "保险", "外汇", "出资", "黄金实物", "书画艺术", "投资贷款", "利息支出", "其他.." }, new String[] { "礼金", "物品", "慈善捐款", "代付款", "其他.." }, };另外, 还需要WheelView的类, 这个可以在网上(csdn)去下载, 然后添加到自己工程里面就可以了
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。