Android 的 DatePicker、TimePicker或NumberPicker

布局文件加上这个就可以,去除日期选择器、时间选择器或数值选择器的可编辑状态。

android:descendantFocusability="blocksDescendants"

CalendarView项目

<LinearLayout 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:descendantFocusability="blocksDescendants"
android:orientation="vertical"
tools:context="com.example.googlenumberpicker.MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" > <NumberPicker
android:id="@+id/np_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <NumberPicker
android:id="@+id/np_hour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" /> <NumberPicker
android:id="@+id/np_minute"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</NumberPicker>
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>

layout_main.xml

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener; public class MainActivity extends Activity implements OnValueChangeListener, OnScrollListener, Formatter
{
int year;
int month;
int day;
int hour;
int minute;
String[] dates; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
this.initDates();
this.init();
} private void initDates() {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.set(year, 0, 1);
dates = new String[c.getActualMaximum(Calendar.DAY_OF_YEAR)];
for (int i = 0; i < dates.length; i++) {
dates[i] = this.format((c.get(Calendar.MONTH)+1)) + "-" + this.format(c.get(Calendar.DAY_OF_MONTH));
c.roll(Calendar.DAY_OF_YEAR, true);
}
} private void init() {
NumberPicker np_date = (NumberPicker) this.findViewById(R.id.np_date);
NumberPicker np_hour = (NumberPicker) this.findViewById(R.id.np_hour);
NumberPicker np_minute = (NumberPicker) this.findViewById(R.id.np_minute); np_date.setFormatter(this);
np_date.setOnScrollListener(this);
np_date.setOnValueChangedListener(this);
np_date.setDisplayedValues(dates);
np_date.setMaxValue(dates.length - 1);
np_date.setValue(Calendar.getInstance().get(Calendar.DAY_OF_YEAR)-1); np_hour.setFormatter(this);
np_hour.setOnScrollListener(this);
np_hour.setOnValueChangedListener(this);
np_hour.setMaxValue(23);
np_hour.setMinValue(0);
np_hour.setValue(hour); np_minute.setFormatter(this);
np_minute.setOnScrollListener(this);
np_minute.setOnValueChangedListener(this);
np_minute.setMaxValue(59);
np_minute.setMinValue(0);
np_minute.setValue(minute);
} @Override
public String format(int value) {
String tmpStr = String .valueOf(value);
if (value < 10) {
tmpStr = "0" + tmpStr;
}
return tmpStr;
} @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// Toast.makeText(MainActivity.this, oldVal + "变成" + newVal , 0).show();
} @Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
/*switch (scrollState) {
case OnScrollListener.SCROLL_STATE_FLING:
Toast.makeText(MainActivity.this, "后续滑动..." , 0).show();
break;
case OnScrollListener.SCROLL_STATE_IDLE:
Toast.makeText(MainActivity.this, "不滑动" , 0).show();
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
Toast.makeText(MainActivity.this, "滑动中" , 0).show();
break; default:
break;
}*/
} }

MainActivity.java

Android 的 DatePicker、TimePicker或NumberPicker

Android日期对话框NumberPicker的用法教程 - Android移动开发技术文章_手机开发 - 红黑联盟

疑难杂症专解:Stack Overflow

上一篇:UVA_10055:Hashmat the brave warrior


下一篇:Summary on deep learning framework --- Torch7