有人知道是否可以使ImageView可检查。我尝试在我的项目中使用State-list drawable资源,我为ImageView检查状态定义图片,但没有属性可以使ImageView可检查,只能点击。
也许有人知道解决这个问题的方法吗?
答案 0 :(得分:27)
使用此自定义ImageView实现(取自here):
public class CheckableImageView extends ImageView implements Checkable {
private boolean mChecked;
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
public CheckableImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
@Override
public int[] onCreateDrawableState(final int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked())
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
return drawableState;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void setChecked(final boolean checked) {
if (mChecked == checked)
return;
mChecked = checked;
refreshDrawableState();
}
}
答案 1 :(得分:12)
您可以将状态列表设置为可绘制的状态列表,而不是将ImageView设置为可选的复选框,以便自动翻转图像。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checkable="true" android:drawable="@drawable/icon_pressed" /> <!-- pressed -->
<item android:state_checked="true" android:drawable="@drawable/icon_focused" /> <!-- focused -->
<item android:drawable="@drawable/icon_default" /> <!-- default -->
</selector>
答案 2 :(得分:2)
我从Android开发人员的代码中实现OnClickListener以进行直接检查。
public class CheckableImageView extends ImageView implements Checkable, View.OnClickListener {
private boolean mChecked;
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
public CheckableImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
@Override
public int[] onCreateDrawableState(final int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked())
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
return drawableState;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void setChecked(final boolean checked) {
if (mChecked == checked)
return;
mChecked = checked;
refreshDrawableState();
}
@Override
public void onClick(View v) {
toggle();
}
}
答案 3 :(得分:0)
如果要在单击ImageView时更改检查状态,请将“extend ImageView”更改为CompoundButton。
public class CheckableImageView extends CompoundButton {
public CheckableImageView(Context context) {
super(context);
}
public CheckableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private static final int[] CheckedStateSet = {android.R.attr.state_checked};
public void setChecked(boolean b) {
boolean oldState = isChecked();
super.setChecked(b);
if (b != oldState) {
refreshDrawableState();
}
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
invalidate();
}
}
答案 4 :(得分:-1)
这是我在我的应用中使用的
public class MainActivity extends Activity implements OnClickListener {
private boolean paused = false;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageview_pause:
paused = !paused;
if (paused) {
btn_pause.setBackgroundResource(R.drawable.pause_key_pressed);
}else{
btn_pause.setBackgroundResource(R.drawable.pause_key);
}
break;
}
}
}