我想根据事实更改我的图像按钮的图像,如果数据库中有某些内容。我决定通过自定义选择器执行此操作,其中包含以下属性:空,满,警告和全部。
现在当我启动MainActivity时,我设置了对db的调用并询问它是否为空。如果不是,则属性应该从empty更改:true,full:false,warning:false和alltaken:false to empty:false,full:true,warning:false and alltaken:false。
我的CustomImageButton类如下:
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
private static final int[] STATE_EMPTY = {R.attr.state_empty};
private static final int[] STATE_FULL = {R.attr.state_full};
private static final int[] STATE_WARNING = {R.attr.state_warning};
private static final int[] STATE_ALLTAKEN = {R.attr.state_alltaken};
private boolean mIsEmpty = false;
private boolean mIsFull = false;
private boolean mIsWarning = false;
private boolean mIsAlltaken = false;
public void setEmpty(boolean isEmpty) {mIsEmpty = isEmpty;}
public void setFull(boolean isFull) {mIsFull = isFull;}
public void setWarning(boolean isWarning) {mIsWarning = isWarning;}
public void setAlltaken(boolean isAlltaken) {mIsAlltaken = isAlltaken;}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (mIsEmpty) {
mergeDrawableStates(drawableState, STATE_EMPTY);
}
if (mIsFull) {
mergeDrawableStates(drawableState, STATE_FULL);
}
if (mIsWarning) {
mergeDrawableStates(drawableState, STATE_WARNING);
}
if (mIsAlltaken) {
mergeDrawableStates(drawableState, STATE_ALLTAKEN);
}
return drawableState;
}
在我的MainActivity中,我将这些函数称为:
CustomImageButton mondaybtn, tuesdaybtn, wednesdaybtn, thursdaybtn, fridaybtn, saturdaybtn, sundaybtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mondaybtn = (CustomImageButton) findViewById(R.id.iBtnMonday);
tuesdaybtn = (CustomImageButton) findViewById(R.id.iBtnTuesday);
wednesdaybtn = (CustomImageButton) findViewById(R.id.iBtnWednesday);
thursdaybtn = (CustomImageButton) findViewById(R.id.iBtnThursday);
fridaybtn = (CustomImageButton) findViewById(R.id.iBtnFriday);
saturdaybtn = (CustomImageButton) findViewById(R.id.iBtnSaturday);
sundaybtn = (CustomImageButton) findViewById(R.id.iBtnSunday);
getDBInfo();
}
private void getDBInfo(){
sqlcon = new SQLController(this);
sqlcon.open();
Cursor c = sqlcon.readEntry();
int rows = c.getCount();
Toast.makeText(MainActivity.this, "Rows in DB:" + String.valueOf(rows), Toast.LENGTH_LONG).show();
if(rows >= 1){
//ToDo all pictures full
setCustomButtonImage(mondaybtn, false, true, false, false);
setCustomButtonImage(tuesdaybtn, false, true, false, false);
setCustomButtonImage(wednesdaybtn, false, true, false, false);
setCustomButtonImage(thursdaybtn, false, true, false, false);
setCustomButtonImage(fridaybtn, false, true, false, false);
setCustomButtonImage(saturdaybtn, false, true, false, false);
setCustomButtonImage(sundaybtn, false, true, false, false);
setDisplayViewButtonClickListener(R.id.iBtnMonday, MedListActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnTuesday, MedListActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnWednesday, MedListActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnThursday, MedListActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnFriday, MedListActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnSaturday, MedListActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnSunday, MedListActivity.class);
}
else{
setCustomButtonImage(mondaybtn, true, false, false, false);
setCustomButtonImage(tuesdaybtn, true, false, false, false);
setCustomButtonImage(wednesdaybtn, true, false, false, false);
setCustomButtonImage(thursdaybtn, true, false, false, false);
setCustomButtonImage(fridaybtn, true, false, false, false);
setCustomButtonImage(saturdaybtn, true, false, false, false);
setCustomButtonImage(sundaybtn, true, false, false, false);
//ToDo all pictures empty
setDisplayViewButtonClickListener(R.id.iBtnMonday, EnterMedActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnTuesday, EnterMedActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnWednesday, EnterMedActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnThursday, EnterMedActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnFriday, EnterMedActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnSaturday, EnterMedActivity.class);
setDisplayViewButtonClickListener(R.id.iBtnSunday, EnterMedActivity.class);
}
}
private void setCustomButtonImage(CustomImageButton btn,Boolean empty, Boolean full, Boolean warning,Boolean alltaken){
btn.setEmpty(empty);
btn.setFull(full);
btn.setWarning(warning);
btn.setAlltaken(alltaken);
}
在drawable中,我有这样的文件用于选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dailymeds="http://schemas.android.com/tools">
<item
dailymeds:state_empty="true"
dailymeds:state_full="false"
dailymeds:state_warning="false"
dailymeds:state_alltaken="false"
android:state_pressed="false"
android:drawable="@drawable/emptyshapefriday" />
<item
dailymeds:state_empty="true"
dailymeds:state_full="false"
dailymeds:state_warning="false"
dailymeds:state_alltaken="false"
android:state_pressed="true"
android:drawable="@drawable/demptyshapefriday" />
<item
dailymeds:state_full="true"
dailymeds:state_empty="false"
dailymeds:state_warning="false"
dailymeds:state_alltaken="false"
android:state_pressed="false"
android:drawable="@drawable/fullshapefriday" />
<item
dailymeds:state_full="true"
dailymeds:state_empty="false"
dailymeds:state_warning="false"
dailymeds:state_alltaken="false"
android:state_pressed="true"
android:drawable="@drawable/dfullshapefriday" /> ....
我还在res / values
中拥有所需的资源文件<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="dailymeds">
<attr name="state_empty" format="boolean" />
<attr name="state_full" format="boolean" />
<attr name="state_warning" format="boolean" />
<attr name="state_alltaken" format="boolean" />
</declare-styleable>
</resources>
为什么即使我更改空,满,警告和所有属性,图像也不会改变?