无法使CalendarView将setDate设置为当前日期

问题描述 投票:1回答:2

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

公共类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);


}

}

我们是谁

    <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"/>
android calendarview
2个回答
1
投票

您甚至不将CalendarView强制转换为代码,请尝试以下操作:

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

代替:

CalendarView cal = new CalendarView(this);

0
投票

我用这个代码

private void Currentdate() {
    CalendarView cal = (CalendarView)findViewById(R.id.calendarView);
    long time = System.currentTimeMillis();
    cal.setDate(time,false,true);
}
© www.soinside.com 2019 - 2024. All rights reserved.