布局文件:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <linearLayout androID:ID="@+ID/layout_spinner" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <!-- 这里显示方式是一个两个滚动条,里面一列是时,一条列分--> <TimePicker androID:ID="@+ID/time_piker_spinner" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:timePickerMode="spinner"> </TimePicker> </linearLayout> <linearLayout androID:ID="@+ID/layout_clock" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:visibility="gone"> <!-- 这里显示方式是一个时钟,第一次点击是选择时,第二次点击是选择分--> <TimePicker androID:ID="@+ID/time_picker_clock" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center"> </TimePicker> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <!-- 点击它改变显示的TimePicker--> <button androID:ID="@+ID/change_timepicker" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="change" androID:layout_gravity="center"/> <!-- 点击它输出TimePicker中的时间--> <button androID:ID="@+ID/setdata_timepicker" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="set data" androID:layout_gravity="center"/> </linearLayout> </relativeLayout>
Java文件:
public class TimePickActivity extends Activity implements VIEw.OnClickListener{ private TimePicker timePicker_spinner; private TimePicker timePicker_clock; private button btn_change; private button btn_setData; private linearLayout layout_clock; private linearLayout layout_spinner; private boolean change_Timepicker = false; private int clock_hour; private int clock_minute; private int spinner_hour; private int spinner_minute; private String clock; private String spinner; @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.timepick_layout); layout_clock = findVIEwByID(R.ID.layout_clock); layout_spinner = findVIEwByID(R.ID.layout_spinner); btn_change = findVIEwByID(R.ID.change_timepicker); btn_setData = findVIEwByID(R.ID.setdata_timepicker); timePicker_spinner = findVIEwByID(R.ID.time_piker_spinner); timePicker_clock = findVIEwByID(R.ID.time_picker_clock); btn_change.setonClickListener(this); btn_setData.setonClickListener(this); //设置其显示方式为24小时(如果参数为false则分为AM与PM显示) timePicker_clock.setIs24HourVIEw(true); timePicker_spinner.setIs24HourVIEw(true); //自动获取焦点 timePicker_spinner.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS); timePicker_clock.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS); //设置初始值(即00:00) timePicker_clock.setCurrentHour(0); timePicker_clock.setCurrentMinute(0); timePicker_spinner.setCurrentHour(0); timePicker_spinner.setCurrentMinute(0); //通过OnTimeChangedListener来得到当前的时与分 timePicker_spinner.setonTimeChangedListener(new TimePicker.OnTimeChangedListener() { @OverrIDe public voID onTimeChanged(TimePicker vIEw, int hourOfDay, int minute) { spinner_hour = hourOfDay; spinner_minute = minute; } }); timePicker_clock.setonTimeChangedListener(new TimePicker.OnTimeChangedListener() { @OverrIDe public voID onTimeChanged(TimePicker vIEw, int hourOfDay, int minute) { clock_hour = hourOfDay; clock_minute = minute; } }); } @OverrIDe public voID onClick(VIEw v) { switch(v.getID()){ //使用该按钮更换两种形式Clock Spinner case R.ID.change_timepicker: if(!change_Timepicker){ layout_spinner.setVisibility(VIEw.GONE); layout_clock.setVisibility(VIEw.VISIBLE); change_Timepicker = !change_Timepicker; }else{ layout_spinner.setVisibility(VIEw.VISIBLE); layout_clock.setVisibility(VIEw.GONE); change_Timepicker = !change_Timepicker; } break; //得到我们所需求的时间 case R.ID.setdata_timepicker: if(!change_Timepicker){ spinner = getTimeString(spinner_hour,spinner_minute); Toast.makeText(TimePickActivity.this, spinner, Toast.LENGTH_SHORT).show(); }else{ clock = getTimeString(clock_hour,clock_minute); Toast.makeText(TimePickActivity.this, clock, Toast.LENGTH_SHORT).show(); } } } public String getTimeString(int hour, int minute){ String string_hour, string_minute; // 显示格式:xx:xx // for example:03:07 if(hour < 10){ string_hour = "0" + hour; }else{ string_hour = "" + hour; } if(minute < 10){ string_minute = "0" + minute; }else { string_minute = "" + minute; } return string_hour + ":" + string_minute; }}
转自:https://blog.csdn.net/qq_39079912/article/details/79696399
总结以上是内存溢出为你收集整理的Android自定义选择时间控件全部内容,希望文章能够帮你解决Android自定义选择时间控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)