在Android应用程序中工作,这里我有一个 TimePicker ,带有微调模式,我的布局背景颜色是黑色的。所以我想改变 TimePicker的字体为白色 [小时,分钟,上午/下午]。
我已经从SO中引用并尝试了很多解决方案,但没有解决我的问题。
最后我找到了一个来源Change the text color of NumberPicker
我已实现如下
import java.lang.reflect.Field;
public class CheckOut extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mTimePicker = (TimePicker) findViewById(R.id.mTimePicker);
int mycolor = getResources().getColor(R.color.tab_background);
setTimePickerColor(mTimePicker,mycolor);
}
public static boolean setTimePickerColor(TimePicker timePicker, int color)
{
final int count = timePicker.getChildCount();
for(int i = 0; i < count; i++){
View child = timePicker.getChildAt(i);
if(child instanceof EditText){
try{
Field selectorWheelPaintField = timePicker.getClass()
.getDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
((Paint)selectorWheelPaintField.get(timePicker)).setColor(color);
((EditText)child).setTextColor(color);
timePicker.invalidate();
return true;
}
catch(NoSuchFieldException e){
Log.w("setNumberPickerTextColor", e);
}
catch(IllegalAccessException e){
Log.w("setNumberPickerTextColor", e);
}
catch(IllegalArgumentException e){
Log.w("setNumberPickerTextColor", e);
}
}
}
return false;
}
}
我不知道我在哪里犯了错误,因为它没有任何效果。
答案 0 :(得分:0)
使用android native date and time picker
的问题和缺点。
pre lollipop
和lollipop
设备中的日期和计时器选择器界面变化。 (我提到这个)
醇>
<强>解决方案强>
我总是使用MaterialDateTimePicker库来摆脱这个问题。这个库是高度可定制的,维护良好的仓库。
我正在分享您的基本活动类代码,您可以在基础活动类中添加或创建一个新代码。
您将通过此基类扩展您的活动类。并且只是调用我写的简单方法。
openTimeDialog(Calendar.getInstance(), new OnTimeSelected() {
@Override
public void onTimeSet(Calendar timeCalendar) {
}
});
<强> BaseProject.java 强>
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Calendar;
/**
* Created by KHEMRAJ on 8/1/2017.
*/
public class BaseProject extends Activity {
public interface OnDateSelected {
void onDateSet(Calendar dateCalendar);
}
public interface OnTimeSelected {
void onTimeSet(Calendar timeCalendar);
}
public void openDateDialog(OnDateSelected onDateSelected) {
openDateDialog(Calendar.getInstance(), null, null, onDateSelected);
}
public void openDateDialog(@NonNull Calendar preSelectedCalendar, final OnDateSelected onDateSelected) {
openDateDialog(preSelectedCalendar, null, null, onDateSelected);
}
public void openDateDialog(@NonNull Calendar preSelectedCalendar, @Nullable Calendar minDateCalander, final OnDateSelected onDateSelected) {
openDateDialog(preSelectedCalendar, minDateCalander, null, onDateSelected);
}
public void openDateDialog(@NonNull Calendar preSelectedCalendar, @Nullable Calendar minDateCalander, @Nullable Calendar maxDateCalander, final OnDateSelected onDateSelected) {
DatePickerDialog dpd = DatePickerDialog.newInstance(
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (onDateSelected != null) onDateSelected.onDateSet(calendar);
}
},
preSelectedCalendar.get(Calendar.YEAR),
preSelectedCalendar.get(Calendar.MONTH),
preSelectedCalendar.get(Calendar.DAY_OF_MONTH)
);
if (minDateCalander != null)
dpd.setMinDate(minDateCalander);
if (maxDateCalander != null)
dpd.setMaxDate(maxDateCalander);
dpd.show(getFragmentManager(), "Datepickerdialog");
}
public void openTimeDialog(@NonNull Calendar preSelectedCalendar, final OnTimeSelected onTimeSelected) {
TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
if (onTimeSelected != null) onTimeSelected.onTimeSet(calendar);
}
},
preSelectedCalendar.get(Calendar.HOUR_OF_DAY),
preSelectedCalendar.get(Calendar.MINUTE),
false);
timePickerDialog.show(getFragmentManager(), "TimePickerDialog");
}
}
添加此依赖项
compile 'com.wdullaer:materialdatetimepicker:3.6.0'
您可以按照link
中的说明更改字体颜色,背景颜色,日期时间文字颜色