1,设置默认的字体大小和颜色
在你的Androidmanifest.xml 对应的 android:theme="" 中添加:
- @style/Widget.EditText.White
//上面所对应的style如下分别是设置默认字体和颜色
//其他方式如直接在android:theme对应的style设置editTextColor,textColor等都无效
2 自定义 DatePicker 设置分割线以及选中的字体和颜色
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import org.elastos.wallet.R;
import org.elastos.wallet.ela.utils.ScreenUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* DatePicker 是由多个NumberPicker组成
* 所有我们需要的是想方设法修改NumberPicker的ui
**/
public class TextConfigDataPicker extends DatePicker {
public TextConfigDataPicker(Context context) {
super(context);
}
public TextConfigDataPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextConfigDataPicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child) {
this.addView(child, null);
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
this.addView(child, 0, params);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
private void updateView(View view1) {
if (view1 instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view1;
//获得所有NumberPicker
List numberPickers = findNumberPicker(viewGroup);
for (NumberPicker np : numberPickers) {
//修改NumberPicker的ui
for (EditText editText : findEditText1(np)) {
//获得所有的edittext并修改ui 这里只有选中的edittext
editText.setTextColor(getResources().getColor(R.color.white));
editText.setTextSize(16);
editText.setGravity(Gravity.CENTER);
resizeNumberPicker(np);//修改NumberPicker的宽高
np.setWrapSelectorWheel(false);//是否循环
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//是否可编辑
np.setOnScrollListener(new NumberPicker.OnScrollListener() {
@Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
view.performClick();//刷新选中状态
}
});
}
try {
//设置分割线大小颜色
Field mSelectionDivider = getFile("mSelectionDivider");
mSelectionDivider.set(np, new ColorDrawable(getResources().getColor(R.color.pickline)));
mSelectionDivider.set(np, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void updateUI(ViewGroup viewGroup) {
List npList = findNumberPicker(viewGroup);
for (NumberPicker np : npList) {
for (EditText editText : findEditText1(np)) {
editText.setTextColor(getResources().getColor(R.color.white));
editText.setTextSize(16);
editText.setGravity(Gravity.CENTER);
resizeNumberPicker(np);
np.setWrapSelectorWheel(false);//是否循环
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//是否可编辑
np.setOnScrollListener(new NumberPicker.OnScrollListener() {
@Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
view.performClick();//刷新选中状态
}
});
}
try {
//设置分割线大小颜色
Field mSelectionDivider = getFile("mSelectionDivider");
mSelectionDivider.set(np, new ColorDrawable(getResources().getColor(R.color.pickline)));
mSelectionDivider.set(np, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private List findEditText1(ViewGroup viewGroup) {
List npList = new ArrayList();
View child = null;
if (null != viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
child = viewGroup.getChildAt(i);
if (child instanceof EditText) {
npList.add((EditText) child);
} else if (child instanceof LinearLayout) {
List result = findEditText1((ViewGroup) child);
if (result.size() > 0) {
return result;
}
}
}
}
return npList;
}
private EditText findEditText(NumberPicker np) {
if (null != np) {
for (int i = 0; i < np.getChildCount(); i++) {
View child = np.getChildAt(i);
if (child instanceof EditText) {
return (EditText) child;
}
}
}
return null;
}
/**
* 得到viewGroup 里面的numberpicker组件
*/
private List findNumberPicker(ViewGroup viewGroup) {
List npList = new ArrayList();
View child = null;
if (null != viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
child = viewGroup.getChildAt(i);
if (child instanceof NumberPicker) {
npList.add((NumberPicker) child);
} else if (child instanceof LinearLayout) {
List result = findNumberPicker((ViewGroup) child);
if (result.size() > 0) {
return result;
}
}
}
}
return npList;
}
/**
* 调整numberpicker大小
*/
private void resizeNumberPicker(NumberPicker np) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ScreenUtil.dp2px(np.getContext(), 100), ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(ScreenUtil.dp2px(np.getContext(), 5), 0, ScreenUtil.dp2px(np.getContext(), 5), 0);
np.setLayoutParams(params);
}
//反射获取控件 mSelectionDivider mInputText当前选择的view
public Field getFile(String fieldName) {
try {
//设置分割线的颜色值
Field pickerFields = NumberPicker.class.getDeclaredField(fieldName);
pickerFields.setAccessible(true);
return pickerFields;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
用法
TextConfigDataPicker datePicker = dialog.findViewById(R.id.np);
// datePicker.updateUI(datePicker);//如果你不想addview是调用updateView