我想将当前日期设置为标题栏中的标题并使其具有响应性,如果我单击标题(当前日期),则将出现一个日历对话框,用于设置日期并根据所选日期必须显示数据。我尝试将文本视图添加到标题栏。但是我没有
答案 0 :(得分:0)
首先在布局xml中设置工具栏
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="8dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
然后在Activity类中,获取当前日期并设置为标题
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
ToolBar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle(date); //This is to set the current date as title
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
mActionBarToolbar.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"Clicked");
new DatePickerDialog(classname.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
将此方法添加到活动中-
private void updateLabel() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
mActionBarToolbar.setText(sdf.format(myCalendar.getTime()));
}