2013-04-15 45 views
4

我使用的是CheckBox和TextView的在我的應用程序時,如何啓用複選框(我; M使用複選框和TextView中之間的圖像視圖,所以我不能使用複選框文本)。如果單擊相應的TextView,我想啓用/禁用我的複選框。任何人都請幫助我。相關的TextView被點擊

+0

你的意思是,你要被點擊時整(假定)的LinearLayout您的複選框被_checked/unchecked_? - 無論如何,請提供一些代碼。 –

+3

您可以設置checkbox.When用戶點擊的可視性TextView的設置複選框的知名度,真實的。我想你想這一點。 – AndiM

回答

0

我認爲它容易。實現您的TextView點擊監聽器和檢查,並取消您的複選框,在onClickListener ..

3

就試試這個:

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:orientation="vertical" 
      android:layout_height="match_parent" 
      > 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="First text box" 
    android:id="@+id/tb_1" 
    /> 
<CheckBox 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_margin="10dp" 
     android:id="@+id/cb_1" 
     /> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Second text box" 
     android:id="@+id/tb_2" 
     android:layout_marginTop="50dp" 
     /> 
<CheckBox 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_margin="10dp" 
     android:id="@+id/cb_2" 
     /> 
    </LinearLayout> 

MyActivity.java

public class MyActivity extends Activity implements View.OnClickListener { 

private TextView tv1; 
private CheckBox cb1; 
private TextView tv2; 
private CheckBox cb2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
    tv1 = (TextView)findViewById(R.id.tb_1); 
    tv2 = (TextView)findViewById(R.id.tb_2); 

    cb1 = (CheckBox)findViewById(R.id.cb_1); 
    cb2 = (CheckBox)findViewById(R.id.cb_2); 

    tv1.setOnClickListener(this); 
    tv2.setOnClickListener(this); 
    } 


@Override 
public void onClick(View v) { 
    switch (v.getId()) 
    { 
     case (R.id.tb_1): 
     { 
      cb1.setChecked(!cb1.isChecked()); 
      break; 
     } 
     case (R.id.tb_2): 
     { 
      cb2.setChecked(!cb2.isChecked()); 
      break; 
     } 
    } 
} 
} 

當你點擊的TextView一個你剛纔設置的電流值setChecked嵌套CheckBox(一個或多個) 。這就是你所需要的。 希望它的幫助。

enter image description here