我在这里搜索,谷歌试图找到可以帮助我关注系统当前日期的内容。我尝试使用.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);
}
}
我的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/