看下效果:
代码很简单..多说无益..接着看
前面俩个包如果大家写过滑轮的话应该会很熟悉的:
MainActivity代码才是重点:
package com.glacier.ui; import android.app.Activity; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.kankan.wheel.widget.OnWheelClickedListener; import android.kankan.wheel.widget.OnWheelScrollListener; import android.kankan.wheel.widget.WheelView; import android.os.Bundle; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.wheel.wheel.adapter.AbstractWheelTextAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; public class MainActivity extends Activity { /** Called when the activity is first created. */ PopupWindow distancePopupWindow; Button confirm_btn, cancel_btn; WheelView wheelView; View layView_view; EditText inputContentTxt; TextView below; String select_type = "全部"; String[] type = { "全部都是体育用材,都是运动员最爱", "篮球", "足球", "排球", "羽毛球", "综合体育", "乒乓球", "网球", "桌球", "保龄球", "游泳", "跑步", "滑雪" }; boolean[] isSelected = new boolean[13]; View distance_view; TypeTextAdapter typeTextAdapter; int id = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); below=(TextView) findViewById(R.id.below); initWheel(); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub id=0; init(); showDistancePopupWindow(); typeTextAdapter.setchange(isSelected); } }); } private void init() { for (int i = 0; i < isSelected.length; i++) { isSelected[i] = false; } } private void initWheel() { LayoutInflater mLayoutInflater = LayoutInflater.from(this); distance_view = mLayoutInflater.inflate(R.layout.type_pop_lay, null); layView_view = (View) mLayoutInflater.inflate(R.layout.type_layout, null); confirm_btn = (Button) distance_view .findViewById(R.id.distance_pop_lay_confirm); cancel_btn = (Button) distance_view .findViewById(R.id.distance_pop_lay_cancel); wheelView = (WheelView) distance_view.findViewById(R.id.type_wheel); typeTextAdapter = new TypeTextAdapter(this, wheelView); } public void showDistancePopupWindow() { distancePopupWindow = new PopupWindow(distance_view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); distance_view.setFocusableInTouchMode(true); distance_view.setBackgroundDrawable(new BitmapDrawable()); distancePopupWindow.showAtLocation( MainActivity.this.findViewById(R.id.below), Gravity.BOTTOM, 0, 0); distance_view.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (distancePopupWindow != null) { distancePopupWindow.dismiss(); wheelView.removeScrollingListener(onWheelScrollListener); wheelView.removeClickingListener(onWheelClickedListener); } break; } return false; } }); confirm_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (distancePopupWindow != null) { distancePopupWindow.dismiss(); wheelView.removeScrollingListener(onWheelScrollListener); wheelView.removeClickingListener(onWheelClickedListener); } } }); cancel_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (distancePopupWindow != null) { distancePopupWindow.dismiss(); wheelView.removeScrollingListener(onWheelScrollListener); wheelView.removeClickingListener(onWheelClickedListener); } } }); initWheel0(id); } private void initWheel0(int id) { typeTextAdapter.init(id); wheelView.setCurrentItem(id); wheelView.setViewAdapter(typeTextAdapter); } class TypeTextAdapter extends AbstractWheelTextAdapter { private WheelView wheelView; protected TypeTextAdapter(Context context, WheelView wheelView) { super(context, R.layout.type_layout, R.id.type_name, isSelected); this.wheelView = wheelView; } @Override public int getItemsCount() { return type.length; } @Override protected CharSequence getItemText(int index) { // TODO Auto-generated method stub return type[index].toString(); } @Override public View getItem(int index, View convertView, ViewGroup parent) { return super.getItem(index, convertView, parent); } private void init(int ids) { // TODO Auto-generated method stub wheelView.addScrollingListener(onWheelScrollListener); wheelView.addClickingListener(onWheelClickedListener); } } OnWheelScrollListener onWheelScrollListener=new OnWheelScrollListener(){ @Override public void onScrollingStarted(WheelView wheel) { select_type = type[wheelView.getCurrentItem()]; } @Override public void onScrollingFinished(WheelView wheel) { id = wheel.getCurrentItem(); select_type = type[wheelView.getCurrentItem()]; //把下面的打开可实现滚轮选择 // if(isSelected[id]){ // isSelected[id]=false; // }else{ // isSelected[id] = true; // } // typeTextAdapter.setchange(isSelected); // StringBuffer myBuffer = new StringBuffer(); // for (int i = 0; i < type.length; i++) { // if(isSelected[i]){ // myBuffer.append(type[i]+","); // } // } // System.out.println("当前选择了"+myBuffer); // below.setText(myBuffer); } }; OnWheelClickedListener onWheelClickedListener =new OnWheelClickedListener() { @Override public void onItemClicked(WheelView wheel, int itemIndex) { // TODO Auto-generated method stub if(isSelected[itemIndex]){ isSelected[itemIndex]=false; }else{ isSelected[itemIndex] = true; } typeTextAdapter.setchange(isSelected); StringBuffer myBuffer = new StringBuffer(); for (int i = 0; i < type.length; i++) { if(isSelected[i]){ myBuffer.append(type[i]+","); } } System.out.println("当前选择了"+myBuffer); below.setText(myBuffer); } }; }
这里主要是设置监听:
addClickingListener(onWheelClickedListener);
这里一定要记得取消的时候监听一定要
wheelView.removeClickingListener(onWheelClickedListener);
如果不remove掉的话每次进来都会多一个.是否选择的监听就会受到影响
源码下载地址:http://download.csdn.net/detail/wang_yubin/4549695