• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Android TimePicker and TimePicekerDialog

互联网 diligentman 3小时前 2次浏览

一.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:ignore="ExtraText"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TimePicker>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="115dp"
        android:layout_gravity="center"
        android:layout_marginVertical="2dp"
        android:paddingTop="45dp"
        android:text="当前时间:10:20"
        android:textAlignment="center"
        android:textSize="24sp" />


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="79dp"
        android:background="#B1A6A6"
        android:backgroundTint="#01579B"
        android:text="TimePickerDialog获取时间"
        android:textAllCaps="false"
        android:textSize="24sp" />

</LinearLayout>

Android TimePicker and TimePicekerDialog

二.Java代码

package com.fzg;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button button1 = findViewById(R.id.button1);
        EditText editText1 = findViewById(R.id.editText1);
        editText1.setInputType(InputType.TYPE_NULL); //edittext禁止弹出软键盘
        TimePicker timePicker1 = findViewById(R.id.timePicker1);


        timePicker1.setHour(10);// 设置小时
        timePicker1.setMinute(20); // 设置分钟
        timePicker1.setIs24HourView(true);


        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                Calendar calendar = Calendar.getInstance(); //获取日期对象
                //★同一命令的参数在不同行是也可以插入注释
                TimePickerDialog timePickerDialog1 = new TimePickerDialog(MainActivity.this,
                        timeSetListener1,  //时间设定并点击确认按钮后触发timeSetListener1监听器
                        calendar.get(Calendar.HOUR_OF_DAY), // 小时
                        calendar.get(Calendar.MINUTE),  // 分钟
                        true); //是否24小时制
                timePickerDialog1.show();
            }
        });



        timePicker1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
        {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
            {
                NumberFormat f = new DecimalFormat("00");
                long hour = Long.valueOf(hourOfDay);
                long fen = Long.valueOf(minute);
                editText1.setText("TimePicker设置的时间:" + f.format(hour) + ":" + f.format(fen));
            }
        });
    }

    TimePickerDialog.OnTimeSetListener timeSetListener1 = new TimePickerDialog.OnTimeSetListener()
    {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute)
        {
            NumberFormat f = new DecimalFormat("00");
            long hour = Long.valueOf(hourOfDay);
            long fen = Long.valueOf(minute);
            TextView editText1 = findViewById(R.id.editText1);
            editText1.setText("TimePickerDialog的时间:" + f.format(hour) + ":" + f.format(fen));
            TimePicker timePicker1 = findViewById(R.id.timePicker1);
            timePicker1.setHour(hourOfDay);
            timePicker1.setMinute(minute);
        }
    };

}

 

 

 

 


程序员灯塔
转载请注明原文链接:Android TimePicker and TimePicekerDialog
喜欢 (0)