android怎么给datepickerdialog设定范围
1个回答
2016-01-12 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517194
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
自定义dialog实现的,在DatePicker的onDateChangedListentr事件中实现范围的控制
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView) findViewById(R.id.text);
LayoutInflater l = LayoutInflater.from(this);
View v = l.inflate(R.layout.dialog, null);
final DatePicker datePicker = (DatePicker) v
.findViewById(R.id.datepicker);
datePicker.init(2000, 1, 3, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (isDateAfter(view)) {
view.init(2000, 11, 30, this);
}
if (isDateBefore(view)) {
view.init(1949, 11, 30, this);
}
}
private boolean isDateAfter(DatePicker tempView) {
if (tempView.getYear() > 2000) {
return true;
} else
return false;
}
private boolean isDateBefore(DatePicker tempView) {
if (tempView.getYear() < 1921) {
return true;
} else
return false;
}
});
Dialog dialog = new AlertDialog.Builder(this)
.setTitle(
datePicker.getYear() + "年"
+ (datePicker.getMonth() + 1) + "月"
+ datePicker.getDayOfMonth() + "日")
.setView(v)
.setIcon(R.drawable.ic_dialog_time)
.setNeutralButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
text.setText(datePicker.getYear() + "年"
+ (datePicker.getMonth() + 1) + "月"
+ datePicker.getDayOfMonth() + "日");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
dialog.show();
}
}
自定义的dialog.xml代码如下【3.0版本之前】
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" " target="_blank">http://schemas.android.com/apk/res/android" </a>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
【在3.0版本以后】
[java] view plaincopy
public void onClick(View v) {
switch (answer.type) {
case 1:// 日期类型
DatePickerDialog dialog = new DatePickerDialog(ctx, this, 1980, 0,
1);
DatePicker datePicker = dialog.getDatePicker();
datePicker.setMinDate(DateUtils.getInstance().format("1970-01-01")
.getTime());
datePicker.setMaxDate(DateUtils.getInstance().format("2013-01-07")
.getTime());
dialog.show();
break;
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
editText.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth
+ "日");
}
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView) findViewById(R.id.text);
LayoutInflater l = LayoutInflater.from(this);
View v = l.inflate(R.layout.dialog, null);
final DatePicker datePicker = (DatePicker) v
.findViewById(R.id.datepicker);
datePicker.init(2000, 1, 3, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (isDateAfter(view)) {
view.init(2000, 11, 30, this);
}
if (isDateBefore(view)) {
view.init(1949, 11, 30, this);
}
}
private boolean isDateAfter(DatePicker tempView) {
if (tempView.getYear() > 2000) {
return true;
} else
return false;
}
private boolean isDateBefore(DatePicker tempView) {
if (tempView.getYear() < 1921) {
return true;
} else
return false;
}
});
Dialog dialog = new AlertDialog.Builder(this)
.setTitle(
datePicker.getYear() + "年"
+ (datePicker.getMonth() + 1) + "月"
+ datePicker.getDayOfMonth() + "日")
.setView(v)
.setIcon(R.drawable.ic_dialog_time)
.setNeutralButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
text.setText(datePicker.getYear() + "年"
+ (datePicker.getMonth() + 1) + "月"
+ datePicker.getDayOfMonth() + "日");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
dialog.show();
}
}
自定义的dialog.xml代码如下【3.0版本之前】
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" " target="_blank">http://schemas.android.com/apk/res/android" </a>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
【在3.0版本以后】
[java] view plaincopy
public void onClick(View v) {
switch (answer.type) {
case 1:// 日期类型
DatePickerDialog dialog = new DatePickerDialog(ctx, this, 1980, 0,
1);
DatePicker datePicker = dialog.getDatePicker();
datePicker.setMinDate(DateUtils.getInstance().format("1970-01-01")
.getTime());
datePicker.setMaxDate(DateUtils.getInstance().format("2013-01-07")
.getTime());
dialog.show();
break;
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
editText.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth
+ "日");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询