Android TimePickerDialog(原生安卓时间选择器)

这个控件说实话之前没用过原生的,都是用的自定义的,其实这个也挺好看的。

Android TimePickerDialog(原生安卓时间选择器)_第1张图片

public class MainActivity extends AppCompatActivity {

    private Calendar c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                c = Calendar.getInstance();
                c.setTimeInMillis(System.currentTimeMillis());
                int hour = c.get(Calendar.HOUR_OF_DAY);
                int minute = c.get(Calendar.MINUTE);
                new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        c.setTimeInMillis(System.currentTimeMillis());
                        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        c.set(Calendar.MINUTE, minute);
                        c.set(Calendar.SECOND, 0);
                        c.set(Calendar.MILLISECOND, 0);
                        Toast.makeText(MainActivity.this, c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE), Toast.LENGTH_SHORT).show();
                    }
                }, hour, minute, true).show();
            }
        });
    }
}

TimePickerDialog的参数
参数1:上下文
参数2:设置时间的回调,在这里获取用户最终选择的时间
参数3:小时初始值
参数4:分钟初始值
参数5:12小时制/24

 
  
 
 

你可能感兴趣的:(Android)