如何更改Calender Dialog Android的颜色

时间:2017-09-11 09:18:56

标签: android datepicker calendar app-themes

在我的应用程序中,我使用DatePicker对话框和时间选择器对话框来相应地选择日期和时间。现在,在那些对话框中,颜色是根据我的Themecolor设置的。但我想做的是手动改变这些颜色。为了exmaple我已经改变了工具栏颜色,就这样工具栏.setBackgroundColor(Constant.color);但是,如何手动以相同的方式更改标题颜色(当前日期显示 - 我已附加图片)和日期选择器颜色(指示当前日期的圆形选择器)。

这是我的DeatepickerDialog

的图片

DatePicker Dialog

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

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();

}

}

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

尝试这样做:

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

DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener()

然后在你的datePicker中改为做。

{{1}}