美文网首页
Dialogfragment和NumberPickerView选

Dialogfragment和NumberPickerView选

作者: 若兮生生 | 来源:发表于2016-11-24 13:12 被阅读335次

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

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。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jyj.dialogtest.MainActivity">
    <TextView
        android:id="@+id/time"
        android:text="时间选择"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1" />

    <LinearLayout
        android:id="@+id/picker_ll"
        android:layout_below="@+id/time"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <cn.carbswang.android.numberpickerview.library.NumberPickerView
            android:layout_weight="1"
            android:id="@+id/picker"
            android:layout_width="wrap_content"
            android:layout_height="240dp"
            android:layout_centerHorizontal="true"
            android:background="#11333333"
            android:contentDescription="test_number_picker_view"
            app:npv_ItemPaddingHorizontal="5dp"
            app:npv_ItemPaddingVertical="5dp"
            app:npv_ShowCount="5"
            app:npv_RespondChangeOnDetached="false"
            app:npv_TextSizeNormal="16sp"
            app:npv_TextSizeSelected="20sp"
            app:npv_WrapSelectorWheel="true"/>



        <cn.carbswang.android.numberpickerview.library.NumberPickerView
            android:layout_weight="1"
            android:id="@+id/picker2"
            android:layout_width="wrap_content"
            android:layout_height="240dp"
            android:layout_centerHorizontal="true"
            android:background="#11333333"
            android:contentDescription="test_number_picker_view"
            app:npv_ItemPaddingHorizontal="5dp"
            app:npv_ItemPaddingVertical="5dp"
            app:npv_ShowCount="5"
            app:npv_RespondChangeOnDetached="false"
            app:npv_TextSizeNormal="16sp"
            app:npv_TextSizeSelected="20sp"
            app:npv_WrapSelectorWheel="true"/>
    </LinearLayout>
<LinearLayout
    android:layout_below="@+id/picker_ll"
    android:layout_width="match_parent"
    android:layout_height="50dp">
    <Button
        android:id="@+id/cancle"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="1"
        android:text="取消"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
    <Button
        android:id="@+id/confirm"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="1"
        android:text="确定"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>

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

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

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选

      本文链接:https://www.haomeiwen.com/subject/dbybpttx.html