android - 无法将 CalendarView 设置为当前日期

标签 android calendarview

我在这里四处搜索,并通过谷歌试图找到可以帮助我关注系统当前日期的内容。我尝试使用 .setDate(long,boolean,boolean) 但它似乎不起作用。

公共(public)类 MainActivity 扩展了 ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void Currentdate(View view) {
    CalendarView cal = new CalendarView(this);
    long time = System.currentTimeMillis();
    cal.setDate(time,false,true);


}

我的xml

    <CalendarView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/calendarView"
    android:layout_gravity="center_horizontal"
    android:firstDayOfWeek="2"
    android:weekNumberColor="#ff8b8b88"
    android:clickable="true"/>

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Current date"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/calendarView"
    android:layout_alignEnd="@+id/calendarView"
    android:onClick="Currentdate"/>

最佳答案

您甚至不用将 CalendarView 转换到您的代码中,试试这个:

CalendarView cal = (CalendarView)findViewById(R.id.calendarView);

代替:

CalendarView cal = new CalendarView(this);

关于android - 无法将 CalendarView 设置为当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29581056/

相关文章:

java - Android 中的后退按钮就像 Gmail 应用程序中的后退按钮吗?

java - 如何以编程方式获取我的设备的手机号码?

Android - 扩展 CalendarView

安卓日历 View

android - Gradle Jenkins Lint 报告

android - Google API 与 Google Play 服务,为什么我们现在需要 Google API?

Android ObjectAnimator 动画 translationY 在所有屏幕尺寸上的比例相同

android - Lollipop CalenderView DatePicker 不调用 OnDateChanged() 方法

android - CalendarView,需要示例

Android - setFocusedMonthDateColor() 和 setUnfocusedMonthDateColor() 在 SDK 级别 23 中已弃用