再次单击复选框,不会显示ImageView

时间:2015-07-25 08:40:39

标签: android checkbox imageview

我有一个奇怪的问题。当我第一次点击复选框时,它可以工作,但当我取消选中并再次检查时,图像不会显示。 请帮忙。

private void ratedialog() {

        dialog = new Dialog(this);
        dialog.setContentView(R.layout.dialog);
        dialog.setTitle("Rate it!");
        dialog.setCancelable(true);
        dialog.show();

        seekbar = (SeekBar) dialog.findViewById(R.id.rate_seekbar);
        button_done = (Button) dialog.findViewById(R.id.button_done);
        seekbar_result = (TextView) dialog.findViewById(R.id.seekbar_result);
        checkBox_seen = (CheckBox) dialog.findViewById(R.id.checkBox_seen);

        button_done.setOnClickListener(EditActivity.this);
        seekbar.setOnSeekBarChangeListener(EditActivity.this);
        checkBox_seen.setOnCheckedChangeListener(EditActivity.this);


        seekbar.setProgress(rate);

    }

监听器:

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.e(TAG, isChecked+"");
        if (isChecked) {
            //seen = true;
            checkBox_seen.setChecked(true);
            Log.e(TAG, "Second time " + isChecked + "");

            image_seen.setImageResource(R.drawable.seen);

        } else {
           // seen = false;
            checkBox_seen.setChecked(false);
            Log.e(TAG, "Third time" + isChecked + "");

            image_seen.setVisibility(View.INVISIBLE);
        }
    }

2 个答案:

答案 0 :(得分:0)

将您的代码更改为

if (isChecked) {
        //seen = true;
        checkBox_seen.setChecked(true);
        Log.e(TAG, "Second time " + isChecked + "");
        image_seen.setVisibility(View.VISIBLE);
        image_seen.setImageResource(R.drawable.seen);

    } else {
       // seen = false;
        checkBox_seen.setChecked(false);
        Log.e(TAG, "Third time" + isChecked + "");

        image_seen.setVisibility(View.INVISIBLE);
    }

在Else条件下,您将visiblity更改为INVISIBLE,以便在if条件运行时它变为不可见,您不会将Visiblity更改为可见,这样就不会显示图像的原因。

答案 1 :(得分:0)

问题是您没有更改已检查状态的可见性设置再次检查CheckBox实例。

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.e(TAG, isChecked+"");
        if (isChecked) {
            //seen = true;
            **checkBox_seen.setChecked(true);// remove this line**
            Log.e(TAG, "Second time " + isChecked + "");
            **image_seen.setVisibility(View.VISIBLE);//Add this line**
            image_seen.setImageResource(R.drawable.seen);//Move this line to `ratedialog()`method.

        } else {
           // seen = false;
            **checkBox_seen.setChecked(false);//Remove this line**
            Log.e(TAG, "Third time" + isChecked + "");

            image_seen.setVisibility(View.INVISIBLE);
        }
    }

简单的代码看起来像

if (isChecked) {
        image_seen.setVisibility(View.VISIBLE);
    } else {
        image_seen.setVisibility(View.INVISIBLE);
 }