代碼以下:java
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)去下載, 而後添加到本身工程裏面就能夠了