2013-03-04 86 views
-1

我想在複選框按鈕中的類代碼而不是xml文件中隱藏複選標記。 因爲我是用兩個背景檢查是否屬實組後臺..如果假組後臺「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); 

    } 
    } 
+0

http://learningnerd.wordpress.com/2006/09/06/english-grammar-types-of-phrases/和http://www.lel.ed.ac.uk/grammar/overview.html。無法理解你想要什麼以及你有什麼。對不起:-( – g00dy 2013-03-04 13:37:46

+0

當我點擊複選框按鈕有藍色複選標記。我想隱藏這個複選標記只因爲我使用不同的背景 – user1714553 2013-03-04 13:39:44

+0

請參閱此鏈接(http://developer.android.com/reference/ android/view/View.html#INVISIBLE),如果你在執行時遇到問題,請回到這裏並解釋。 – g00dy 2013-03-04 13:41:17

回答

0

你也許應該使用框架的常量,如View.Gone

+0

由於他試圖設置視圖的背景,它仍然需要在那裏。 – Geobits 2013-03-04 13:41:32

+0

我已經把:buttonView.setVisibility(2);但不是有效的,,我想隱藏內部檢查標記 – user1714553 2013-03-04 13:46:22

+0

2問題:你問題的描述不是很清楚,你應該使用命名常量(View.GO NE,View.INVISIBLE)爲清楚起見。 – DigCamara 2013-03-04 13:52:44

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

退房代碼...

+0

不,我想以編程方式使用它,因爲我將用於循環。 – user1714553 2013-03-04 13:48:42

+0

因爲這只是得到狀態單獨... if(checkbox.ischecked()){//做你的東西..} – itsrajesh4uguys 2013-03-04 13:49:42

+0

View.INVISIBLE它會隱藏我所有的複選框按鈕:(我只想隱藏裏面的複選標記我檢查按鈕 – user1714553 2013-03-04 13:50:25