有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何设置复选标记不可见

我想在类代码而不是xml文件中隐藏复选框按钮中的复选标记。 因为我使用了两个背景,如果检查为真,则设置背景。。如果为false,则设置背景“checkbox.png”

公共类ListActivity扩展了活动{

@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) 个答案

  1. # 1 楼答案

    在drawable文件夹中将其另存为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.

    您不需要以编程方式更改图像

    希望这对你有帮助

    复选框。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);
            }
        }
    });
    

    查看代码

  2. # 2 楼答案

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