zoukankan      html  css  js  c++  java
  • 放弃安卓原生TimePicker,选择wheelView打造更漂亮的时间get,以及动态拉伸输入框布局,这些,这里都有!

     最近公司要求的上线项目有这么一个需求,要写一个请假申请的页面,里面必须有请假开始时间,结束时间,还有一个请假原因。

    于是想到时间选择嘛,官方不是有个DatePicker吗?额,是不是要DatePicker哦,sorry,楼主有点英文犯愁,于是去看一看安卓原生态的时间选择器,感觉还行,基本的功能都有呈现,就将就着用呗。

    没事去串了串负责开发IOS端的同事的UI,我去,这么高大上,简直就是高富帅和白富美用的嘛,再看看我的,什么玩意儿,丑的有模有样,真是有点儿意思。

    如果让同样的功能,给我们安卓端一个这么丑的玩意儿,还真的是抹黑!!折煞了我们大安卓的开源性,于是,自己写呗,咦,似乎有个叫WheelView的玩意儿,额,就是这个,在它上面下点功夫。

    额,还是先给大家带来个运行图,要是大家觉得有用,可以花个几分钟碎片时间瞧一瞧,不要钱的。看不了放心,看不了舒心!

    代码中实现了弹出动画,以及一些shape的定义。

     

    由于上面共享手机屏幕软件的原因,无法直接看到软键盘,而实际上在我们的真机上是可以直接弹出软键盘的,并且输入框的高度会随着软键盘上升且不会覆盖输入框上部布局,实际效果是这样~~

     

    额,其实实现起来很简单很简单啦,对于要使用wheelView的代码,网上搜一大堆,你也可以去楼主上传代码的github网站下载Demo自行获取。

    项目已同步至github:https://github.com/nanchen2251/DateTestDemo

    对于实现上给大家稍微讲解一下。

    首先是把必须用到的几个java代码拷贝进去,也就是我上传demo的Adapter和widget两个包。

    主页面的布局相当简单,就4个按钮。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2               xmlns:tools="http://schemas.android.com/tools"
     3               android:layout_width="match_parent"
     4               android:layout_height="match_parent"
     5               android:orientation="vertical"
     6               android:gravity="center"
     7               tools:context=".MainActivity">
     8 
     9     <Button
    10         android:id="@+id/tv_edit"
    11         style="@style/btn_bg"
    12         android:text="弹出可输入的对话框"/>
    13 
    14     <Button
    15             android:id="@+id/tv_time"
    16             style="@style/btn_bg"
    17             android:layout_marginTop="10dp"
    18             android:text="弹出时间"/>
    19 
    20     <Button
    21             android:id="@+id/tv_date"
    22             style="@style/btn_bg"
    23             android:layout_marginTop="10dp"
    24             android:text="弹出日期"/>
    25 
    26     <Button
    27             android:id="@+id/tv_date_time"
    28             style="@style/btn_bg"
    29             android:layout_marginTop="10dp"
    30             android:text="弹出日期时间"/>
    31 
    32 
    33 
    34 </LinearLayout>

    主页面的代码MainActivity.java

    在其中目前用到动画的是自定义的AlertDialog,其实代码中也实现了popWindow跳出的另一种方式,具体大家自行脑补。

    具体的代码上就相对简单啦,我相信小伙伴们一看就能明了,不过大家在开发中真的需要仔细,楼主就在开发这个的时候写了一个TimeUtils,因为写错一个参数导致调了两小时,不过当然错误没在这个Demo中发生啦,是在楼主写的项目中。

    很简单的代码逻辑,基本就是4个按钮,分别设置一个点击事件,跳转到一个自定义的AlertDialog,设置一些布局的基本用处,再通过初始化WheelView的值,额,因为在时间上每个月的天数有些不一致,在闰年平年也有不一致,所以需要写一个方法对此进行标注。额,好像没有了耶。

    代码中也注释得很清楚啦。还是直接上代码吧。

      1 package com.example.nanchen.datetest;
      2 
      3 import android.app.Activity;
      4 import android.app.AlertDialog;
      5 import android.graphics.drawable.BitmapDrawable;
      6 import android.os.Bundle;
      7 import android.view.Gravity;
      8 import android.view.LayoutInflater;
      9 import android.view.MotionEvent;
     10 import android.view.View;
     11 import android.view.View.OnClickListener;
     12 import android.view.ViewGroup.LayoutParams;
     13 import android.view.Window;
     14 import android.view.inputmethod.InputMethodManager;
     15 import android.widget.Button;
     16 import android.widget.EditText;
     17 import android.widget.LinearLayout;
     18 import android.widget.PopupWindow;
     19 import android.widget.PopupWindow.OnDismissListener;
     20 import android.widget.TextView;
     21 import android.widget.Toast;
     22 
     23 import com.example.nanchen.datetest.adapter.NumericWheelAdapter;
     24 import com.example.nanchen.datetest.widget.WheelView;
     25 
     26 import java.util.Calendar;
     27 import java.util.Locale;
     28 
     29 
     30 /**
     31  * @author  nanchen
     32  * @date 2016-08-08
     33  */
     34 public class MainActivity extends Activity{
     35     private LayoutInflater inflater = null;
     36     private WheelView year;
     37     private WheelView month;
     38     private WheelView day;
     39     private WheelView hour;
     40     private WheelView mins;
     41     
     42     PopupWindow menuWindow;
     43     
     44     Button tv_time,tv_date,popBtn;
     45     private Button btn_edit;
     46 
     47     @Override
     48     protected void onCreate(Bundle savedInstanceState) {
     49         super.onCreate(savedInstanceState);
     50         setContentView(R.layout.activity_main);
     51         inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
     52         tv_time=(Button) findViewById(R.id.tv_time);//时间选择器
     53         tv_date=(Button) findViewById(R.id.tv_date);//日期选择器
     54 
     55         popBtn = (Button) findViewById(R.id.tv_date_time);
     56 
     57         btn_edit = (Button) findViewById(R.id.tv_edit);
     58         btn_edit.setOnClickListener(new OnClickListener() {
     59             @Override
     60             public void onClick(View view) {
     61                 showPopwindow(getEditText());
     62             }
     63         });
     64 
     65         tv_time.setOnClickListener(new OnClickListener() {
     66             @Override
     67             public void onClick(View arg0) {
     68 //                showPopwindow(getTimePick());//弹出时间选择器
     69                 showTimeDialog();
     70             }
     71         });
     72         tv_date.setOnClickListener(new OnClickListener() {
     73             @Override
     74             public void onClick(View arg0) {
     75 //                showPopwindow(getDataPick());//弹出日期选择器
     76                 showDateDialog();
     77             }
     78         });
     79 
     80         popBtn.setOnClickListener(new OnClickListener() {
     81             @Override
     82             public void onClick(View view) {
     83 //                showMyNewDate(getDateAndTime());
     84                 showDateAndTime();
     85             }
     86         });
     87     }
     88 
     89 
     90 
     91     /**
     92      * 初始化popupWindow
     93      * @param view
     94      */
     95     private void showPopwindow(View view) {
     96         menuWindow = new PopupWindow(view,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
     97         menuWindow.setFocusable(true);
     98         menuWindow.setBackgroundDrawable(new BitmapDrawable());
     99         menuWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    100         menuWindow.setOnDismissListener(new OnDismissListener() {
    101             @Override
    102             public void onDismiss() {
    103                 menuWindow=null;
    104             }
    105         });
    106     }
    107 
    108     private View getEditText() {
    109         View view = inflater.inflate(R.layout.edit_layout,null);
    110         final EditText editText = (EditText) view.findViewById(R.id.editText);
    111         InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    112         manager.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);
    113         Button btn_ok = (Button) view.findViewById(R.id.btn_ok);
    114         Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
    115         btn_ok.setOnClickListener(new OnClickListener() {
    116             @Override
    117             public void onClick(View view) {
    118                 Toast.makeText(MainActivity.this,editText.getText().toString(),Toast.LENGTH_SHORT).show();
    119                 menuWindow.dismiss();
    120             }
    121         });
    122         btn_cancel.setOnClickListener(new OnClickListener() {
    123             @Override
    124             public void onClick(View view) {
    125                 menuWindow.dismiss();
    126             }
    127         });
    128         return view;
    129     }
    130 
    131     /**
    132      * 
    133      * @return
    134      */
    135     private View getTimePick() {
    136         View view = inflater.inflate(R.layout.time_picker_layout, null);
    137         hour = (WheelView) view.findViewById(R.id.hour);
    138         initHour();
    139         mins = (WheelView) view.findViewById(R.id.mins);
    140         initMins();
    141         // 设置当前时间
    142         hour.setCurrentItem(8);
    143         mins.setCurrentItem(30);
    144 
    145 
    146         hour.setVisibleItems(7);
    147         mins.setVisibleItems(7);
    148         
    149         Button bt = (Button) view.findViewById(R.id.set);
    150         bt.setOnClickListener(new OnClickListener() {
    151             @Override
    152             public void onClick(View v) {
    153                 String str = hour.getCurrentItem() + ":"+ mins.getCurrentItem();
    154                 Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
    155                 menuWindow.dismiss();
    156             }
    157         });
    158         Button cancel = (Button) view.findViewById(R.id.cancel);
    159         cancel.setOnClickListener(new OnClickListener() {
    160             @Override
    161             public void onClick(View v) {
    162                 menuWindow.dismiss();
    163             }
    164         });
    165         
    166         return view;
    167     }
    168 
    169     /**
    170      * 
    171      * @return
    172      */
    173     private View getDataPick() {
    174         Calendar c = Calendar.getInstance();
    175         int curYear = c.get(Calendar.YEAR);
    176         int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
    177         int curDate = c.get(Calendar.DATE);
    178         final View view = inflater.inflate(R.layout.datepicker_layout, null);
    179         
    180         year = (WheelView) view.findViewById(R.id.year);
    181         initYear();
    182         month = (WheelView) view.findViewById(R.id.month);
    183         initMonth();
    184         day = (WheelView) view.findViewById(R.id.day);
    185         initDay(curYear,curMonth);
    186 
    187         year.setCurrentItem(curYear - 1950);
    188         month.setCurrentItem(curMonth - 1);
    189         day.setCurrentItem(curDate - 1);
    190         year.setVisibleItems(7);
    191         month.setVisibleItems(7);
    192         day.setVisibleItems(7);
    193         
    194         Button bt = (Button) view.findViewById(R.id.set);
    195         bt.setOnClickListener(new OnClickListener() {
    196             @Override
    197             public void onClick(View v) {
    198                 String str = (year.getCurrentItem()+1950) + "-"+ (month.getCurrentItem()+1)+"-"+(day.getCurrentItem());
    199                 Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
    200                 menuWindow.dismiss();
    201             }
    202         });
    203         Button cancel = (Button) view.findViewById(R.id.cancel);
    204         cancel.setOnClickListener(new OnClickListener() {
    205             @Override
    206             public void onClick(View v) {
    207                 menuWindow.dismiss();
    208             }
    209         });
    210         return view;
    211     }
    212 
    213 
    214 
    215     /**
    216      * 
    217      * @param year
    218      * @param month
    219      * @return
    220      */
    221     private int getDay(int year, int month) {
    222         int day = 30;
    223         boolean flag = false;
    224         switch (year % 4) {
    225         case 0:
    226             flag = true;
    227             break;
    228         default:
    229             flag = false;
    230             break;
    231         }
    232         switch (month) {
    233         case 1:
    234         case 3:
    235         case 5:
    236         case 7:
    237         case 8:
    238         case 10:
    239         case 12:
    240             day = 31;
    241             break;
    242         case 2:
    243             day = flag ? 29 : 28;
    244             break;
    245         default:
    246             day = 30;
    247             break;
    248         }
    249         return day;
    250     }
    251     /**
    252      * 初始化年
    253      */
    254     private void initYear() {
    255         NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1950, 2050);
    256         numericWheelAdapter.setLabel(" 年");
    257         //        numericWheelAdapter.setTextSize(15);  设置字体大小
    258         year.setViewAdapter(numericWheelAdapter);
    259         year.setCyclic(true);
    260     }
    261 
    262     /**
    263      * 初始化月
    264      */
    265     private void initMonth() {
    266         NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1, 12, "%02d");
    267         numericWheelAdapter.setLabel(" 月");
    268         //        numericWheelAdapter.setTextSize(15);  设置字体大小
    269         month.setViewAdapter(numericWheelAdapter);
    270         month.setCyclic(true);
    271     }
    272 
    273     /**
    274      * 初始化天
    275      */
    276     private void initDay(int arg1, int arg2) {
    277         NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
    278         numericWheelAdapter.setLabel(" 日");
    279         //        numericWheelAdapter.setTextSize(15);  设置字体大小
    280         day.setViewAdapter(numericWheelAdapter);
    281         day.setCyclic(true);
    282     }
    283 
    284     /**
    285      * 初始化时
    286      */
    287     private void initHour() {
    288         NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,0, 23, "%02d");
    289         numericWheelAdapter.setLabel(" 时");
    290         //        numericWheelAdapter.setTextSize(15);  设置字体大小
    291         hour.setViewAdapter(numericWheelAdapter);
    292         hour.setCyclic(true);
    293     }
    294 
    295     /**
    296      * 初始化分
    297      */
    298     private void initMins() {
    299         NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,0, 59, "%02d");
    300         numericWheelAdapter.setLabel(" 分");
    301 //        numericWheelAdapter.setTextSize(15);  设置字体大小
    302         mins.setViewAdapter(numericWheelAdapter);
    303         mins.setCyclic(true);
    304     }
    305 
    306 
    307     /**
    308      * 显示全部日期
    309      */
    310     private void showDateAndTime(){
    311         Calendar c = Calendar.getInstance();
    312         int curYear = c.get(Calendar.YEAR);
    313         int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
    314         int curDate = c.get(Calendar.DATE);
    315         int curHour = c.get(Calendar.HOUR_OF_DAY);
    316         int curMin = c.get(Calendar.MINUTE);
    317 
    318 
    319         final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
    320                 .create();
    321         dialog.show();
    322         Window window = dialog.getWindow();
    323         // 设置布局
    324         window.setContentView(R.layout.date_time_picker_layout);
    325         // 设置宽高
    326         window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    327         // 设置弹出的动画效果
    328         window.setWindowAnimations(R.style.AnimBottom);
    329 
    330         year = (WheelView) window.findViewById(R.id.new_year);
    331         initYear();
    332         month = (WheelView) window.findViewById(R.id.new_month);
    333         initMonth();
    334         day = (WheelView) window.findViewById(R.id.new_day);
    335         initDay(curYear,curMonth);
    336         hour = (WheelView) window.findViewById(R.id.new_hour);
    337         initHour();
    338         mins = (WheelView) window.findViewById(R.id.new_mins);
    339         initMins();
    340 
    341         // 设置当前时间
    342         year.setCurrentItem(curYear - 1950);
    343         month.setCurrentItem(curMonth - 1);
    344         day.setCurrentItem(curDate - 1);
    345         hour.setCurrentItem(curHour);
    346         mins.setCurrentItem(curMin);
    347 
    348         month.setVisibleItems(7);
    349         day.setVisibleItems(7);
    350         hour.setVisibleItems(7);
    351         mins.setVisibleItems(7);
    352 
    353         // 设置监听
    354         TextView ok = (TextView) window.findViewById(R.id.set);
    355         TextView cancel = (TextView) window.findViewById(R.id.cancel);
    356         ok.setOnClickListener(new OnClickListener() {
    357             @Override
    358             public void onClick(View v) {
    359                 String time = String.format(Locale.CHINA,"%04d年%02d月%02d日 %02d时%02d分",year.getCurrentItem()+1950,
    360                         month.getCurrentItem()+1,day.getCurrentItem()+1,hour.getCurrentItem(),mins.getCurrentItem());
    361                 Toast.makeText(MainActivity.this, time, Toast.LENGTH_LONG).show();
    362                 dialog.cancel();
    363             }
    364         });
    365         cancel.setOnClickListener(new OnClickListener() {
    366             @Override
    367             public void onClick(View v) {
    368                 dialog.cancel();
    369             }
    370         });
    371         LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
    372         cancelLayout.setOnTouchListener(new View.OnTouchListener() {
    373             @Override
    374             public boolean onTouch(View view, MotionEvent motionEvent) {
    375                 dialog.cancel();
    376                 return false;
    377             }
    378         });
    379     }
    380 
    381 
    382     /**
    383      * 显示时间
    384      */
    385     private void showTimeDialog(){
    386         final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
    387                 .create();
    388         dialog.show();
    389         Window window = dialog.getWindow();
    390         // 设置布局
    391         window.setContentView(R.layout.time_picker_layout);
    392         // 设置宽高
    393         window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    394         // 设置弹出的动画效果
    395         window.setWindowAnimations(R.style.AnimBottom);
    396 
    397         Calendar c = Calendar.getInstance();
    398         int curHour = c.get(Calendar.HOUR_OF_DAY);
    399         int curMin = c.get(Calendar.MINUTE);
    400 
    401 
    402         hour = (WheelView) window.findViewById(R.id.hour);
    403         initHour();
    404         mins = (WheelView) window.findViewById(R.id.mins);
    405         initMins();
    406         // 设置当前时间
    407         hour.setCurrentItem(curHour);
    408         mins.setCurrentItem(curMin);
    409 
    410 
    411         hour.setVisibleItems(7);
    412         mins.setVisibleItems(7);
    413 
    414         // 设置监听
    415         Button ok = (Button) window.findViewById(R.id.set);
    416         Button cancel = (Button) window.findViewById(R.id.cancel);
    417         ok.setOnClickListener(new OnClickListener() {
    418             @Override
    419             public void onClick(View v) {
    420                 // TODO Auto-generated method stub
    421                 String str = String.format(Locale.CHINA,"%2d:%2d",hour.getCurrentItem(), mins.getCurrentItem());
    422                 Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
    423                 dialog.cancel();
    424             }
    425         });
    426         cancel.setOnClickListener(new OnClickListener() {
    427             @Override
    428             public void onClick(View v) {
    429                 // TODO Auto-generated method stub
    430                 dialog.cancel();
    431             }
    432         });
    433         LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
    434         cancelLayout.setOnTouchListener(new View.OnTouchListener() {
    435             @Override
    436             public boolean onTouch(View view, MotionEvent motionEvent) {
    437                 dialog.cancel();
    438                 return false;
    439             }
    440         });
    441     }
    442 
    443 
    444     /**
    445      * 显示日期
    446      */
    447     private void showDateDialog() {
    448         final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
    449                 .create();
    450         dialog.show();
    451         Window window = dialog.getWindow();
    452         // 设置布局
    453         window.setContentView(R.layout.datepicker_layout);
    454         // 设置宽高
    455         window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    456         // 设置弹出的动画效果
    457         window.setWindowAnimations(R.style.AnimBottom);
    458 
    459 
    460         Calendar c = Calendar.getInstance();
    461         int curYear = c.get(Calendar.YEAR);
    462         int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
    463         int curDate = c.get(Calendar.DATE);
    464         year = (WheelView) window.findViewById(R.id.year);
    465         initYear();
    466         month = (WheelView) window.findViewById(R.id.month);
    467         initMonth();
    468         day = (WheelView) window.findViewById(R.id.day);
    469         initDay(curYear,curMonth);
    470 
    471 
    472         year.setCurrentItem(curYear - 1950);
    473         month.setCurrentItem(curMonth - 1);
    474         day.setCurrentItem(curDate - 1);
    475         year.setVisibleItems(7);
    476         month.setVisibleItems(7);
    477         day.setVisibleItems(7);
    478 
    479         // 设置监听
    480         Button ok = (Button) window.findViewById(R.id.set);
    481         Button cancel = (Button) window.findViewById(R.id.cancel);
    482         ok.setOnClickListener(new OnClickListener() {
    483             @Override
    484             public void onClick(View v) {
    485                 String str = String.format(Locale.CHINA,"%4d年%2d月%2d日",year.getCurrentItem()+1950,month.getCurrentItem()+1,day.getCurrentItem()+1);
    486                 Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
    487                 dialog.cancel();
    488             }
    489         });
    490         cancel.setOnClickListener(new OnClickListener() {
    491             @Override
    492             public void onClick(View v) {
    493                 dialog.cancel();
    494             }
    495         });
    496         LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
    497         cancelLayout.setOnTouchListener(new View.OnTouchListener() {
    498             @Override
    499             public boolean onTouch(View view, MotionEvent motionEvent) {
    500                 dialog.cancel();
    501                 return false;
    502             }
    503         });
    504 
    505     }
    506 
    507 }

    另外的几个小布局也意义奉上。

    date_time_picker_layout.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3               android:layout_width="match_parent"
     4               android:layout_height="match_parent"
     5               android:orientation="vertical">
     6 
     7     <LinearLayout
     8         android:id="@+id/view_none"
     9         android:layout_width="match_parent"
    10         android:layout_height="0dp"
    11         android:layout_weight="1"
    12         android:orientation="vertical"
    13         >
    14     </LinearLayout>
    15 
    16     <LinearLayout
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content"
    19         android:background="#ffffff"
    20         android:orientation="vertical">
    21 
    22         <RelativeLayout
    23             android:layout_width="match_parent"
    24             android:layout_height="40dp">
    25 
    26             <TextView
    27                 android:id="@+id/cancel"
    28                 android:layout_width="120dp"
    29                 android:layout_height="match_parent"
    30                 android:background="#fff"
    31                 android:gravity="center"
    32                 android:text="取消"
    33                 android:textColor="#1298FF"/>
    34 
    35 
    36             <TextView
    37                 android:id="@+id/set"
    38                 android:layout_width="120dp"
    39                 android:layout_height="match_parent"
    40                 android:layout_alignParentRight="true"
    41                 android:background="#fff"
    42                 android:gravity="center"
    43                 android:text="确定"
    44                 android:textColor="#1298FF"/>
    45         </RelativeLayout>
    46 
    47         <LinearLayout
    48             android:layout_width="fill_parent"
    49             android:layout_height="wrap_content"
    50             android:background="#fff"
    51             android:orientation="horizontal"
    52             android:paddingBottom="10dp">
    53 
    54             <com.example.nanchen.datetest.widget.WheelView
    55                 android:id="@+id/new_year"
    56                 android:layout_width="fill_parent"
    57                 android:layout_height="wrap_content"
    58                 android:layout_marginBottom="10dip"
    59                 android:layout_weight="0.9"/>
    60 
    61             <com.example.nanchen.datetest.widget.WheelView
    62                 android:id="@+id/new_month"
    63                 android:layout_width="fill_parent"
    64                 android:layout_height="wrap_content"
    65                 android:layout_marginBottom="10dip"
    66                 android:layout_weight="1"/>
    67 
    68             <com.example.nanchen.datetest.widget.WheelView
    69                 android:id="@+id/new_day"
    70                 android:layout_width="fill_parent"
    71                 android:layout_height="wrap_content"
    72                 android:layout_marginBottom="10dip"
    73                 android:layout_weight="1"/>
    74 
    75             <com.example.nanchen.datetest.widget.WheelView
    76                 android:id="@+id/new_hour"
    77                 android:layout_width="fill_parent"
    78                 android:layout_height="wrap_content"
    79                 android:layout_marginBottom="10dip"
    80                 android:layout_weight="1"/>
    81 
    82             <com.example.nanchen.datetest.widget.WheelView
    83                 android:id="@+id/new_mins"
    84                 android:layout_width="fill_parent"
    85                 android:layout_height="wrap_content"
    86                 android:layout_marginBottom="10dip"
    87                 android:layout_marginRight="5dip"
    88                 android:layout_weight="1"/>
    89         </LinearLayout>
    90 
    91     </LinearLayout>
    92 
    93 
    94 </LinearLayout>

    datepicker_layout.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3               android:layout_width="fill_parent"
     4               android:layout_height="match_parent"
     5               android:orientation="vertical">
     6 
     7     <LinearLayout
     8             android:id="@+id/view_none"
     9             android:layout_width="match_parent"
    10             android:layout_height="match_parent"
    11             android:layout_weight="1"
    12             android:orientation="vertical"
    13             >
    14     </LinearLayout>
    15 
    16     <LinearLayout
    17             android:layout_width="fill_parent"
    18             android:layout_height="wrap_content"
    19             android:background="#00000000"
    20             android:gravity="center"
    21             android:orientation="vertical">
    22 
    23         <RelativeLayout
    24                 android:layout_width="fill_parent"
    25                 android:layout_height="40dp"
    26                 android:background="#fff"
    27                 android:orientation="horizontal">
    28 
    29             <Button
    30                     android:id="@+id/cancel"
    31                     android:layout_width="120dp"
    32                     android:layout_height="fill_parent"
    33                     android:background="@drawable/dialog_btn_right_selector"
    34                     android:text="取消"
    35                     android:textColor="#5C5D5C"/>
    36             <Button
    37                     android:id="@+id/set"
    38                     android:layout_width="120dp"
    39                     android:layout_height="fill_parent"
    40                     android:background="@drawable/dialog_btn_left_selector"
    41                     android:text="确定"
    42                     android:layout_alignParentRight="true"
    43                     android:textColor="#1298FF"/>
    44         </RelativeLayout>
    45 
    46         <LinearLayout
    47                 android:layout_width="fill_parent"
    48                 android:layout_height="wrap_content"
    49                 android:background="#fff"
    50                 android:paddingBottom="20dp"
    51                 android:paddingTop="10dp"
    52                 android:orientation="horizontal">
    53 
    54             <com.example.nanchen.datetest.widget.WheelView
    55                     android:id="@+id/year"
    56                     android:layout_width="fill_parent"
    57                     android:layout_height="wrap_content"
    58                     android:layout_marginBottom="10dip"
    59                     android:layout_marginLeft="5dip"
    60                     android:layout_marginTop="10dip"
    61                     android:layout_weight="0.8"/>
    62 
    63             <com.example.nanchen.datetest.widget.WheelView
    64                     android:id="@+id/month"
    65                     android:layout_width="fill_parent"
    66                     android:layout_height="wrap_content"
    67                     android:layout_marginBottom="10dip"
    68                     android:layout_marginTop="10dip"
    69                     android:layout_weight="1"/>
    70 
    71             <com.example.nanchen.datetest.widget.WheelView
    72                     android:id="@+id/day"
    73                     android:layout_width="fill_parent"
    74                     android:layout_height="wrap_content"
    75                     android:layout_marginBottom="10dip"
    76                     android:layout_marginRight="5dip"
    77                     android:layout_marginTop="10dip"
    78                     android:layout_weight="1"/>
    79         </LinearLayout>
    80 
    81 
    82     </LinearLayout>
    83 
    84 </LinearLayout>

    time_picker_layout.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3               android:layout_width="match_parent"
     4               android:layout_height="match_parent"
     5               android:orientation="vertical">
     6 
     7     <LinearLayout
     8             android:id="@+id/view_none"
     9             android:layout_width="match_parent"
    10             android:layout_height="0dp"
    11             android:layout_weight="1"
    12             android:orientation="vertical"
    13             >
    14     </LinearLayout>
    15 
    16     <LinearLayout
    17             android:layout_width="match_parent"
    18             android:layout_height="wrap_content"
    19             android:background="#ffffff"
    20             android:gravity="center"
    21             android:orientation="vertical">
    22 
    23         <RelativeLayout
    24                 android:layout_width="match_parent"
    25                 android:layout_height="40dp"
    26                 android:background="#fff"
    27                 android:orientation="horizontal">
    28 
    29             <Button
    30                     android:id="@+id/cancel"
    31                     android:layout_width="120dp"
    32                     android:layout_height="fill_parent"
    33                     android:background="@drawable/dialog_btn_right_selector"
    34                     android:text="取消"
    35                     android:textColor="#5C5D5C"/>
    36             <Button
    37                     android:id="@+id/set"
    38                     android:layout_width="120dp"
    39                     android:layout_height="match_parent"
    40                     android:background="@drawable/dialog_btn_left_selector"
    41                     android:text="确定"
    42                     android:layout_alignParentRight="true"
    43                     android:textColor="#1298FF"/>
    44         </RelativeLayout>
    45 
    46         <LinearLayout
    47                 android:layout_width="match_parent"
    48                 android:layout_height="wrap_content"
    49                 android:background="#fff"
    50                 android:paddingBottom="20dp"
    51                 android:paddingTop="10dp"
    52                 android:paddingRight="30dp"
    53                 android:paddingLeft="30dp"
    54                 android:orientation="horizontal">
    55 
    56             <com.example.nanchen.datetest.widget.WheelView
    57                     android:id="@+id/hour"
    58                     android:layout_width="match_parent"
    59                     android:layout_height="wrap_content"
    60                     android:layout_weight="1"/>
    61 
    62             <com.example.nanchen.datetest.widget.WheelView
    63                     android:id="@+id/mins"
    64                     android:layout_width="fill_parent"
    65                     android:layout_height="wrap_content"
    66                     android:layout_weight="1"/>
    67         </LinearLayout>
    68 
    69     </LinearLayout>
    70 
    71 </LinearLayout>

    edit_layout.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3               android:layout_width="match_parent"
     4               android:layout_height="match_parent"
     5               android:orientation="vertical">
     6 
     7     <LinearLayout
     8         android:id="@+id/view_none"
     9         android:layout_width="match_parent"
    10         android:layout_height="0dp"
    11         android:layout_weight="1"
    12         android:orientation="vertical"
    13         >
    14     </LinearLayout>
    15 
    16     <LinearLayout
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content"
    19         android:background="#ffffff"
    20         android:gravity="center"
    21         android:orientation="vertical">
    22 
    23         <RelativeLayout
    24             android:layout_width="match_parent"
    25             android:layout_height="40dp"
    26             android:background="#fff"
    27             android:orientation="horizontal">
    28 
    29             <Button
    30                 android:id="@+id/btn_cancel"
    31                 android:layout_width="120dp"
    32                 android:layout_height="fill_parent"
    33                 android:background="@drawable/dialog_btn_right_selector"
    34                 android:text="取消"
    35                 android:textColor="#5C5D5C"/>
    36             <Button
    37                 android:id="@+id/btn_ok"
    38                 android:layout_width="120dp"
    39                 android:layout_height="match_parent"
    40                 android:background="@drawable/dialog_btn_left_selector"
    41                 android:text="确定"
    42                 android:layout_alignParentRight="true"
    43                 android:textColor="#1298FF"/>
    44         </RelativeLayout>
    45 
    46         <EditText
    47             android:layout_width="match_parent"
    48             android:layout_height="wrap_content"
    49             android:id="@+id/editText"
    50             android:hint="请输入文本..."/>
    51     </LinearLayout>
    52 
    53 </LinearLayout>

    对于楼主自己画的.9图和一些drawable以及anim包里面的东西楼主就不一一奉上了,大家还是去github看吧~https://github.com/nanchen2251/DateTestDemo

    额。对了,对于小伙伴们之前问的如何让键盘谈起不覆盖布局并且当输入文本框拉伸的时候对其他布局不造成影响的方式很简单,只需要在你的activity的申明中这样。

    1 <activity android:name=".MainActivity"
    2                   android:windowSoftInputMode="adjustResize">

    是的,就加那么一句,完美实现。

    另外需要让输入框获得焦点自动弹出软键盘的方式也很简单,两句话完美解决。

    1 InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    2         manager.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);

    额,ok啦,楼主还堆着一大堆的项目代码等着去完成,但是楼主都尽量地抽出时间为大家分享楼主的真切感受,如果大家觉得有所帮助的话,别忘了分享点赞关注,把相对有用的东西分享给更多的人,对于不足之处,还请见谅,我不是大牛,我只是你们的同行者,欢迎指正~~

    注:此文章为原创,欢迎转载,请在文章页面明显位置给出此文链接:http://www.cnblogs.com/liushilin/p/5749481.html
    若您觉得这篇文章还不错请点击下右下角的推荐,非常感谢!

  • 相关阅读:
    python--随机生成汉字、数字
    PyCharm详细教程
    Struts2 笔记
    Servlet读取配置文件的三种方式
    Java笔记(十)
    Java笔记(九)
    Java笔记(八)
    Java笔记(七)
    Java笔记(六)
    Java笔记(五)
  • 原文地址:https://www.cnblogs.com/liushilin/p/5749481.html
Copyright © 2011-2022 走看看