无法将 CalendarView 设置为当前日期
Can't get CalendarView to setDate to current date
我在这里四处搜索,google 试图找到可以帮助我关注系统当前日期的内容。我尝试使用 .setDate(long,boolean,boolean) 但它似乎不起作用。
public class 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);
我用这个代码
private void Currentdate() {
CalendarView cal = (CalendarView)findViewById(R.id.calendarView);
long time = System.currentTimeMillis();
cal.setDate(time,false,true);
}
我在这里四处搜索,google 试图找到可以帮助我关注系统当前日期的内容。我尝试使用 .setDate(long,boolean,boolean) 但它似乎不起作用。
public class 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);
我用这个代码
private void Currentdate() {
CalendarView cal = (CalendarView)findViewById(R.id.calendarView);
long time = System.currentTimeMillis();
cal.setDate(time,false,true);
}