• Android TimePicker


    package com.neddy;

    import java.util.Calendar;

    import android.app.Activity;
    import android.app.Dialog;
    import android.app.TimePickerDialog;
    import android.app.TimePickerDialog.OnTimeSetListener;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.TimePicker;

    public class HelloTimePicker extends Activity
    {
    private TextView mTimeDisplay;
    private Button mPickTime;

    private int mHour;
    private int mMinute;
    private OnTimeSetListener mTimeSetListener=
    new OnTimeSetListener()
    {
    public void onTimeSet(TimePicker view ,int hourOfDay,int minute)
    {
    mHour
    = hourOfDay;
    mMinute
    =minute;
    updateDisplay();
    }
    };

    static final int TIME_DIALOG_ID=0;

    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTimeDisplay
    =(TextView)findViewById(R.id.timeDisplay);
    mPickTime
    =(Button)findViewById(R.id.pickTime);

    mPickTime.setOnClickListener(
    new View.OnClickListener()
    {
    public void onClick(View v)
    {
    showDialog(TIME_DIALOG_ID);
    }
    });

    final Calendar c=Calendar.getInstance();
    mHour
    =c.get(Calendar.HOUR_OF_DAY);
    mMinute
    =c.get(Calendar.MINUTE);

    updateDisplay();
    }

    private void updateDisplay()
    {
    mTimeDisplay.setText(
    new StringBuilder()
    .append(pad(mHour)).append(
    ":")
    .append(pad(mMinute)));

    }

    private static String pad(int c)
    {
    if(c>=10) return String.valueOf(c);
    else return "0" + String.valueOf(c);
    }

    protected Dialog onCreateDialog(int id)
    {
    switch(id)
    {
    case TIME_DIALOG_ID:
    return new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, false);
    }
    return null;
    }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="vertical"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    >
    <TextView
    android:id="@+id/timeDisplay"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:text
    =""
    />
    <Button
    android:id="@+id/pickTime"
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:text
    ="Change the time"
    />
    </LinearLayout>

  • 相关阅读:
    iOS
    iOS
    ios
    iOS
    ios
    ios
    iOS
    ios
    常用NSString的方法
    instancetype
  • 原文地址:https://www.cnblogs.com/Neddy/p/2179209.html
Copyright © 2020-2023  润新知