如何设置checkmark不可见

时间:2013-03-04 13:34:10

标签: java android

我想在类代码而非xml文件中的复选框按钮中隐藏复选标记。 因为如果选中了真实的背景,我正在使用两个背景..如果错误设置背景“checkbox.png”

公共类ListActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);


    AbsoluteLayout ff = (AbsoluteLayout) this.findViewById(R.id.AbsoluteLayout1);

    ScrollView myScrollView =  (ScrollView) findViewById(R.id.scrollView1);
    TableLayout tl =(TableLayout) findViewById(R.id.h103);
    TableRow tr = (TableRow) findViewById(R.id.TableRow18);
    CheckBox cb =  (CheckBox) findViewById(R.id.CheckBox18);
    TextView tv = (TextView) findViewById(R.id.TextView34);





//  ScrollView myScrollView1 = new  ScrollView(this);
    TableLayout tl1 =new TableLayout(this);
    TableRow tr1 = new  TableRow(this);
    final CheckBox buttonView = new CheckBox(this) ;
    TextView tv1 = new  TextView(this);

    /*myScrollView.getLayoutParams();
    ViewGroup.LayoutParams iv_params_b = myScrollView1.getLayoutParams();
    myScrollView1.setLayoutParams(iv_params_b);*/


    //buttonView.setVisibility(1);
    buttonView.setBackground(getResources().getDrawable(R.drawable.checkbox));
    //buttonView.setVisibility(View.GONE);
    buttonView.setFocusable(false);

    buttonView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            if(((CheckBox) v).isChecked())
            {           

                //buttonView.setChecked(false);
                buttonView.setBackground(getResources().getDrawable(R.drawable.star));

            }else
            {
                //buttonView.setChecked(true);
                buttonView.setBackground(getResources().getDrawable(R.drawable.checkbox));

            } 

        }
    });


     /* if(isChecked)
        {               
            buttonView.setBackground(getResources().getDrawable(R.drawable.star));
        }else
        {
            buttonView.setBackground(getResources().getDrawable(R.drawable.checkbox));
        }   */











    ViewGroup.LayoutParams iv_params_b = tl.getLayoutParams();
    tl1.setLayoutParams(iv_params_b);


    ViewGroup.LayoutParams iv_params_b1 = tr.getLayoutParams();
    tr1.setLayoutParams(iv_params_b1);


    ViewGroup.LayoutParams iv_params_b2 = cb.getLayoutParams();
    buttonView.setLayoutParams(iv_params_b2);


    ViewGroup.LayoutParams iv_params_b3 = tv.getLayoutParams();
    tv1.setLayoutParams(iv_params_b3);

    tl.addView(tr1);
    tr1.addView(buttonView);

  }
   }

2 个答案:

答案 0 :(得分:0)

您可能应该使用框架的常量,例如View.Gone

答案 1 :(得分:0)

将其另存为可绘制文件夹中的xml。

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
     android:state_checked="true" android:state_focused="true"
  android:drawable="@drawable/checked"/>
  <item
    android:state_checked="false" android:state_focused="false" 
     android:drawable="@drawable/notchecked" 
     />
</selector>

并将其设置为您的复选框andriod:button="@drawable/yourxmlfile.

您无需以编程方式更改图像。

希望这会对你有所帮助。

checkbox.setVisibility(View.INVISIBLE)这是您提出的答案。

修改

final CheckBox checkBox1=new CheckBox(MainActivity.this);

checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton paramCompoundButton,
            boolean paramBoolean) {
        // TODO Auto-generated method stub

        if (paramBoolean) {

            checkBox1.setButtonDrawable(R.drawable.ic_launcher);
            //here you can change 
        }
        else
        {
            checkBox1.setBackgroundColor(Color.BLUE);
        }
    }
});

查看代码......