A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置小結(jié)
更新時(shí)間:2013年06月13日 17:59:55 作者:
本文將帶領(lǐng)大家一起學(xué)習(xí)時(shí)間日期和時(shí)鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈
目標(biāo):學(xué)習(xí)時(shí)間日期和時(shí)鐘的設(shè)置
picker的計(jì)算機(jī)專(zhuān)業(yè)解釋是“選擇器”。
簡(jiǎn)單翻譯一下:
TimePicker 時(shí)間選擇器
DatePicker 日期選擇器
AnalogClock 模擬時(shí)鐘
DigitalClock 數(shù)字時(shí)鐘
一、TimePicker
1.TimePicker使用的監(jiān)聽(tīng)器接口是OnTimeChangedListener
2.TimePicker默認(rèn)顯示系統(tǒng)當(dāng)前時(shí)間,可以使用setCurrentHour和setCurrentMinute兩個(gè)方法設(shè)置默認(rèn)顯示時(shí)間
3.可使用setIs24HourView方法設(shè)置TimePicker以24小時(shí)制顯示
4.獲取TimePicker的當(dāng)前時(shí)間,使用getCurrentHour和getCurrentMinute兩個(gè)方法
模擬器android4.2顯示效果(非24小時(shí)制):
真機(jī)android2.3.7顯示效果(非24小時(shí)制):
真機(jī)android2.3.7顯示效果(24小時(shí)制):
Java代碼:
package com.haut.a07_timepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private TimePicker timePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) findViewById(R.id.timePickerId);
button = (Button) findViewById(R.id.buttonId);
// 為timePicker創(chuàng)建監(jiān)聽(tīng)器
TimePickerListener timeListener = new TimePickerListener();
timePicker.setOnTimeChangedListener(timeListener);
// 為button創(chuàng)建監(jiān)聽(tīng)器
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
// TimePicker默認(rèn)顯示當(dāng)前時(shí)間,可以手動(dòng)制定它的默認(rèn)顯示時(shí)間
timePicker.setCurrentHour(12);
timePicker.setCurrentMinute(0);
// 設(shè)置顯示格式為24小時(shí)制
timePicker.setIs24HourView(true);
}
class TimePickerListener implements OnTimeChangedListener {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// 使用Toast顯示TimePicker的時(shí)間
String time = hourOfDay + "點(diǎn):" + minute + "分";
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
}
class ButtonListener implements OnClickListener {
public void onClick(View v) {
String time = timePicker.getCurrentHour() + "點(diǎn):"
+ timePicker.getCurrentMinute() + "分";
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
xml代碼:
<RelativeLayout 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:background="@drawable/folwer1"
tools:context=".MainActivity" >
<TimePicker
android:id="@+id/timePickerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取設(shè)置時(shí)間"
android:layout_below="@id/timePickerId"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
二、DatePicker
1.DatePicker沒(méi)有像TimePicker一樣類(lèi)似OnTimeChangedListener的監(jiān)聽(tīng)器接口。有對(duì)話(huà)框,以后補(bǔ)充。
補(bǔ)充見(jiàn):DatePicker的對(duì)話(huà)框設(shè)置
模擬器android4.2效果圖:
手機(jī)android2.3.7效果圖:
java代碼:
package com.haut.a07_datepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private DatePicker datePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = (DatePicker)findViewById(R.id.datePickerId);
button = (Button)findViewById(R.id.buttonId);
//為button創(chuàng)建監(jiān)聽(tīng)器
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
String date = datePicker.getYear() + "年:" + datePicker.getMonth() + "月:" + datePicker.getDayOfMonth() + "日";
Toast.makeText(MainActivity.this, date, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
xml代碼:
<RelativeLayout 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:background="@drawable/leaf"
tools:context=".MainActivity" >
<DatePicker
android:id="@+id/datePickerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取設(shè)置日期"
android:layout_below="@id/datePickerId"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
三、AnalogClock
顯示的時(shí)鐘時(shí)間會(huì)隨著系統(tǒng)時(shí)間的變化而變化。
代碼比較簡(jiǎn)單就不貼了,只是在xml布局文件中添加一個(gè)<AnalogClock/>標(biāo)簽。
模擬器android4.2效果圖:
手機(jī)android2.3.7效果圖:
四、DigitalClock
顯示的時(shí)鐘時(shí)間會(huì)隨著系統(tǒng)時(shí)間的變化而變化。
模擬器android4.2效果圖:
手機(jī)android2.3.7效果圖:
xml代碼:
<RelativeLayout 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:background="@drawable/folwer"
tools:context=".MainActivity" >
<DigitalClock
android:id="@+id/digitalClockId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textColor="#ff0000"
android:textSize="30sp" />
</RelativeLayout>
具體的操作以后用到再具體補(bǔ)充~
picker的計(jì)算機(jī)專(zhuān)業(yè)解釋是“選擇器”。
簡(jiǎn)單翻譯一下:
TimePicker 時(shí)間選擇器
DatePicker 日期選擇器
AnalogClock 模擬時(shí)鐘
DigitalClock 數(shù)字時(shí)鐘
一、TimePicker
1.TimePicker使用的監(jiān)聽(tīng)器接口是OnTimeChangedListener
2.TimePicker默認(rèn)顯示系統(tǒng)當(dāng)前時(shí)間,可以使用setCurrentHour和setCurrentMinute兩個(gè)方法設(shè)置默認(rèn)顯示時(shí)間
3.可使用setIs24HourView方法設(shè)置TimePicker以24小時(shí)制顯示
4.獲取TimePicker的當(dāng)前時(shí)間,使用getCurrentHour和getCurrentMinute兩個(gè)方法
模擬器android4.2顯示效果(非24小時(shí)制):

真機(jī)android2.3.7顯示效果(非24小時(shí)制):

真機(jī)android2.3.7顯示效果(24小時(shí)制):

Java代碼:
復(fù)制代碼 代碼如下:
package com.haut.a07_timepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private TimePicker timePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) findViewById(R.id.timePickerId);
button = (Button) findViewById(R.id.buttonId);
// 為timePicker創(chuàng)建監(jiān)聽(tīng)器
TimePickerListener timeListener = new TimePickerListener();
timePicker.setOnTimeChangedListener(timeListener);
// 為button創(chuàng)建監(jiān)聽(tīng)器
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
// TimePicker默認(rèn)顯示當(dāng)前時(shí)間,可以手動(dòng)制定它的默認(rèn)顯示時(shí)間
timePicker.setCurrentHour(12);
timePicker.setCurrentMinute(0);
// 設(shè)置顯示格式為24小時(shí)制
timePicker.setIs24HourView(true);
}
class TimePickerListener implements OnTimeChangedListener {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// 使用Toast顯示TimePicker的時(shí)間
String time = hourOfDay + "點(diǎn):" + minute + "分";
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
}
class ButtonListener implements OnClickListener {
public void onClick(View v) {
String time = timePicker.getCurrentHour() + "點(diǎn):"
+ timePicker.getCurrentMinute() + "分";
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
xml代碼:
復(fù)制代碼 代碼如下:
<RelativeLayout 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:background="@drawable/folwer1"
tools:context=".MainActivity" >
<TimePicker
android:id="@+id/timePickerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取設(shè)置時(shí)間"
android:layout_below="@id/timePickerId"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
二、DatePicker
1.DatePicker沒(méi)有像TimePicker一樣類(lèi)似OnTimeChangedListener的監(jiān)聽(tīng)器接口。有對(duì)話(huà)框,以后補(bǔ)充。
補(bǔ)充見(jiàn):DatePicker的對(duì)話(huà)框設(shè)置
模擬器android4.2效果圖:

手機(jī)android2.3.7效果圖:

java代碼:
復(fù)制代碼 代碼如下:
package com.haut.a07_datepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private DatePicker datePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = (DatePicker)findViewById(R.id.datePickerId);
button = (Button)findViewById(R.id.buttonId);
//為button創(chuàng)建監(jiān)聽(tīng)器
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
String date = datePicker.getYear() + "年:" + datePicker.getMonth() + "月:" + datePicker.getDayOfMonth() + "日";
Toast.makeText(MainActivity.this, date, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
xml代碼:
復(fù)制代碼 代碼如下:
<RelativeLayout 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:background="@drawable/leaf"
tools:context=".MainActivity" >
<DatePicker
android:id="@+id/datePickerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取設(shè)置日期"
android:layout_below="@id/datePickerId"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
三、AnalogClock
顯示的時(shí)鐘時(shí)間會(huì)隨著系統(tǒng)時(shí)間的變化而變化。
代碼比較簡(jiǎn)單就不貼了,只是在xml布局文件中添加一個(gè)<AnalogClock/>標(biāo)簽。
模擬器android4.2效果圖:

手機(jī)android2.3.7效果圖:

四、DigitalClock
顯示的時(shí)鐘時(shí)間會(huì)隨著系統(tǒng)時(shí)間的變化而變化。
模擬器android4.2效果圖:

手機(jī)android2.3.7效果圖:

xml代碼:
復(fù)制代碼 代碼如下:
<RelativeLayout 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:background="@drawable/folwer"
tools:context=".MainActivity" >
<DigitalClock
android:id="@+id/digitalClockId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textColor="#ff0000"
android:textSize="30sp" />
</RelativeLayout>
具體的操作以后用到再具體補(bǔ)充~
相關(guān)文章
Android 動(dòng)態(tài)顯示和隱藏狀態(tài)欄詳解及實(shí)例
這篇文章主要介紹了Android 動(dòng)態(tài)顯示和隱藏狀態(tài)欄的相關(guān)資料,需要的朋友可以參考下2017-06-06android dialog背景模糊化效果實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了android dialog背景模糊化效果的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android ListView分頁(yè)功能實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android ListView分頁(yè)功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android 中實(shí)現(xiàn)ListView滑動(dòng)隱藏標(biāo)題欄的代碼
本文通過(guò)實(shí)例代碼給大家分享了android listview滑動(dòng)隱藏標(biāo)題欄的方法,代碼簡(jiǎn)單易懂,需要的朋友參考下2017-01-01Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類(lèi)的ArrayList示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類(lèi)的ArrayList,涉及Android對(duì)象序列化、反序列化、Intent數(shù)據(jù)傳輸?shù)认嚓P(guān)操作技巧,需要的朋友可以參考下2017-08-08Android ViewPager實(shí)現(xiàn)選項(xiàng)卡切換
這篇文章主要介紹了Android ViewPager實(shí)現(xiàn)選項(xiàng)卡切換,詳細(xì)分析了ViewPager實(shí)現(xiàn)選項(xiàng)卡切換功能,感興趣的小伙伴們可以參考一下2016-02-02