android: WheelView组件(滑轮组件)的应用!

android前段组件中, 填表单,选择条目 的样式有很多, WheelView滚动组件为其中一种,如下图所示:

android: WheelView组件(滑轮组件)的应用!_第1张图片                                        android: WheelView组件(滑轮组件)的应用!_第2张图片

 

前两种大家很多都用过,  不过我修改了一下,弄出第三种,希望能帮助到大家:

                                        android: WheelView组件(滑轮组件)的应用!_第3张图片

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(category_str1));
			final WheelView category2 = new WheelView(Set_accountActivity.this);
			category2.setVisibleItems(5);
			category2.setCyclic(true);
			category2
					.setAdapter(new ArrayWheelAdapter(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(
							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基础类.zip)   ;  

 

所需依赖文件下载地址:

http://download.csdn.net/download/cjwdts/5681237 :WheelView依赖的java类

http://download.csdn.net/download/cjwdts/5681241:依赖的drawable文件


你可能感兴趣的:(UI)