是否可以更改 numberpicker 中所选项目的颜色,以便每次出现新的中心子 TextView 时将其颜色更改为我喜欢的任何颜色我没有发现任何样式或 API 公开与此相关?
最佳答案
我想出了如何只为选定的数字更改颜色r。在您的 NumberPicker 中设置 OnTouchListener。
numberPicker.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View v, MotionEvent event) {
currentTouchAction = event.getAction();
if (currentTouchAction == MotionEvent.ACTION_UP) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (currentTouchAction == MotionEvent.ACTION_UP) {
setSelectedTextColor((NumberPicker)v, selectedColorRes);
}
}
}, 300);
}
return false;
}
});
和下面的代码用于更改所选数字的颜色。您可能必须使用“performClick()” 动态应用颜色。
如果你这样做,选择器会立即停止。所以这就是为什么我把 postDelayed(300) 放在上面。用户可能会多次拖动数字选择器来选择利润数字。如果放在postDelay()上面等几毫秒,就可以顺利滚动了。
public void setSelectedTextColor(NumberPicker np, int colorRes) {
final int count = np.getChildCount();
for(int i = 0; i < count; i++){
View child = np.getChildAt(i);
if(child instanceof EditText){
try{
Field selectorWheelPaintField = np.getClass().getDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
((EditText) child).setTextColor(mContext.getResources().getColor(colorRes));
np.performClick();
}
catch(NoSuchFieldException e){
}
catch(IllegalArgumentException e){
}
}
}
}
关于android - [Android]NumberPicker 选中项改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336374/