-1

我想在类代码而不是 xml 文件中的复选框按钮中隐藏复选标记。因为如果选中 true 设置背景,我使用两个背景 .. 如果 false 设置背景“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);

  }
   }
4

2 回答 2

0

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

于 2013-03-04T13:38:00.990 回答
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);
        }
    }
});

查看代码...

于 2013-03-04T13:42:28.080 回答