安卓 使用TimePicker进行时间选择器 定闹钟一类时间滚动选定 (21)

在闹钟里面,我们都会看到一个时分滚动的条,里面可以选择24小时里面各个时间,这里用TimePicker可以实现一个时分的时间选择器。

首先写一个布局放入两种TimePicker:

<?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>

实现一个Activity:

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值