如何为" TODAYS DATE"的日历视图设置onclick监听器。 ?该代码适用于onclick昨天的日期或明天的日期,但它不适用于今天的日期?有人可以帮忙吗?
代码:
MainActivity.java :
final CalendarView calendarview=(CalendarView)findViewById(R.id.Calendarview);
final Button selectDate=(Button)findViewById(R.id.Date_seletcion_btn);
selectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
calendarview.setVisibility(View.VISIBLE);
}
});
calendarview.setClickable(true);
//calendarview.getSelectedDateVerticalBar();
/*calendarview.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView calendarview, int year, int month,
int day) {
// TODO Auto-generated method stub
int day1, month1, year1;
day1=day;
month1=month;
year1=year;
Calendar calendar=Calendar.getInstance();
clickeddate=calendarview.getDate();
calendar.setTimeInMillis(clickeddate);
//ArrayList<> array=calendar.getTouchables();
Toast.makeText(getApplicationContext(), "Date Selected: " + day,Toast.LENGTH_LONG).show();
Intent homein=new Intent(getApplicationContext(),HomePageActivity.class);
homein.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
homein.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homein);
finish();
}
}); */
** XML代码:**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Calendar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<!--
<LinearLayout
android:id="@+id/Calendar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
-->
<Button
android:id="@+id/Date_seletcion_btn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/button_states"
android:text="@string/calendarselection" />
<!--
<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
-->
<CalendarView
android:id="@+id/Calendarview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:visibility="gone"
android:clickable="true" />
</LinearLayout>
答案 0 :(得分:0)
正如名称setOnDateChangeListener所说,当您更改日历上的日期时,将调用此方法。今天与否无关紧要。当您打开calendarview时,默认显示今天的日期。所以当你点击“今天”时,方法不会被调用。因为没有变化。选择其他日期,然后尝试选择“今天”。所以这个方法在变化时被调用。 In SO, this question was already answered。只需创建稍后将使用的自定义CalendarView类。
答案 1 :(得分:0)
对话框类需要betterpickers libary
public class DateDialog {
private static final SimpleDateFormat sSDF = new SimpleDateFormat("EEE", Locale.US);
private CalendarDatePickerDialog mDialog;
private FragmentManager mFragmentManager;
public DateDialog(FragmentManager fm, OnDateSetListener listener) {
mFragmentManager = fm;
Calendar c = Calendar.getInstance();
String EEE = sSDF.format(c.getTime());
if (EEE.contains("Sun")) {
c.set(Calendar.DAY_OF_MONTH, -2);
}
mDialog = CalendarDatePickerDialog.newInstance(listener, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
public void show() {
mDialog.show(mFragmentManager, "calendarDialog");
}
}
//否则创建BaseActivity类
public abstract class BaseActivity extends FragmentActivity {
private final SimpleDateFormat sSDF = new SimpleDateFormat("EEE", Locale.US);
public void showCalendarDialog(OnDateSetListener listener, FragmentManager fm) {
Calendar c = Calendar.getInstance();
String EEE = sSDF.format(c.getTime());
if (EEE.contains("Sun")) {
c.set(Calendar.DAY_OF_MONTH, -2);
}
CalendarDatePickerDialog mDialog = CalendarDatePickerDialog.newInstance(listener, c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
mDialog.setCancelable(true);
mDialog.show(fm, FRAG_TAG_DATE_PICKER);
}
}
MainActivity.class
MainActivity extends FragmentActivity implements OnDateSetListener{
private static final SimpleDateFormat sSDF_dmye = new SimpleDateFormat("dd/MM/yyyy (EEE)", Locale.US);
//call method when you want to click this action listener
//call this mothod on btn click action
e.g showCalendarDialog(this, getSupportFragmentManager());
}
答案 2 :(得分:0)
OnGlobalLayoutListener
会做到这一点。
所以,如果你尝试:
calendarView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
@Override
public void onGlobalLayout()
{}
});
答案 3 :(得分:0)
Calendar disabledCal = Calendar.getInstance();
disabledCal.set(Calendar.DATE, disabledCal.get(Calendar.DATE) - 1);