NumberPicker實現滑動選擇

NumberPicker使用,實現滑動選擇(可實現省市聯動)

最近使用了一個控件NumberPicker,該控件能夠實現省市區聯動以及其餘各類滑動選擇,這裏只是簡單的記錄下如何使用。
效果圖:
圖片描述android

先是xml文件:數組

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.why.numberpickertest.MainActivity">
   <NumberPicker
       android:layout_gravity="center"
       android:id="@+id/number_picker"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></NumberPicker>
</FrameLayout>

接下來是Activity的代碼:ide

public class MainActivity extends AppCompatActivity implements NumberPicker.Formatter{

    private NumberPicker mNumberPicker;
    private String[] mCities  = {"北京","上海","廣州","深圳","杭州","青島","西安"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme);
        setContentView(R.layout.activity_main);
        mNumberPicker = (NumberPicker)findViewById(R.id.number_picker);
        mNumberPicker.setDisplayedValues(mCities);//設置須要顯示的數組
        mNumberPicker.setMinValue(0);
        mNumberPicker.setMaxValue(mCities.length - 1);//這兩行不能缺乏,否則只能顯示第一個,關聯到format方法
        setPickerDividerColor();

        setNumberPickerTextColor(mNumberPicker,Color.RED);
    }

    //這個方法是根據index 格式化先生的文字,須要先 implements NumberPicker.Formatter
    @Override
    public String format(int value) {
        return mCities[value];
    }

    /**
     * 經過反射改變分割線顏色,
     */
    private void setPickerDividerColor() {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try{
                    pf.set(mNumberPicker,new ColorDrawable(Color.BLUE));
                }catch (IllegalAccessException e) {
                    e.printStackTrace();
                }catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                }catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 過反射改變文字的顏色
     * @param numberPicker
     * @param color
     * @return
     */
    public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color)
    {
        final int count = numberPicker.getChildCount();
        for(int i = 0; i < count; i++){
            View child = numberPicker.getChildAt(i);
            if(child instanceof EditText){
                try{
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText)child).setTextColor(color);
                    numberPicker.invalidate();
                    return true;
                }
                catch(NoSuchFieldException e){
                    Log.w("setTextColor", e);
                }
                catch(IllegalAccessException e){
                    Log.w("setTextColor", e);
                }
                catch(IllegalArgumentException e){
                    Log.w("setTextColor", e);
                }
            }
        }
        return false;
    }
}

以上就是NumberPicker的簡單使用了,能夠用它實現省市區聯動(3個NumberPicker),也能夠實現充值打折金額滑動選擇(好比充100(實付:90))等等.經過反射改變分割線的方法是在stackoverflow上看到的:
改變分割線顏色:http://stackoverflow.com/ques...
改變文字顏色:http://stackoverflow.com/ques...spa

相關文章
相關標籤/搜索