2017-09-11 98 views
0

在我的應用程序中,我使用DatePicker對話框和時間選擇器對話框來相應地選擇日期和時間。現在,在那些對話框中,根據我的Themecolor設置顏色。但我想做什麼,是手動改變這些顏色。例如,我改變了工具欄的顏色,以這種方式toolbar.setBackgroundColor(Constant.color);但是,如何以同樣的方式手動更改標題顏色(當前日期顯示 - 我已附加圖片)和日期選擇器顏色(指示當前日期的圓形選擇器)。如何更改日曆對話框的顏色Android

這裏是我的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(); 

} 

}

回答

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> 
+0

謝謝你的回答。但在我的應用程序中,我有一個設置選項,我在這裏設置了很多顏色選項來根據用戶選擇設置colr,我給出了我的代碼,在這裏我設置了工具欄選項。但我想知道它有什麼方法來改變日曆的標題顏色,當前時間顯示 – tamrezh21

0

試着這樣做:

在你styles.xml添加以下內容:

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

然後在你的日期選擇器做到這一點,而不是。

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

@MartinThank你的答案。但在我的應用程序中,我有一個設置選項,我在這裏設置了很多顏色選項來根據用戶選擇設置colr,我給出了我的代碼,在這裏我設置了工具欄選項。但我想知道它有什麼方法來改變當前時間顯示的日曆的標題顏色 – tamrezh21

相關問題