我有一个ImageViews数组。单击这些ImageView即可选择它们。它们的笔触属性从0(未选中)到4(已选中)变化。现在,我有两个按钮,“ ClearAll”和“ Back”。如果单击后退按钮,则所有选中的ImageViews描述将显示在Toast消息中(例如,“您已选择iv1,iv2,iv3”)。 “ ClearAll”按钮的作用是清除选择-假设我单击了ImageView 1、2和3-如果单击“ ClearAll”,则它们的笔划将回到0(指示所有选择均被清除) 。但是我有一个问题,假设我单击了ImageView 1、2和3,然后改变了主意,我想单击ImageView 4、5、6,然后单击“返回”。 Toast消息显示了我也清除过的那些ImageView的描述,输出是这样的,“您选择了iv1,iv2,iv3,iv4,iv5,iv6。如何修复我的代码,以便已被“ ClearAll”清除,不会在Toast中显示?
这是上面每个代码的片段。
ImageViews:
for (int i = 0 ; i < color.length ; i++) {
color[i] = (ImageView) findViewById (ivCirclesId[i]);
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
color[i].setTag ("0");
color[i].setOnClickListener (new OnClickListener() {
public void onClick (View v) {
if (v.getTag ().toString ().equals ("1")) {
--ctr;
v.setTag ("0");
ivCircleOnClick.setStroke (0, Color.WHITE);
} else {
if(ctr >= 8) {
Toast.makeText (getApplicationContext (), "Limit reached!", Toast.LENGTH_SHORT).show ();
return;
}
++ctr;
v.setTag ("1");
ivCircleOnClick.setStroke (4, Color.WHITE);
message = message + "#" + v.getContentDescription ();
return;
}
}
});
ClearAll:
Button btnClear = (Button) findViewById (R.id.btnClearAll);
btnClear.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
for (int i = 0 ; i < color.length; i++) {
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
ivCircleOnClick.setStroke (0, Color.WHITE);
color[i].setTag ("0");
ctr = 0;
}
}
});
背部:
Button btnBack = (Button) findViewById (R.id.btnBack);
btnBack.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
if (ctr >= 1) {
Toast.makeText (Filter.this, message, Toast.LENGTH_SHORT).show();
message = "";
}
finish();
}
});
最佳答案
当您仅取消选择一个图像视图时,这也应该起作用。希望这可以帮助.....
ImageViews:
for (int i = 0 ; i < color.length ; i++) {
color[i] = (ImageView) findViewById (ivCirclesId[i]);
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
color[i].setTag ("0");
color[i].setOnClickListener (new OnClickListener() {
public void onClick (View v) {
if (v.getTag ().toString ().equals ("1")) {
--ctr;
v.setTag ("0");
ivCircleOnClick.setStroke (0, Color.WHITE);
} else {
if(ctr >= 8) {
Toast.makeText (getApplicationContext (), "Limit reached!", Toast.LENGTH_SHORT).show ();
return;
}
++ctr;
v.setTag ("1");
ivCircleOnClick.setStroke (4, Color.WHITE);
//message = message + "#" + v.getContentDescription ();
return;
}
}
});
ClearAll:
Button btnClear = (Button) findViewById (R.id.btnClearAll);
btnClear.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
for (int i = 0 ; i < color.length; i++) {
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
ivCircleOnClick.setStroke (0, Color.WHITE);
color[i].setTag ("0");
ctr = 0;
message = "";
}
}
});
背部:
Button btnBack = (Button) findViewById (R.id.btnBack);
btnBack.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
message = "";
for(int i = 0 ; i < color.length ; i++){
if(color[i].getTag().toString().equals("1")){
message = message + "#" + color[i].getContentDescription();
}
}
if (ctr >= 1) {
Toast.makeText (Filter.this, message, Toast.LENGTH_SHORT).show();
message = "";
}
//finish();
}
});