Dialogfragment和NumberPickerView选择对话框

1.在Dialogfragment和NumberPickerView的配合下,我得到了这样的效果:

Dialogfragment和NumberPickerView选择对话框_第1张图片
Screenshot_2016-11-24-11-58-47_com.example.jyj.di.png

2.导入依赖.此依赖的github地址 https://github.com/Carbs0126/NumberPickerView/tree/master/app
compile 'cn.carbswang.android:NumberPickerView:1.1.1'
3.定义一个dialog继承fragmentDialog,并重写onCreateView方法。

      @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(STYLE_NO_TITLE);//去掉title,是为了在布局里面自定义title
        View view=inflater.inflate(R.layout.ll,null);
        ButterKnife.bind(this,view);//使用注解
        picker.setOnValueChangedListener(this);//监听选中状态

        picker.setDisplayedValues(getResources().getStringArray(R.array.minute_display));
        picker.setMaxValue(59);
        picker.setMinValue(0);
        picker.setValue(10);

        picker2.setOnValueChangedListener(this);
        String[] units = {"kg","inch"};
        picker2.setDisplayedValues(units);
        picker2.setMaxValue(units.length-1);
        picker2.setMinValue(0);
        picker2.setValue(0);
        return view;
    }    

4.看一下布局文件,里面主要是两个NumberPickerView。



    

    
        



        
    

    

5.上面第三步所需要的数组。
在Strings里面的string-array数组。

 
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
    

6.获得选中的值

    @Override
    public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
        switch (picker.getId()){
            case R.id.picker:
                Log.e("AAA","===oldVal===="+oldVal+"===newVal=="+newVal);
                this.newVal=newVal;
                break;
            case R.id.picker2:
                Log.e("AAA","===oldVal===="+oldVal+"===newVal=="+newVal);
                String newString=picker.getDisplayedValues()[newVal];
                Log.e("AAA","==保存=newString===="+newString);
        }

    }

7.在activity里面调用一下就好

    @OnClick(R.id.showDialog)
    public void showDialog(){
      new MyDialog().show(getFragmentManager(),"dialogFragment");
    }

你可能感兴趣的:(Dialogfragment和NumberPickerView选择对话框)