如何更改Calender对话的颜色Android



在我的应用程序中,我使用的是datePicker对话框和时间选择器对话框来相应地选择日期和时间。现在,在那些对话框中,颜色是基于我的theSecolor设置的。但是我想做的是手动更改这些颜色。对于Exmaple,我更改了工具栏颜色,以这种方式,工具栏。setBackgroundColor(constant.Color);但是,我如何以手动相同的方式更改标题颜色(在当前日期显示的位置 - 我已附加了图片)和datepicker颜色(指示当前日期的圆形拾取器)。

这是我的deatepickerdialog

的图像

datepicker对话框

这是我的日期选择器对话框的代码

public class TodoAddActivity extends AppCompatActivity {
// Variables
private Context mContext;
....
private RealmResults<TodoModel> results;
public final static String INTENT_KEY_ID = "taskId";
public final static String DATE_FORMAT = "dd/MMM/yy";
Constant constant;
SharedPreferences app_preferences;
int appTheme;
int themeColor;
int appColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView( R.layout.todo_add_layout);
    app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    appColor = app_preferences.getInt("color", 0xff3F51B5);
    appTheme = app_preferences.getInt("theme", 0xff3F51B5);
    themeColor = appColor;
    constant.color = appColor;
    if (themeColor == 0){
        setTheme(Constant.theme);
    }else if (appTheme == 0){
        setTheme(Constant.theme);
    }else{
        setTheme(appTheme);
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_add);
    toolbar.setBackgroundColor(Constant.color);
    setSupportActionBar(toolbar);
    mContext = this;
    realm=Realm.getDefaultInstance();
    setUpUIViews();
    showData();
    final String taskId = getIntent().getStringExtra(INTENT_KEY_ID);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (taskId == null)
                addTask();
            else
                editTask(taskId);
        }
    });
    mTaskDateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            datePicker(view);
        }
    });
    mTaskDateEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b)
                datePicker(view);
        }
    });
    mTaskTimeEditText.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timePicker( view );
        }
    } );
    mTaskTimeEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b)
                timePicker( view );
        }
    });
    if (taskId != null) {
        fillTask(realm.where(TodoModel.class).equalTo("id", taskId).findFirst());
        button.setText("EDIT");
    }

}
private void setUpUIViews() {
    ...
}
public void showData(){
    //tasks = new ArrayList<>(); //error
    //RealmResults<TodoModel> tasksResult = realm.where(TodoModel.class).findAll();
    //for (TodoModel task : tasksResult)
    //    tasks.add(task); //error*/
    results = realm.where(TodoModel.class).findAll();
}
protected TodoModel getTask(int position) {
    return results.get(position);
}
private void fillTask(TodoModel task) {
   ...
}
private void addTask() {
    ...
}
protected void addTask(TodoModel task) {
   ..
}
private void editTask(String taskId) {
    S....
}
protected void updateTask(TodoModel task) {
   ....
}

private void datePicker(final View view) {
    Calendar currentTime = Calendar.getInstance();
    int year = currentTime.get(Calendar.YEAR);
    int monthOfYear = currentTime.get(Calendar.MONTH);
    int dayOfMonth = currentTime.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext,0, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, monthOfYear, dayOfMonth);
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            ((EditText) view).setText(sdf.format(calendar.getTime()));
        }
    }, year, monthOfYear, dayOfMonth);
    mDatePickerDialog.setTitle("Select Time");
    mDatePickerDialog.show();
}
private void timePicker(final View view) {
    Calendar mcurrentTime = Calendar.getInstance();
    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
    int minute = mcurrentTime.get(Calendar.MINUTE);
    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(mContext,0, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
            mTaskTimeEditText.setText( selectedHour + ":" + selectedMinute);
        }
    }, hour, minute, true);//Yes 24 hour time
    mTimePicker.setTitle("Select Time");
    mTimePicker.show();
}

}

尝试这样做:

在您的styles.xml中添加以下内容:

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/yourColor</item>
</style>

然后在您的datepicker中做这个。

DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener()
new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //Your Code
}
}, year, monthOfYear, dayOfMonth).show();

编辑 - 将主题更改为theme.appcompat.light.dialog,建议

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
      <item name="colorAccent">@color/xyz</item>
</style>

相关内容

  • 没有找到相关文章

最新更新