show code block

2016年12月18日 星期日

Android元件:時間選擇器TimePickerDialog

時間選擇器TimePickerDialog





一切的出發點,主要程式碼:

private TimePickerDialog timePickerDialog;


timePickerDialog=new TimePickerDialog(this,onTimeSetListener,hours,mins,false);
timePickerDialog.show(); //調用


/**
*this  運行指定Activity
*onTimeSetListener 重製時間監聽、用戶選好時間後的監聽
*hours 設置初始hours 未選擇/初始 前的小時
*mins  未選擇/初始 前的分鐘
*false 鬧鐘顯示方式(true為上下午制、false為24小時制)
*/






實作:

*onTimeSetListener 重製時間監聽、用戶選好時間後的監聽

   onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

           @RequiresApi(api = Build.VERSION_CODES.N)
           @Override
           public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
               DecimalFormat decimalFormat = new DecimalFormat("00");
               textview.setText(decimalFormat.format(hourOfDay) + ":" + decimalFormat.format(minute));
           }
       };
/**
* DecimalFormat 數字格式轉換的方法,我們用到裡面的format 避免倒數到10秒內時(12:09),產生12:9 這種情況
*/


*hours 設置初始hours 未選擇/初始 前的小時
*mins  未選擇/初始 前的分鐘

final Calendar calendar = Calendar.getInstance();
               int hour = calendar.get(Calendar.HOUR_OF_DAY);
               int minute = calendar.get(Calendar.MINUTE);


*false 鬧鐘顯示方式(true為上下午制、false為24小時制)










簡單三步驟END!



全部參考程式碼:


MainActivity.xml


<code>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.a015865.timepicker.MainActivity">
<TextView
    android:id="@+id/hello"
    android:layout_width="wrap_content"
       android:layout_height="wrap_content"
    android:text="Hello World!" />
<Button
    android:onClick="picker"
    android:id="@+id/picker"
       android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       android:layout_below="@id/hello"
    android:text="Picker" />
<Button
    android:onClick="pickertwo"
    android:id="@+id/pickertwo"
       android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       android:layout_below="@id/picker"
    android:text="Pickertwo" />
</RelativeLayout></code>


MainActivity.Java


public class MainActivity extends AppCompatActivity {

   private TimePickerDialog timePickerDialog;
   private TimePickerDialog.OnTimeSetListener onTimeSetListener;
   private TextView textview;
   Calendar calendar;
   int hour;
   int minute;

   @RequiresApi(api = Build.VERSION_CODES.N)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       textview = (TextView) findViewById(R.id.hello);
       calendar = Calendar.getInstance();
       hour = calendar.get(Calendar.HOUR_OF_DAY);
       minute = calendar.get(Calendar.MINUTE);
       onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {


           @RequiresApi(api = Build.VERSION_CODES.N)
           @Override
           public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
               DecimalFormat decimalFormat = new DecimalFormat("00");
               textview.setText(decimalFormat.format(hourOfDay) + ":" + decimalFormat.format(minute));
           }
       };

   }



   public void picker(View view) {
       timePickerDialog = new TimePickerDialog(this, onTimeSetListener, hour,minute, true);
       timePickerDialog.show();
   }


   public void pickertwo(View view) {
       timePickerDialog = new TimePickerDialog(this, onTimeSetListener, hour,minute, false);
       timePickerDialog.show();
   }
}


完成囉!

沒有留言:

張貼留言

協程(coroutine) - 協程為什麼要學它?

 Coroutine 協程 再強調一次 協程就是由kotlin官方所提供的線程api //Thread Thread { }.start() //Executor val execu...