我想编写一个具有按钮的应用程序,当我单击按钮时,我想在对话框中显示日历,例如对话框中的 Datepicker。
我试着看构造new DatePickerDialog.OnDateSetListener()
器喜欢日历。难道不能这样做吗?
在 api 级别 11 之后,它支持日历视图。
您好,请尝试以下代码,希望对您有所帮助:
<Button
android:id="@+id/birthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
现在将此按钮调用到您的活动中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
editBirthday = (Button)findViewById(R.id.birthday);
/*
* Change Birth day on click of edit box
*/
editBirthday.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editBirthday.getWindowToken(), 0);
showDialog(DATE_DIALOG_ID);
return true;
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, year, month,
day);
}
return null;
}
// updates the date we display in the TextView
private void updateDisplay() {
/*
* Hide virtual keyboard
*/
editBirthday.setText(new StringBuilder()
// Month is 0 based so add 1
.append(year).append("-").append(month + 1).append("-")
.append(day).append(""));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int myear, int monthOfYear,
int dayOfMonth) {
year = myear;
month = monthOfYear;
day = dayOfMonth;
updateDisplay();
}
};
//此代码在对话框中显示日历,希望对您有所帮助
LayoutInflater inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.calendar, null, false);
CalendarView cv = (CalendarView) ll.findViewById(R.id.calendarView);
cv.setBackgroundColor(Color.BLACK);
cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
// TODO Auto-generated method stub
Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);
int rows = showMapFragment(year, month, dayOfMonth);
if (rows == 0){
Toast.makeText(getApplicationContext(), year + "/" + (month + 1) +"/" + dayOfMonth + " No route to display", Toast.LENGTH_SHORT).show();
}
}
});
ll.findViewById(R.id.calendarView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("calendar view", "calendar view clicked");
}
});
Dialog calendarDialog = new Dialog(Main.this);
calendarDialog.setContentView(ll);
calendarDialog.setTitle("Pick a date to view your performance and route");
calendarDialog.show();
//下面是日历布局xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height= "400dp"
android:showWeekNumber="false"
android:clickable="true"
android:weekDayTextAppearance="@style/calendarStyle"
android:dateTextAppearance="@style/calendarStyle"
/>
</LinearLayout>