Android - Toast

时间:2014-08-12 03:36:15

标签: java android android-toast

我有一个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而不是点击'Back'。 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();
        }
    });

1 个答案:

答案 0 :(得分:1)

当您取消选择一个图像视图时,这也应该有效。希望这会有所帮助.....

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();
    }
});