放弃安卓原生TimePicker,选择wheelView打造更漂亮的时间get,以及动态拉伸输入框布局,这些,这里都有!
最近公司要求的上线项目有这么一个需求,要写一个请假申请的页面,里面必须有请假开始时间,结束时间,还有一个请假原因。
于是想到时间选择嘛,官方不是有个DatePicker吗?额,是不是要DatePicker哦,sorry,楼主有点英文犯愁,于是去看一看安卓原生态的时间选择器,感觉还行,基本的功能都有呈现,就将就着用呗。
没事去串了串负责开发IOS端的同事的UI,我去,这么高大上,简直就是高富帅和白富美用的嘛,再看看我的,什么玩意儿,丑的有模有样,真是有点儿意思。
如果让同样的功能,给我们安卓端一个这么丑的玩意儿,还真的是抹黑!!折煞了我们大安卓的开源性,于是,自己写呗,咦,似乎有个叫WheelView的玩意儿,额,就是这个,在它上面下点功夫。
额,还是先给大家带来个运行图,要是大家觉得有用,可以花个几分钟碎片时间瞧一瞧,不要钱的。看不了放心,看不了舒心!
代码中实现了弹出动画,以及一些shape的定义。

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

额,其实实现起来很简单很简单啦,对于要使用wheelView的代码,网上搜一大堆,你也可以去楼主上传代码的github网站下载Demo自行获取。
项目已同步至github:https://github.com/nanchen2251/DateTestDemo
对于实现上给大家稍微讲解一下。
首先是把必须用到的几个java代码拷贝进去,也就是我上传demo的Adapter和widget两个包。
主页面的布局相当简单,就4个按钮。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"> <Button
android:id="@+id/tv_edit"
style="@style/btn_bg"
android:text="弹出可输入的对话框"/> <Button
android:id="@+id/tv_time"
style="@style/btn_bg"
android:layout_marginTop="10dp"
android:text="弹出时间"/> <Button
android:id="@+id/tv_date"
style="@style/btn_bg"
android:layout_marginTop="10dp"
android:text="弹出日期"/> <Button
android:id="@+id/tv_date_time"
style="@style/btn_bg"
android:layout_marginTop="10dp"
android:text="弹出日期时间"/> </LinearLayout>
主页面的代码MainActivity.java
在其中目前用到动画的是自定义的AlertDialog,其实代码中也实现了popWindow跳出的另一种方式,具体大家自行脑补。
具体的代码上就相对简单啦,我相信小伙伴们一看就能明了,不过大家在开发中真的需要仔细,楼主就在开发这个的时候写了一个TimeUtils,因为写错一个参数导致调了两小时,不过当然错误没在这个Demo中发生啦,是在楼主写的项目中。
很简单的代码逻辑,基本就是4个按钮,分别设置一个点击事件,跳转到一个自定义的AlertDialog,设置一些布局的基本用处,再通过初始化WheelView的值,额,因为在时间上每个月的天数有些不一致,在闰年平年也有不一致,所以需要写一个方法对此进行标注。额,好像没有了耶。
代码中也注释得很清楚啦。还是直接上代码吧。
package com.example.nanchen.datetest; import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import android.widget.Toast; import com.example.nanchen.datetest.adapter.NumericWheelAdapter;
import com.example.nanchen.datetest.widget.WheelView; import java.util.Calendar;
import java.util.Locale; /**
* @author nanchen
* @date 2016-08-08
*/
public class MainActivity extends Activity{
private LayoutInflater inflater = null;
private WheelView year;
private WheelView month;
private WheelView day;
private WheelView hour;
private WheelView mins; PopupWindow menuWindow; Button tv_time,tv_date,popBtn;
private Button btn_edit; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
tv_time=(Button) findViewById(R.id.tv_time);//时间选择器
tv_date=(Button) findViewById(R.id.tv_date);//日期选择器 popBtn = (Button) findViewById(R.id.tv_date_time); btn_edit = (Button) findViewById(R.id.tv_edit);
btn_edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showPopwindow(getEditText());
}
}); tv_time.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// showPopwindow(getTimePick());//弹出时间选择器
showTimeDialog();
}
});
tv_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// showPopwindow(getDataPick());//弹出日期选择器
showDateDialog();
}
}); popBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// showMyNewDate(getDateAndTime());
showDateAndTime();
}
});
} /**
* 初始化popupWindow
* @param view
*/
private void showPopwindow(View view) {
menuWindow = new PopupWindow(view,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
menuWindow.setFocusable(true);
menuWindow.setBackgroundDrawable(new BitmapDrawable());
menuWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
menuWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
menuWindow=null;
}
});
} private View getEditText() {
View view = inflater.inflate(R.layout.edit_layout,null);
final EditText editText = (EditText) view.findViewById(R.id.editText);
InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
manager.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);
Button btn_ok = (Button) view.findViewById(R.id.btn_ok);
Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
btn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,editText.getText().toString(),Toast.LENGTH_SHORT).show();
menuWindow.dismiss();
}
});
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
menuWindow.dismiss();
}
});
return view;
} /**
*
* @return
*/
private View getTimePick() {
View view = inflater.inflate(R.layout.time_picker_layout, null);
hour = (WheelView) view.findViewById(R.id.hour);
initHour();
mins = (WheelView) view.findViewById(R.id.mins);
initMins();
// 设置当前时间
hour.setCurrentItem(8);
mins.setCurrentItem(30); hour.setVisibleItems(7);
mins.setVisibleItems(7); Button bt = (Button) view.findViewById(R.id.set);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = hour.getCurrentItem() + ":"+ mins.getCurrentItem();
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
menuWindow.dismiss();
}
});
Button cancel = (Button) view.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menuWindow.dismiss();
}
}); return view;
} /**
*
* @return
*/
private View getDataPick() {
Calendar c = Calendar.getInstance();
int curYear = c.get(Calendar.YEAR);
int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
int curDate = c.get(Calendar.DATE);
final View view = inflater.inflate(R.layout.datepicker_layout, null); year = (WheelView) view.findViewById(R.id.year);
initYear();
month = (WheelView) view.findViewById(R.id.month);
initMonth();
day = (WheelView) view.findViewById(R.id.day);
initDay(curYear,curMonth); year.setCurrentItem(curYear - 1950);
month.setCurrentItem(curMonth - 1);
day.setCurrentItem(curDate - 1);
year.setVisibleItems(7);
month.setVisibleItems(7);
day.setVisibleItems(7); Button bt = (Button) view.findViewById(R.id.set);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = (year.getCurrentItem()+1950) + "-"+ (month.getCurrentItem()+1)+"-"+(day.getCurrentItem());
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
menuWindow.dismiss();
}
});
Button cancel = (Button) view.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menuWindow.dismiss();
}
});
return view;
} /**
*
* @param year
* @param month
* @return
*/
private int getDay(int year, int month) {
int day = 30;
boolean flag = false;
switch (year % 4) {
case 0:
flag = true;
break;
default:
flag = false;
break;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
day = flag ? 29 : 28;
break;
default:
day = 30;
break;
}
return day;
}
/**
* 初始化年
*/
private void initYear() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1950, 2050);
numericWheelAdapter.setLabel(" 年");
// numericWheelAdapter.setTextSize(15); 设置字体大小
year.setViewAdapter(numericWheelAdapter);
year.setCyclic(true);
} /**
* 初始化月
*/
private void initMonth() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1, 12, "%02d");
numericWheelAdapter.setLabel(" 月");
// numericWheelAdapter.setTextSize(15); 设置字体大小
month.setViewAdapter(numericWheelAdapter);
month.setCyclic(true);
} /**
* 初始化天
*/
private void initDay(int arg1, int arg2) {
NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
numericWheelAdapter.setLabel(" 日");
// numericWheelAdapter.setTextSize(15); 设置字体大小
day.setViewAdapter(numericWheelAdapter);
day.setCyclic(true);
} /**
* 初始化时
*/
private void initHour() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,0, 23, "%02d");
numericWheelAdapter.setLabel(" 时");
// numericWheelAdapter.setTextSize(15); 设置字体大小
hour.setViewAdapter(numericWheelAdapter);
hour.setCyclic(true);
} /**
* 初始化分
*/
private void initMins() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,0, 59, "%02d");
numericWheelAdapter.setLabel(" 分");
// numericWheelAdapter.setTextSize(15); 设置字体大小
mins.setViewAdapter(numericWheelAdapter);
mins.setCyclic(true);
} /**
* 显示全部日期
*/
private void showDateAndTime(){
Calendar c = Calendar.getInstance();
int curYear = c.get(Calendar.YEAR);
int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
int curDate = c.get(Calendar.DATE);
int curHour = c.get(Calendar.HOUR_OF_DAY);
int curMin = c.get(Calendar.MINUTE); final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.create();
dialog.show();
Window window = dialog.getWindow();
// 设置布局
window.setContentView(R.layout.date_time_picker_layout);
// 设置宽高
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// 设置弹出的动画效果
window.setWindowAnimations(R.style.AnimBottom); year = (WheelView) window.findViewById(R.id.new_year);
initYear();
month = (WheelView) window.findViewById(R.id.new_month);
initMonth();
day = (WheelView) window.findViewById(R.id.new_day);
initDay(curYear,curMonth);
hour = (WheelView) window.findViewById(R.id.new_hour);
initHour();
mins = (WheelView) window.findViewById(R.id.new_mins);
initMins(); // 设置当前时间
year.setCurrentItem(curYear - 1950);
month.setCurrentItem(curMonth - 1);
day.setCurrentItem(curDate - 1);
hour.setCurrentItem(curHour);
mins.setCurrentItem(curMin); month.setVisibleItems(7);
day.setVisibleItems(7);
hour.setVisibleItems(7);
mins.setVisibleItems(7); // 设置监听
TextView ok = (TextView) window.findViewById(R.id.set);
TextView cancel = (TextView) window.findViewById(R.id.cancel);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String time = String.format(Locale.CHINA,"%04d年%02d月%02d日 %02d时%02d分",year.getCurrentItem()+1950,
month.getCurrentItem()+1,day.getCurrentItem()+1,hour.getCurrentItem(),mins.getCurrentItem());
Toast.makeText(MainActivity.this, time, Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
cancelLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
dialog.cancel();
return false;
}
});
} /**
* 显示时间
*/
private void showTimeDialog(){
final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.create();
dialog.show();
Window window = dialog.getWindow();
// 设置布局
window.setContentView(R.layout.time_picker_layout);
// 设置宽高
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// 设置弹出的动画效果
window.setWindowAnimations(R.style.AnimBottom); Calendar c = Calendar.getInstance();
int curHour = c.get(Calendar.HOUR_OF_DAY);
int curMin = c.get(Calendar.MINUTE); hour = (WheelView) window.findViewById(R.id.hour);
initHour();
mins = (WheelView) window.findViewById(R.id.mins);
initMins();
// 设置当前时间
hour.setCurrentItem(curHour);
mins.setCurrentItem(curMin); hour.setVisibleItems(7);
mins.setVisibleItems(7); // 设置监听
Button ok = (Button) window.findViewById(R.id.set);
Button cancel = (Button) window.findViewById(R.id.cancel);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = String.format(Locale.CHINA,"%2d:%2d",hour.getCurrentItem(), mins.getCurrentItem());
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
cancelLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
dialog.cancel();
return false;
}
});
} /**
* 显示日期
*/
private void showDateDialog() {
final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.create();
dialog.show();
Window window = dialog.getWindow();
// 设置布局
window.setContentView(R.layout.datepicker_layout);
// 设置宽高
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// 设置弹出的动画效果
window.setWindowAnimations(R.style.AnimBottom); Calendar c = Calendar.getInstance();
int curYear = c.get(Calendar.YEAR);
int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
int curDate = c.get(Calendar.DATE);
year = (WheelView) window.findViewById(R.id.year);
initYear();
month = (WheelView) window.findViewById(R.id.month);
initMonth();
day = (WheelView) window.findViewById(R.id.day);
initDay(curYear,curMonth); year.setCurrentItem(curYear - 1950);
month.setCurrentItem(curMonth - 1);
day.setCurrentItem(curDate - 1);
year.setVisibleItems(7);
month.setVisibleItems(7);
day.setVisibleItems(7); // 设置监听
Button ok = (Button) window.findViewById(R.id.set);
Button cancel = (Button) window.findViewById(R.id.cancel);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = String.format(Locale.CHINA,"%4d年%2d月%2d日",year.getCurrentItem()+1950,month.getCurrentItem()+1,day.getCurrentItem()+1);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_none);
cancelLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
dialog.cancel();
return false;
}
}); } }
另外的几个小布局也意义奉上。
date_time_picker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:id="@+id/view_none"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"> <TextView
android:id="@+id/cancel"
android:layout_width="120dp"
android:layout_height="match_parent"
android:background="#fff"
android:gravity="center"
android:text="取消"
android:textColor="#1298FF"/> <TextView
android:id="@+id/set"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#fff"
android:gravity="center"
android:text="确定"
android:textColor="#1298FF"/>
</RelativeLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:paddingBottom="10dp"> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/new_year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_weight="0.9"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/new_month"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_weight="1"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/new_day"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_weight="1"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/new_hour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_weight="1"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/new_mins"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginRight="5dip"
android:layout_weight="1"/>
</LinearLayout> </LinearLayout> </LinearLayout>
datepicker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:id="@+id/view_none"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical"> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#fff"
android:orientation="horizontal"> <Button
android:id="@+id/cancel"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:background="@drawable/dialog_btn_right_selector"
android:text="取消"
android:textColor="#5C5D5C"/>
<Button
android:id="@+id/set"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:background="@drawable/dialog_btn_left_selector"
android:text="确定"
android:layout_alignParentRight="true"
android:textColor="#1298FF"/>
</RelativeLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:orientation="horizontal"> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="5dip"
android:layout_marginTop="10dip"
android:layout_weight="0.8"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/month"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:layout_weight="1"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/day"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip"
android:layout_weight="1"/>
</LinearLayout> </LinearLayout> </LinearLayout>
time_picker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:id="@+id/view_none"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#fff"
android:orientation="horizontal"> <Button
android:id="@+id/cancel"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:background="@drawable/dialog_btn_right_selector"
android:text="取消"
android:textColor="#5C5D5C"/>
<Button
android:id="@+id/set"
android:layout_width="120dp"
android:layout_height="match_parent"
android:background="@drawable/dialog_btn_left_selector"
android:text="确定"
android:layout_alignParentRight="true"
android:textColor="#1298FF"/>
</RelativeLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:orientation="horizontal"> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/hour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/> <com.example.nanchen.datetest.widget.WheelView
android:id="@+id/mins"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout> </LinearLayout> </LinearLayout>
edit_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:id="@+id/view_none"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#fff"
android:orientation="horizontal"> <Button
android:id="@+id/btn_cancel"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:background="@drawable/dialog_btn_right_selector"
android:text="取消"
android:textColor="#5C5D5C"/>
<Button
android:id="@+id/btn_ok"
android:layout_width="120dp"
android:layout_height="match_parent"
android:background="@drawable/dialog_btn_left_selector"
android:text="确定"
android:layout_alignParentRight="true"
android:textColor="#1298FF"/>
</RelativeLayout> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="请输入文本..."/>
</LinearLayout> </LinearLayout>
对于楼主自己画的.9图和一些drawable以及anim包里面的东西楼主就不一一奉上了,大家还是去github看吧~https://github.com/nanchen2251/DateTestDemo
额。对了,对于小伙伴们之前问的如何让键盘谈起不覆盖布局并且当输入文本框拉伸的时候对其他布局不造成影响的方式很简单,只需要在你的activity的申明中这样。
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
是的,就加那么一句,完美实现。
另外需要让输入框获得焦点自动弹出软键盘的方式也很简单,两句话完美解决。
InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
manager.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);
额,ok啦,楼主还堆着一大堆的项目代码等着去完成,但是楼主都尽量地抽出时间为大家分享楼主的真切感受,如果大家觉得有所帮助的话,别忘了分享点赞关注,把相对有用的东西分享给更多的人,对于不足之处,还请见谅,我不是大牛,我只是你们的同行者,欢迎指正~~
注:此文章为原创,欢迎转载,请在文章页面明显位置给出此文链接:http://www.cnblogs.com/liushilin/p/5749481.html
若您觉得这篇文章还不错请点击下右下角的推荐,非常感谢!
放弃安卓原生TimePicker,选择wheelView打造更漂亮的时间get,以及动态拉伸输入框布局,这些,这里都有!的更多相关文章
- 【安卓开发】用PageTransformer打造更好的动画效果
Android的ViewPager类已经变成一个相当流行的Android应用组件了.它简单直观,并且提供了极好的功能.你可以经常在设置向导,图片画廊种看到它,它还是分开应用内容的良好方式. 标准的Vi ...
- MSYS2是对MSYS的一个独立的重写,是基于当前的Cygwin和MinGW-w64重写的,以同原生的Windows软件有更好的交互性为目的
MSYS2的官网:http://sourceforge.net/projects/msys2/ 官网的描述: Description MSYS2 is an independent rewrite o ...
- AI剪辑和自定义UI,打造更智能的剪辑体验
为满足开发者构建高效的应用内视频编辑能力,7月的HMS Core 6.0 推出了视频编辑服务(Video Editor Kit),一站式的视频处理能力获得了积极反响.同时,我们也关注到开发者需要集成丰 ...
- 【2021/12/31】uniapp之安卓原生插件开发教程
uniapp之安卓原生插件开发教程 准备 hbuilderX,下载 app离线SDK,下载 Andorid Studio,安卓官方或中文社区 证书(可以自己准备,也可以使用android Studio ...
- 产品 | GreatSQL,打造更好的MGR生态
欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 用 ...
- Unity实现相似于安卓原生项目的点击安卓返回button回到前一页的功能
本章博主和大家一起讨论下Unity怎么实现类似安卓原生项目,点击安卓返回button实现返回到前一个页面的功能. 1.定义一个泛型用于响应安卓的返回button public static List& ...
- angularjs中安卓原生APP调用H5页面js函数,js写法应注意
安卓原生app调用js方法,js方法应写在html下的script标签内,不能有任何function包裹,例如angular的controller层,这样APP也是获取不到的: 所以只有放在html中 ...
- Qt 打开安卓相冊选择图片并获取图片的本地路径
Qt 打开安卓相冊选择图片并获取图片的本地路径 过程例如以下: 通过 Intent 打开安卓的系统相冊. 推荐使用 QAndroidJniObject::getStaticObjectField 获取 ...
- 将H5页面打包成安卓原生app
第一步:下载HBuilderX,新建项目选择5+App新建一个空项目如下图 新建后项目目录结构如下图 第二步,将你要打包成安卓app的文件打包,最后生成的文件目录如下图 1.打包完成后,将对应文件内容 ...
随机推荐
- “四核”驱动的“三维”导航 -- 淘宝新UI(需求分析篇)
前言 孔子说:"软件是对客观世界的抽象". 首先声明,这里的"三维导航"和地图没一毛钱关系,"四核驱动"和硬件也没关系,而是为了复杂的应用而 ...
- xpath提取多个标签下的text
title: xpath提取多个标签下的text author: 青南 date: 2015-01-17 16:01:07 categories: [Python] tags: [xpath,Pyth ...
- hash表长度优化证明
hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...
- python与c互相调用
虽然python开发效率很高,但作为脚本语言,其性能不高,所以为了兼顾开发效率和性能,通常把性能要求高的模块用c或c++来实现或者在c或c++中运行python脚本来处理逻辑,前者通常是python中 ...
- 设置tomcat远程debug
查看端口占用情况命令: netstat -tunlp |grep 8000 tomcat 启动远程debug: startup.sh 中的最后一行 exec "$PRGDIR"/& ...
- mysql 大表拆分成csv导出
最近公司有一个几千万行的大表需要按照城市的id字段拆分成不同的csv文件. 写了一个自动化的shell脚本 在/home/hdh 下面 linux-xud0:/home/hdh # lltotal 1 ...
- Linux设备管理(四)_从sysfs回到ktype
sysfs是一个基于ramfs的文件系统,在2.6内核开始引入,用来导出内核对象(kernel object)的数据.属性到用户空间.与同样用于查看内核数据的proc不同,sysfs只关心具有层次结构 ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
- maven常见异常以及解决方法
本文写的是我在整合ssm框架时遇到的一些问题,我估计很多人也会遇到,所以,这里作为一个总结,希望能够帮助大家解决问题 一,加入shiro组件时抛出的异常 加入步骤(略) 问题 1,保存后,无法导入sh ...
- ASP.NET Core MVC 中的 [Controller] 和 [NonController]
前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...