最近公司要求的上線項目有這么一個需求,要寫一個請假申請的頁面,里面必須有請假開始時間,結束時間,還有一個請假原因。
於是想到時間選擇嘛,官方不是有個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
若您覺得這篇文章還不錯請點擊下右下角的推薦,非常感謝!