編輯:關於Android編程
本文實例分析了Android開發之TimePicker控件用法。分享給大家供大家參考,具體如下:
新建項目:
New Android Project->
Project name:HelloSpinner
Build Target:Android 2.2
Application name:HelloSpinner
Package name:com.b510
Create Activity:MainActivity
Min SDK Version:9
Finish
運行效果:
如果:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
代碼部分:
MainActivity.java:
package com.b510; import java.util.Calendar; import android.app.Activity; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class HelloTimePicker extends Activity { /* 顯示時間信息 */ private TextView tvTimePickerDisplay; /* 設置時間按鈕 */ private Button btnTimePicker; /* 小時 */ private int mHour; /* 分鐘 */ private int mMinute; /* 標識 dialog的id */ static final int TIME_DIALOG_ID = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.tvTimePickerDisplay = (TextView) this .findViewById(R.id.tv_timepicker_display); this.btnTimePicker = (Button) findViewById(R.id.btn_timepicker); btnTimePicker.setOnClickListener(listener); // get the current time final Calendar c = Calendar.getInstance(); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); // display the current date updateDisplay(); } private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }; // updates the time we display in the TextView private void updateDisplay() { tvTimePickerDisplay.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); } // the callback received when the user "sets" the time in the dialog private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; updateDisplay(); } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true); } return null; } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv_timepicker_display" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_timepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change the time" /> </LinearLayout>
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
首先,很榮幸此專欄能被CSDN推薦到主頁。榮幸的同時,也激勵自己會把這個專欄一直更新下去。進入今天的主題:我們在qq登錄的時候,會有一個下拉的按鈕,來查看歷史登錄賬號。這
魅族的魅藍手機通常都是只有移動和聯通版兩種版本的,但是最近最新發布的魅藍E再次吧魅藍手機推向了一個新的高度,很多機友就開始問小編了。魅族魅藍E支持什麼SIM
實現如下邊框效果:虛線畫效果,可以使用Android中的xml來做。下面話不多說,直接上代碼:<RelativeLayout android:id=@+i
觸控事件MotionEvent類: //單擊觸摸按下動作 public static final int ACTION_DOWN = 0;