Android自己定义NumberPicker

在项目中使用要使用滑轮来选择时间,而android系统自带的NumberPicker样式和需求不一样,而且颜色默认的是蓝色的,字体只能为黑色,上下只能显示1个,效果如下图:


	这样子满足不了我们的需求,但是大体功能还是和我们需求一样的,只是外貌不是我们想要的。我在网上看过一些资料后,和一些开源的项目,最终还是决定从numberPicker的源码下手,修改一些属性,以满足需求。经过一番修改后达到我们想要的结果,用一个小的demo作为演示,效果如下:


	点击这里下载

	大家可以看到在自定义的NumberPicker上显示的效果与原来的不一样,首先,线条不再为蓝色,可以自定义线条的颜色,中间的字体大小和颜色也可以根据自己的要求变更颜色,上下显示的个数,也可自己制定。
	首先,简单介绍一下这个demo,这是一个简单个人信息的填写的demo,隐藏原来的软键盘,下面使用到了一个PopupWindow,在PopupWindow中添加两个控件:1、自定义的NumberPicker。2、自定义的数字键盘
	接下来,先看一下我的项目结构,我在这里使用的Android Studio,会和使用Eclipse的朋友结构会有所不一样

	
	大家可以看到项目中只有3个类,NumberPicker和Scroller都是从源码中直接复制过来,只是修改了NumberPicker中的一些代码,和drawable中的一些样式,就可以达到修改NumberPicker的外观。大家使用NumberPicker时一定要将values中和drawable中的样式复制完全,不然程序会报错。
	然后介绍一下布局文件:

  
  
[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:background="#f4f5f3">  
  6.   
  7.     <include  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_width="match_parent"  
  10.         layout="@layout/title"  
  11.         android:id="@+id/Title" />  
  12.   
  13.     <ScrollView  
  14.         android:id="@+id/scrollView"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content">  
  17.   
  18.         <LinearLayout  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:background="#ffffff"  
  22.             android:focusable="true"  
  23.             android:focusableInTouchMode="true"  
  24.             android:orientation="vertical">  
  25.   
  26.             <TextView  
  27.                 android:layout_width="match_parent"  
  28.                 android:layout_height="1dp"  
  29.                 android:layout_marginTop="20dp"  
  30.                 android:background="#8033b5e5" />  
  31.   
  32.             <LinearLayout  
  33.                 android:layout_width="match_parent"  
  34.                 android:layout_height="45dp"  
  35.                 android:orientation="horizontal"  
  36.                 android:layout_marginLeft="15dp"  
  37.                 android:layout_marginRight="15dp"  
  38.                 android:gravity="center">  
  39.   
  40.                 <TextView  
  41.                     android:layout_width="wrap_content"  
  42.                     android:layout_height="match_parent"  
  43.                     android:gravity="center"  
  44.                     android:text="性别"  
  45.                     android:textSize="20sp"  
  46.                     android:textColor="#90000000" />  
  47.   
  48.                 <EditText  
  49.                     android:textCursorDrawable="@null"  
  50.                     android:id="@+id/edit_sex"  
  51.                     android:layout_width="0dp"  
  52.                     android:layout_weight="1"  
  53.                     android:layout_height="match_parent"  
  54.                     android:background="#ffffff"  
  55.                     android:text="男"  
  56.                     android:gravity="center_vertical|right"  
  57.                     android:textColor="#33b5e5"  
  58.                     android:textSize="20sp" />  
  59.             </LinearLayout>  
  60.   
  61.             <TextView  
  62.                 android:layout_width="match_parent"  
  63.                 android:layout_height="1dp"  
  64.                 android:layout_marginLeft="15dp"  
  65.                 android:layout_marginRight="15dp"  
  66.                 android:background="#8033b5e5" />  
  67.   
  68.             <LinearLayout  
  69.                 android:layout_width="match_parent"  
  70.                 android:layout_height="45dp"  
  71.                 android:orientation="horizontal"  
  72.                 android:layout_marginLeft="15dp"  
  73.                 android:layout_marginRight="15dp"  
  74.                 android:gravity="center">  
  75.   
  76.                 <TextView  
  77.                     android:layout_width="wrap_content"  
  78.                     android:layout_height="match_parent"  
  79.                     android:gravity="center"  
  80.                     android:text="出生年份"  
  81.                     android:textSize="20sp"  
  82.                     android:textColor="#90000000" />  
  83.   
  84.                 <EditText  
  85.                     android:textCursorDrawable="@null"  
  86.                     android:id="@+id/edit_birthday"  
  87.                     android:layout_width="0dp"  
  88.                     android:layout_weight="1"  
  89.                     android:layout_height="match_parent"  
  90.                     android:background="#ffffff"  
  91.                     android:text="1990"  
  92.                     android:gravity="center_vertical|right"  
  93.                     android:textColor="#33b5e5"  
  94.                     android:textSize="20sp" />  
  95.             </LinearLayout>  
  96.   
  97.             <TextView  
  98.                 android:layout_width="match_parent"  
  99.                 android:layout_height="1dp"  
  100.                 android:layout_marginLeft="15dp"  
  101.                 android:layout_marginRight="15dp"  
  102.                 android:background="#8033b5e5" />  
  103.   
  104.             <LinearLayout  
  105.                 android:layout_width="match_parent"  
  106.                 android:layout_height="45dp"  
  107.                 android:orientation="horizontal"  
  108.                 android:layout_marginLeft="15dp"  
  109.                 android:layout_marginRight="15dp"  
  110.                 android:gravity="center">  
  111.   
  112.                 <TextView  
  113.                     android:layout_width="wrap_content"  
  114.                     android:layout_height="match_parent"  
  115.                     android:gravity="center"  
  116.                     android:text="体重"  
  117.                     android:textSize="20sp"  
  118.                     android:textColor="#90000000" />  
  119.   
  120.                 <EditText  
  121.                     android:textCursorDrawable="@null"  
  122.                     android:id="@+id/edit_weight"  
  123.                     android:layout_width="0dp"  
  124.                     android:layout_weight="1"  
  125.                     android:layout_height="match_parent"  
  126.                     android:background="#ffffff"  
  127.                     android:text="50 kg"  
  128.                     android:gravity="center_vertical|right"  
  129.                     android:textColor="#33b5e5"  
  130.                     android:textSize="20sp" />  
  131.             </LinearLayout>  
  132.   
  133.             <TextView  
  134.                 android:layout_width="match_parent"  
  135.                 android:layout_height="1dp"  
  136.                 android:layout_marginLeft="15dp"  
  137.                 android:layout_marginRight="15dp"  
  138.                 android:background="#8033b5e5" />  
  139.   
  140.             <LinearLayout  
  141.                 android:layout_width="match_parent"  
  142.                 android:layout_height="45dp"  
  143.                 android:orientation="horizontal"  
  144.                 android:layout_marginLeft="15dp"  
  145.                 android:layout_marginRight="15dp"  
  146.                 android:gravity="center">  
  147.   
  148.                 <TextView  
  149.                     android:layout_width="wrap_content"  
  150.                     android:layout_height="match_parent"  
  151.                     android:gravity="center"  
  152.                     android:text="身高"  
  153.                     android:textSize="20sp"  
  154.                     android:textColor="#90000000" />  
  155.   
  156.                 <EditText  
  157.                     android:textCursorDrawable="@null"  
  158.                     android:id="@+id/edit_height"  
  159.                     android:layout_width="0dp"  
  160.                     android:layout_weight="1"  
  161.                     android:layout_height="match_parent"  
  162.                     android:background="#ffffff"  
  163.                     android:text="175 cm"  
  164.                     android:gravity="center_vertical|right"  
  165.                     android:textColor="#33b5e5"  
  166.                     android:textSize="20sp" />  
  167.             </LinearLayout>  
  168.   
  169.             <TextView  
  170.                 android:layout_width="match_parent"  
  171.                 android:layout_height="1dp"  
  172.                 android:layout_marginLeft="15dp"  
  173.                 android:layout_marginRight="15dp"  
  174.                 android:background="#8033b5e5" />  
  175.   
  176.             <LinearLayout  
  177.                 android:layout_width="match_parent"  
  178.                 android:layout_height="45dp"  
  179.                 android:orientation="horizontal"  
  180.                 android:layout_marginLeft="15dp"  
  181.                 android:layout_marginRight="15dp"  
  182.                 android:gravity="center">  
  183.   
  184.                 <TextView  
  185.                     android:layout_width="wrap_content"  
  186.                     android:layout_height="match_parent"  
  187.                     android:gravity="center"  
  188.                     android:text="步长"  
  189.                     android:textSize="20sp"  
  190.                     android:textColor="#90000000" />  
  191.   
  192.                 <EditText  
  193.                     android:textCursorDrawable="@null"  
  194.                     android:id="@+id/edit_step"  
  195.                     android:layout_width="0dp"  
  196.                     android:layout_weight="1"  
  197.                     android:layout_height="match_parent"  
  198.                     android:background="#ffffff"  
  199.                     android:text="45 cm"  
  200.                     android:gravity="center_vertical|right"  
  201.                     android:textColor="#33b5e5"  
  202.                     android:textSize="20sp" />  
  203.             </LinearLayout>  
  204.   
  205.             <TextView  
  206.                 android:layout_width="match_parent"  
  207.                 android:layout_height="1dp"  
  208.                 android:layout_marginLeft="15dp"  
  209.                 android:layout_marginRight="15dp"  
  210.                 android:background="#8033b5e5" />  
  211.   
  212.             <LinearLayout  
  213.                 android:layout_width="match_parent"  
  214.                 android:layout_height="45dp"  
  215.                 android:orientation="horizontal"  
  216.                 android:layout_marginLeft="15dp"  
  217.                 android:layout_marginRight="15dp"  
  218.                 android:gravity="center">  
  219.   
  220.                 <TextView  
  221.                     android:layout_width="wrap_content"  
  222.                     android:layout_height="match_parent"  
  223.                     android:gravity="center"  
  224.                     android:text="敏感度"  
  225.                     android:textSize="20sp"  
  226.                     android:textColor="#90000000" />  
  227.   
  228.                 <EditText  
  229.                     android:textCursorDrawable="@null"  
  230.                     android:id="@+id/edit_sensitive"  
  231.                     android:layout_width="0dp"  
  232.                     android:layout_weight="1"  
  233.                     android:layout_height="match_parent"  
  234.                     android:background="#ffffff"  
  235.                     android:text="5"  
  236.                     android:gravity="center_vertical|right"  
  237.                     android:textColor="#33b5e5"  
  238.                     android:textSize="20sp" />  
  239.             </LinearLayout>  
  240.   
  241.             <TextView  
  242.                 android:layout_width="match_parent"  
  243.                 android:layout_height="1dp"  
  244.                 android:background="#8033b5e5" />  
  245.   
  246.         </LinearLayout>  
  247.   
  248.   
  249.     </ScrollView>  
  250.   
  251.   
  252. </LinearLayout>  
接下来时PopupWindow的布局文件:

  
  
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#00f4f5f3">  
  7.   
  8.     <RelativeLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="50dp"  
  11.         android:background="#ffffff"  
  12.         android:layout_above="@+id/linearLayout"  
  13.         android:orientation="horizontal">  
  14.   
  15.         <Button  
  16.             android:id="@+id/last"  
  17.             android:layout_width="80dp"  
  18.             android:layout_height="match_parent"  
  19.             android:text="上一个"  
  20.             android:textSize="18sp"  
  21.             android:gravity="center"  
  22.             android:background="#00ffffff"  
  23.             android:textColor="#000000"  
  24.             android:clickable="true" />  
  25.   
  26.         <Button  
  27.             android:id="@+id/next"  
  28.             android:layout_width="80dp"  
  29.             android:layout_height="match_parent"  
  30.             android:layout_toRightOf="@+id/last"  
  31.             android:gravity="center"  
  32.             android:text="下一个"  
  33.             android:textSize="18sp"  
  34.             android:background="#00ffffff"  
  35.             android:textColor="#000000"  
  36.             android:clickable="true" />  
  37.   
  38.         <Button  
  39.             android:id="@+id/done"  
  40.             android:layout_width="80dp"  
  41.             android:layout_height="match_parent"  
  42.             android:layout_alignParentRight="true"  
  43.             android:gravity="center"  
  44.             android:layout_marginRight="10dp"  
  45.             android:text="完成"  
  46.             android:textSize="18sp"  
  47.             android:background="#00ffffff"  
  48.             android:textColor="#000000"  
  49.             android:clickable="true" />  
  50.     </RelativeLayout>  
  51.   
  52.     <LinearLayout  
  53.         android:id="@+id/linearLayout"  
  54.         android:layout_width="match_parent"  
  55.         android:layout_alignParentBottom="true"  
  56.         android:layout_height="285dp"  
  57.         android:orientation="vertical">  
  58.   
  59.         <com.example.liyachao.informationset.NumberPicker  
  60.             android:id="@+id/numberpicker"  
  61.             android:layout_width="match_parent"  
  62.             android:layout_height="match_parent"  
  63.             android:background="#ffffff"  
  64.             android:visibility="gone">  
  65.   
  66.         </com.example.liyachao.informationset.NumberPicker>  
  67.   
  68.         <com.example.liyachao.informationset.NumberPicker  
  69.             android:id="@+id/numberpicker1"  
  70.             android:layout_width="match_parent"  
  71.             android:layout_height="match_parent"  
  72.             android:background="#ffffff"  
  73.             android:visibility="gone">  
  74.   
  75.         </com.example.liyachao.informationset.NumberPicker>  
  76.   
  77.         <TableLayout  
  78.             android:id="@+id/tableLayout"  
  79.             android:layout_width="match_parent"  
  80.             android:layout_height="match_parent"  
  81.             android:visibility="visible">  
  82.   
  83.             <TableRow>  
  84.   
  85.                 <TextView  
  86.                     android:id="@+id/number_1"  
  87.                     android:layout_width="0dp"  
  88.                     android:layout_height="70dp"  
  89.                     android:layout_weight="1"  
  90.                     android:gravity="center"  
  91.                     android:text="1"  
  92.                     android:textColor="#808080"  
  93.                     android:background="@drawable/number_bg"  
  94.                     android:textSize="30sp" />  
  95.   
  96.                 <TextView  
  97.                     android:id="@+id/number_2"  
  98.                     android:layout_width="0dp"  
  99.                     android:layout_height="70dp"  
  100.                     android:layout_weight="1"  
  101.                     android:gravity="center"  
  102.                     android:text="2"  
  103.                     android:textColor="#808080"  
  104.                     android:background="@drawable/number_bg"  
  105.                     android:textSize="30sp" />  
  106.   
  107.                 <TextView  
  108.                     android:id="@+id/number_3"  
  109.                     android:layout_width="0dp"  
  110.                     android:layout_height="70dp"  
  111.                     android:layout_weight="1"  
  112.                     android:gravity="center"  
  113.                     android:text="3"  
  114.                     android:textColor="#808080"  
  115.                     android:background="@drawable/number_bg"  
  116.                     android:textSize="30sp" />  
  117.             </TableRow>  
  118.   
  119.             <TableRow>  
  120.   
  121.                 <TextView  
  122.                     android:id="@+id/number_4"  
  123.                     android:layout_width="0dp"  
  124.                     android:layout_height="70dp"  
  125.                     android:layout_weight="1"  
  126.                     android:gravity="center"  
  127.                     android:text="4"  
  128.                     android:textColor="#808080"  
  129.                     android:background="@drawable/number_bg"  
  130.                     android:textSize="30sp" />  
  131.   
  132.                 <TextView  
  133.                     android:id="@+id/number_5"  
  134.                     android:layout_width="0dp"  
  135.                     android:layout_height="70dp"  
  136.                     android:layout_weight="1"  
  137.                     android:gravity="center"  
  138.                     android:text="5"  
  139.                     android:textColor="#808080"  
  140.                     android:background="@drawable/number_bg"  
  141.                     android:textSize="30sp" />  
  142.   
  143.                 <TextView  
  144.                     android:id="@+id/number_6"  
  145.                     android:layout_width="0dp"  
  146.                     android:layout_height="70dp"  
  147.                     android:layout_weight="1"  
  148.                     android:gravity="center"  
  149.                     android:text="6"  
  150.                     android:textColor="#808080"  
  151.                     android:background="@drawable/number_bg"  
  152.                     android:textSize="30sp" />  
  153.             </TableRow>  
  154.   
  155.             <TableRow>  
  156.   
  157.                 <TextView  
  158.                     android:id="@+id/number_7"  
  159.                     android:layout_width="0dp"  
  160.                     android:layout_height="70dp"  
  161.                     android:layout_weight="1"  
  162.                     android:gravity="center"  
  163.                     android:text="7"  
  164.                     android:textColor="#808080"  
  165.                     android:background="@drawable/number_bg"  
  166.                     android:textSize="30sp" />  
  167.   
  168.                 <TextView  
  169.                     android:id="@+id/number_8"  
  170.                     android:layout_width="0dp"  
  171.                     android:layout_height="70dp"  
  172.                     android:layout_weight="1"  
  173.                     android:gravity="center"  
  174.                     android:text="8"  
  175.                     android:textColor="#808080"  
  176.                     android:background="@drawable/number_bg"  
  177.                     android:textSize="30sp" />  
  178.   
  179.                 <TextView  
  180.                     android:id="@+id/number_9"  
  181.                     android:layout_width="0dp"  
  182.                     android:layout_height="70dp"  
  183.                     android:layout_weight="1"  
  184.                     android:gravity="center"  
  185.                     android:text="9"  
  186.                     android:textColor="#808080"  
  187.                     android:background="@drawable/number_bg"  
  188.                     android:textSize="30sp" />  
  189.             </TableRow>  
  190.   
  191.             <TableRow>  
  192.   
  193.                 <TextView  
  194.                     android:id="@+id/number_"  
  195.                     android:layout_width="0dp"  
  196.                     android:layout_height="70dp"  
  197.                     android:layout_weight="1"  
  198.                     android:gravity="center"  
  199.                     android:text="."  
  200.                     android:textColor="#808080"  
  201.                     android:background="@drawable/number_bg"  
  202.                     android:textSize="30sp" />  
  203.   
  204.                 <TextView  
  205.                     android:id="@+id/number_0"  
  206.                     android:layout_width="0dp"  
  207.                     android:layout_height="70dp"  
  208.                     android:layout_weight="1"  
  209.                     android:gravity="center"  
  210.                     android:text="0"  
  211.                     android:textColor="#808080"  
  212.                     android:background="@drawable/number_bg"  
  213.                     android:textSize="30sp" />  
  214.   
  215.                 <TextView  
  216.                     android:id="@+id/number_reduce"  
  217.                     android:layout_height="70dp"  
  218.                     android:layout_width="0dp"  
  219.                     android:layout_weight="1"  
  220.                     android:gravity="center"  
  221.                     android:text="删除"  
  222.                     android:textColor="#808080"  
  223.                     android:background="@drawable/number_bg"  
  224.                     android:textSize="30sp" />  
  225.             </TableRow>  
  226.   
  227.         </TableLayout>  
  228.     </LinearLayout>  
  229. </RelativeLayout>  
	然后就是MainActivity的代码:

   
   
[java] view plain copy
  1. package com.example.liyachao.informationset;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.InputType;  
  6. import android.util.Log;  
  7. import android.view.Gravity;  
  8. import android.view.LayoutInflater;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.view.WindowManager;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.PopupWindow;  
  16. import android.widget.ScrollView;  
  17. import android.widget.TableLayout;  
  18. import android.widget.TextView;  
  19.   
  20. import java.lang.reflect.Method;  
  21. import java.util.ArrayList;  
  22. import java.util.List;  
  23.   
  24.   
  25. public class MainActivity extends Activity {  
  26.   
  27.     private PopupWindow pw;  
  28.     private List<EditText> editTexts;  
  29.     private View view;  
  30.     private ScrollView scrollView;  
  31.     ViewGroup.LayoutParams layoutParams;  
  32.   
  33.     private Button pw_btn_done, pw_btn_last, pw_btn_next;  
  34.     private EditText sex;  
  35.     private EditText birth;  
  36.     private EditText weight;  
  37.     private EditText height;  
  38.     private EditText step;  
  39.     private EditText sensitive;  
  40.   
  41.     private TextView one, two, three, four, five, six,  
  42.             seven, eight, nine, zero, delete, point;  
  43.     private List<TextView> number;  
  44.     private String myNumber = "";  
  45.   
  46.     private NumberPicker numberPicker, sexPicker;  
  47.     private TableLayout tableLayout;  
  48.   
  49.     private String sexStr;  
  50.     private String birthStr = "1990";  
  51.     private String weightStr = "50 kg";  
  52.     private String heightStr = "175 cm";  
  53.     private String stepStr = "35 cm";  
  54.     private String sensitiveStr = "5";  
  55.   
  56.     private String strValue[] = {sexStr, birthStr, weightStr, heightStr, stepStr, sensitiveStr};  
  57.   
  58.     private String str[] = new String[]{"男""女"};  
  59.   
  60.   
  61.     @Override  
  62.     protected void onCreate(Bundle savedInstanceState) {  
  63.         super.onCreate(savedInstanceState);  
  64.         setContentView(R.layout.activity_main);  
  65.         scrollView = (ScrollView) findViewById(R.id.scrollView);  
  66.         view = LayoutInflater.from(this).inflate(R.layout.pwpupwindow, null);  
  67.         layoutParams = scrollView.getLayoutParams();  
  68.         editTexts = new ArrayList<EditText>();  
  69.         initView();  
  70.         setPWView();  
  71.         setListener();  
  72.     }  
  73.   
  74.     /** 
  75.      * 这只监听事件 
  76.      */  
  77.     private void setListener() {  
  78.   
  79.         for (int i = 0; i < editTexts.size(); i++) {  
  80.             editTexts.get(i).setOnFocusChangeListener(new myFocusChangeListener());  
  81.         }  
  82.   
  83.         pw_btn_done.setOnClickListener(new myClickListener());  
  84.         pw_btn_next.setOnClickListener(new myClickListener());  
  85.         pw_btn_last.setOnClickListener(new myClickListener());  
  86.   
  87.         numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {  
  88.             @Override  
  89.             public void onValueChange(NumberPicker picker, int oldVal, int newVal, EditText editText) {  
  90.                 for (int i = 0; i < editTexts.size(); i++) {  
  91.                     if (editTexts.get(i).isFocused()) {  
  92.                         editTexts.get(i).setText(picker.getValue() + "");  
  93.                         editTexts.get(i).setSelection(editTexts.get(i).getText().length());  
  94.                         strValue[i] = picker.getValue() + "";  
  95.                         break;  
  96.                     }  
  97.                 }  
  98.             }  
  99.         });  
  100.         sexPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {  
  101.             @Override  
  102.             public void onValueChange(NumberPicker picker, int oldVal, int newVal, EditText editText) {  
  103.                 sex.setText(str[picker.getValue()]);  
  104.                 sex.setSelection(str[picker.getValue()].length());  
  105.                 strValue[0] = str[picker.getValue()];  
  106.             }  
  107.         });  
  108.     }  
  109.   
  110.     /** 
  111.      * 初始化popupwindow中的控件 
  112.      */  
  113.     private void setPWView() {  
  114.   
  115.         number = new ArrayList<TextView>();  
  116.         pw = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,  
  117.                 dp2px(350));  
  118.         pw_btn_done = (Button) view.findViewById(R.id.done);  
  119.         pw_btn_last = (Button) view.findViewById(R.id.last);  
  120.         pw_btn_next = (Button) view.findViewById(R.id.next);  
  121.         numberPicker = (NumberPicker) view.findViewById(R.id.numberpicker);  
  122.         tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);  
  123.   
  124.         one = (TextView) view.findViewById(R.id.number_1);  
  125.         number.add(one);  
  126.         two = (TextView) view.findViewById(R.id.number_2);  
  127.         number.add(two);  
  128.         three = (TextView) view.findViewById(R.id.number_3);  
  129.         number.add(three);  
  130.         four = (TextView) view.findViewById(R.id.number_4);  
  131.         number.add(four);  
  132.         five = (TextView) view.findViewById(R.id.number_5);  
  133.         number.add(five);  
  134.         six = (TextView) view.findViewById(R.id.number_6);  
  135.         number.add(six);  
  136.         seven = (TextView) view.findViewById(R.id.number_7);  
  137.         number.add(seven);  
  138.         eight = (TextView) view.findViewById(R.id.number_8);  
  139.         number.add(eight);  
  140.         nine = (TextView) view.findViewById(R.id.number_9);  
  141.         number.add(nine);  
  142.         zero = (TextView) view.findViewById(R.id.number_0);  
  143.         number.add(zero);  
  144.         point = (TextView) view.findViewById(R.id.number_);  
  145.         number.add(point);  
  146.         delete = (TextView) view.findViewById(R.id.number_reduce);  
  147.         number.add(delete);  
  148.   
  149.         for (int i = 0; i < number.size(); i++) {  
  150.             number.get(i).setOnClickListener(new NumberListener());  
  151.         }  
  152.   
  153.   
  154.         sexPicker = (NumberPicker) view.findViewById(R.id.numberpicker1);  
  155.         numberPicker.setWrapSelectorWheel(false);  
  156.   
  157.     }  
  158.   
  159.     /** 
  160.      * 给自定义的数字键盘设置点击事件 
  161.      */  
  162.     private class NumberListener implements View.OnClickListener {  
  163.   
  164.         @Override  
  165.         public void onClick(View v) {  
  166.             switch (v.getId()) {  
  167.                 case R.id.number_0:  
  168.                     myNumber += "0";  
  169.                     break;  
  170.                 case R.id.number_1:  
  171.                     myNumber += "1";  
  172.                     break;  
  173.                 case R.id.number_2:  
  174.                     myNumber += "2";  
  175.                     break;  
  176.                 case R.id.number_3:  
  177.                     myNumber += "3";  
  178.                     break;  
  179.                 case R.id.number_4:  
  180.                     myNumber += "4";  
  181.                     break;  
  182.                 case R.id.number_5:  
  183.                     myNumber += "5";  
  184.                     break;  
  185.                 case R.id.number_6:  
  186.                     myNumber += "6";  
  187.                     break;  
  188.                 case R.id.number_7:  
  189.                     myNumber += "7";  
  190.                     break;  
  191.                 case R.id.number_8:  
  192.                     myNumber += "8";  
  193.                     break;  
  194.                 case R.id.number_9:  
  195.                     myNumber += "9";  
  196.                     break;  
  197.                 case R.id.number_:  
  198.                     myNumber += ".";  
  199.                     break;  
  200.                 case R.id.number_reduce:  
  201.                     myNumber = myNumber.substring(0, myNumber.length() - 1);  
  202.                     break;  
  203.             }  
  204.   
  205.             for (int i = 0; i < editTexts.size(); i++) {  
  206.                 if (editTexts.get(i).isFocused()) {  
  207.                     String str = strValue[i].substring(strValue[i].length() - 3, strValue[i].length());  
  208.                     Log.i("tag", strValue[i]);  
  209.                     editTexts.get(i).setText(myNumber + str);  
  210.                     editTexts.get(i).setSelection(myNumber.length());  
  211.                     break;  
  212.                 }  
  213.             }  
  214.         }  
  215.     }  
  216.   
  217.     private class myFocusChangeListener implements View.OnFocusChangeListener {  
  218.         @Override  
  219.         public void onFocusChange(View v, boolean hasFocus) {  
  220.             switch (v.getId()) {  
  221.                 case R.id.edit_sex:  
  222.                     sexStr = sex.getText().toString().trim();  
  223.                     if (hasFocus) {  
  224.                         tableLayout.setVisibility(View.GONE);  
  225.                         sexPicker.setVisibility(View.VISIBLE);  
  226.                         numberPicker.setVisibility(View.GONE);  
  227.                         sexPicker.setWrapSelectorWheel(false);  
  228.                         sexPicker.setMaxValue(1);  
  229.                         sexPicker.setMinValue(0);  
  230.                         sexPicker.setDisplayedValues(str);  
  231.                         sex.setCursorVisible(true);  
  232.                         sex.setSelection(1);  
  233.                     } else {  
  234.                         sex.setText(str[sexPicker.getValue()]);  
  235.                     }  
  236.                     break;  
  237.                 case R.id.edit_birthday:  
  238.                     if (hasFocus) {  
  239.                         tableLayout.setVisibility(View.GONE);  
  240.                         numberPicker.setVisibility(View.VISIBLE);  
  241.                         sexPicker.setVisibility(View.GONE);  
  242.                         numberPicker.setValue(Integer.parseInt(strValue[1]));  
  243.                         numberPicker.setMaxValue(2015);  
  244.                         numberPicker.setMinValue(1970);  
  245.                         birth.setSelection(4);  
  246.   
  247.                     } else {  
  248.                         birth.setText(strValue[1]);  
  249.                     }  
  250.                     break;  
  251.                 case R.id.edit_weight:  
  252.                     if (hasFocus) {  
  253.                         tableLayout.setVisibility(View.VISIBLE);  
  254.                         numberPicker.setVisibility(View.GONE);  
  255.                         sexPicker.setVisibility(View.GONE);  
  256.                         weight.setText(" kg");  
  257.                     } else {  
  258.                         weight.setText(myNumber + " kg");  
  259.                         myNumber = "";  
  260.                     }  
  261.                     break;  
  262.                 case R.id.edit_height:  
  263.                     if (hasFocus) {  
  264.                         tableLayout.setVisibility(View.VISIBLE);  
  265.                         numberPicker.setVisibility(View.GONE);  
  266.                         sexPicker.setVisibility(View.GONE);  
  267.                         height.setText(" cm");  
  268.                     } else {  
  269.                         height.setText(myNumber + " cm");  
  270.                         myNumber = "";  
  271.                     }  
  272.                     break;  
  273.                 case R.id.edit_step:  
  274.                     if (hasFocus) {  
  275.                         tableLayout.setVisibility(View.VISIBLE);  
  276.                         numberPicker.setVisibility(View.GONE);  
  277.                         sexPicker.setVisibility(View.GONE);  
  278.                         step.setText(" cm");  
  279.                     } else {  
  280.                         step.setText(myNumber + " cm");  
  281.                         myNumber = "";  
  282.                     }  
  283.                     break;  
  284.                 case R.id.edit_sensitive:  
  285.                     if (hasFocus) {  
  286.                         tableLayout.setVisibility(View.GONE);  
  287.                         numberPicker.setVisibility(View.VISIBLE);  
  288.                         sexPicker.setVisibility(View.GONE);  
  289.   
  290.                         numberPicker.setValue(Integer.parseInt(strValue[5]));  
  291.                         numberPicker.setMaxValue(10);  
  292.                         numberPicker.setMinValue(1);  
  293.                         sensitive.setText(sensitiveStr);  
  294.                         sensitive.setSelection(sensitiveStr.length());  
  295.                     } else {  
  296.                         sensitive.setText(strValue[5]);  
  297.                     }  
  298.                     break;  
  299.             }  
  300.         }  
  301.     }  
  302.   
  303.     private class myClickListener implements View.OnClickListener {  
  304.         @Override  
  305.         public void onClick(View v) {  
  306.             switch (v.getId()) {  
  307.                 case R.id.done:  
  308.                     layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
  309.                     scrollView.setLayoutParams(layoutParams);  
  310.                     pw.dismiss();  
  311.                     break;  
  312.                 case R.id.last:  
  313.                     for (int i = 0; i < editTexts.size(); i++) {  
  314.                         if (editTexts.get(i).isFocused() && i != 0) {  
  315.                             editTexts.get(i).clearFocus();  
  316.                             editTexts.get(i - 1).requestFocus();  
  317.                             break;  
  318.                         }  
  319.                     }  
  320.                     break;  
  321.                 case R.id.next:  
  322.                     for (int i = 0; i < editTexts.size(); i++) {  
  323.                         if (editTexts.get(i).isFocused() && i != (editTexts.size() - 1)) {  
  324.                             editTexts.get(i).clearFocus();  
  325.                             editTexts.get(i + 1).requestFocus();  
  326.                             break;  
  327.                         }  
  328.                     }  
  329.   
  330.                     break;  
  331.             }  
  332.         }  
  333.     }  
  334.   
  335.   
  336.     private int dp2px(float value) {  
  337.         float v = getResources().getDisplayMetrics().density;  
  338.         return (int) (v * value + 0.5f);  
  339.     }  
  340.   
  341.   
  342.     private void initView() {  
  343.   
  344.         sex = (EditText) findViewById(R.id.edit_sex);  
  345.         birth = (EditText) findViewById(R.id.edit_birthday);  
  346.         weight = (EditText) findViewById(R.id.edit_weight);  
  347.         height = (EditText) findViewById(R.id.edit_height);  
  348.         step = (EditText) findViewById(R.id.edit_step);  
  349.         sensitive = (EditText) findViewById(R.id.edit_sensitive);  
  350.         editTexts.add(sex);  
  351.         editTexts.add(birth);  
  352.         editTexts.add(weight);  
  353.         editTexts.add(height);  
  354.         editTexts.add(step);  
  355.         editTexts.add(sensitive);  
  356.         for (int i = 0; i < editTexts.size(); i++) {  
  357.             hideSoftInputMethod(editTexts.get(i));  
  358.             editTexts.get(i).setOnTouchListener(new View.OnTouchListener() {  
  359.                 @Override  
  360.                 public boolean onTouch(View v, MotionEvent event) {  
  361.                     if (pw.isShowing()) {  
  362.                         layoutParams.height = dp2px(200);  
  363.                         scrollView.setLayoutParams(layoutParams);  
  364.                     } else {  
  365.                         pw.showAtLocation(view, Gravity.BOTTOM, 00);  
  366.                     }  
  367.                     return false;  
  368.                 }  
  369.             });  
  370.         }  
  371.     }  
  372.   
  373.   
  374.     /** 
  375.      * 一直隐藏软键盘 
  376.      * 
  377.      * @param ed 
  378.      */  
  379.     public void hideSoftInputMethod(EditText ed) {  
  380.         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  
  381.         int currentVersion = android.os.Build.VERSION.SDK_INT;  
  382.         String methodName = null;  
  383.         if (currentVersion >= 16) {  
  384.             // 4.2  
  385.             methodName = "setShowSoftInputOnFocus";  
  386.         } else if (currentVersion >= 14) {  
  387.             // 4.0  
  388.             methodName = "setSoftInputShownOnFocus";  
  389.         }  
  390.         if (methodName == null) {  
  391.             ed.setInputType(InputType.TYPE_NULL);  
  392.         } else {  
  393.             Class<EditText> cls = EditText.class;  
  394.             Method setShowSoftInputOnFocus;  
  395.             try {  
  396.                 setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);  
  397.                 setShowSoftInputOnFocus.setAccessible(true);  
  398.                 setShowSoftInputOnFocus.invoke(ed, false);  
  399.             } catch (Exception e) {  
  400.                 ed.setInputType(InputType.TYPE_NULL);  
  401.                 e.printStackTrace();  
  402.             }  
  403.         }  
  404.     }  
  405.   
  406.   
  407. }  
	NumberPicker代码太长就不贴出来了,感兴趣的朋友可以下载下来研究研究。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值