2013-10-14 31 views
4

我在我的xml文件中创建了一个复选框。我试图在checkBox的文本上设置onClickListener,但是我找不到真正的解决方案。换句话说,我希望能够点击复选框(使复选框可选),并点击文本以协议期限开启新的活动。谢谢。在复选框文本上设置onClickListener

main.xml中

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="I agree to the Terms of Agreement." 
    android:textColor="#CC000000" 
    android:checked="true"/> 

这是下面的代码我尝试,但它只会导致崩溃。

CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1); 
Button termsOfAgreement = (Button) checkBox2.getText(); 

.... 

termsOfAgreement.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      //Launch activity 
     }   
    }); 

回答

4

getText()不是一个按钮,它是一个字符串,所以你不能把它转换为一个按钮。

你最好不要设置复选框的文本,只使用常规textview旁边的复选框,并将一个监听器放在文本上。

然后您必须在单击文本时管理复选框。所以你有2个点击监听器,一个用于复选框和一个TextView的

+0

是的,我已经考虑到了,并打算作为我最后的解决方案。如果可能的话,最好从checkBox中执行它。想知道是否有任何可能的方法。 – ChallengeAccepted

+0

不,不存在 – tyczj

+0

谢谢。我继续做,并做你的建议。可能不是答案,但它是最好的选择。 – ChallengeAccepted

0

的onclick是不正确的监听器,它的:

onCheckedChanged 

编辑: 并设置监听其:

setOnCheckedChangeListener 
+0

我完全知道。我想要两个功能。一个是复选框本身,另一个能够在点击文本时打开活动。 – ChallengeAccepted

+0

我不认为这可能..也许你应该通过扩展android.widget.CompoundButton创建你自己的复选框 – Tom

+0

谢谢你的帮助。我解决了它。看到我发布的答案。 – ChallengeAccepted

1

可能是一个骗子,但如何将复选框(没有文本)旁边的文本视图(与您的文本),这样你可以使用文本视图的ontouch/onclick事件来设置复选框并打开该活动,并按复选框将只有通道eck /取消选中它。

+0

谢谢。我继续做你和tyczj的建议。这些并没有真正回答这个问题,但它最终完成了这项工作。 – ChallengeAccepted

1

Result with TextView beside the CheckBox

看起来没有真正解决这一点。唯一的可能性是把它放在复选框旁边。这样做的目的是为了避免textView和checkBox之间的任何错位对齐,但这是最好的解决方案。这是代码。

<CheckBox 
    android:id="@+id/checkBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/checkBox" 
    android:layout_margin="5dp" 
    android:textColor="#CC000000" 
    android:checked="true"/> 

    <TextView 
    android:id="@+id/termsOfAgreement" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="I agree to the Terms of Agreement." 
    android:textColor="#CC000000" 
    android:layout_toRightOf="@id/checkBox" 
    android:layout_alignTop="@id/checkBox" 
    android:textSize="16sp"/> 

然后在Java代码设置为onClickListener TextView的和为CheckBox本身一个单独的监听器。

+1

只需将文本对齐复选框底部 – tyczj

+0

不错的建议,但这使得它看起来更糟糕。它使它在底部,超过了盒子。我想alignTop现在应该做。 – ChallengeAccepted

+1

添加填充到底部,您可以很容易地使它看起来像定位和填充复选框。 'android:paddingBottom =“5dp”' – tyczj

3

由于没有以前的答案真的给你你想要的东西,所以你可能只需将你的CheckBox和TextView包装在一个更大的视图组(如LinearLayout)中。

你在XML实现将如下所示:

<LinearLayout 
    android:id="@+id/check_box_and_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:orientation="horizontal"> 

    <CheckBox 
     android:id="@+id/checkBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:textColor="#CC000000" 
     android:checked="true"/> 

    <TextView 
     android:id="@+id/termsOfAgreement" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="I agree to the Terms of Agreement." 
     android:textColor="#CC000000" 
     android:textSize="16sp"/> 

</LinearLayout> 

有了这个,你现在可以只设置OnClickListener到的LinearLayout而不是有一个为你的CheckBox和你的TextView两者。请注意,我在LinearLayout下将clickable设置为true。这是一件值得注意的事情,因为默认情况下,LinearLayout上的clickable属性为false。在Java代码中

实施情况如下:

LinearLayout myCheckbox = (LinearLayout)findViewById(R.id.check_box_and_text); 
myCheckbox.setOnClickListener(yourListener); 

作为一个快速的最后一点,如果您发现校准是靠不住与CheckBox和TextView的,直视使用XML属性layout_gravity和重力让他们像你想要的那样。

希望这会有所帮助!祝你好运

+0

伟大的使用layout_gravity和重力!我似乎忘记了这个属性。谢谢! – ChallengeAccepted