【问题标题】:Setting an onClickListener on a CheckBox Text在 CheckBox 文本上设置 onClickListener
【发布时间】:2013-10-22 11:35:48
【问题描述】:

我在我的 xml 文件中创建了一个复选框。我正在尝试在复选框的文本上设置一个 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
        }           
    });

【问题讨论】:

标签: android checkbox onclicklistener


【解决方案1】:

getText() 不是按钮,它是字符串,因此您不能将其转换为按钮。

最好不要为复选框设置文本,而只在复选框旁边使用常规文本视图并在文本上放置一个侦听器。

那么您必须在单击文本时管理复选框。所以你会有 2 个点击监听器,一个用于复选框,一个用于文本视图

【讨论】:

  • 是的,我已经考虑过这一点,并打算将其作为我的最后一个解决方案。如果可能,最好从复选框中执行此操作。想知道是否有任何可能的方法。
  • 谢谢。我继续按照你的建议做了。可能不是答案,但它是最好的选择。
【解决方案2】:

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

onCheckedChanged

编辑: 并将侦听器设置为:

setOnCheckedChangeListener

【讨论】:

  • 我很清楚。我想要它有两个功能。一个是复选框本身,第二个是能够在单击文本时打开一个活动。
  • 我不认为这是可能的......也许你应该通过扩展 android.widget.CompoundButton 来创建你自己的复选框
  • 感谢您的帮助。我解决了。请参阅我发布的答案。
【解决方案3】:

可能有点作弊,但是如何将文本视图(带有文本)放在复选框(没有文本)旁边,这样您就可以使用文本视图的 ontouch/onclick 事件来设置复选框并打开活动, 并且按下复选框只会选中/取消选中它。

【讨论】:

  • 谢谢。我继续按照你和 tyczj 的建议做了。这些并没有真正回答问题,但它最终作为替代方案完成了工作。
【解决方案4】:

似乎没有真正的解决方案。唯一的可能是将其放在复选框中。这样做的目的是避免 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 代码中为 TextView 设置一个 onClickListener 并为 CheckBox 本身设置一个单独的侦听器。

【讨论】:

  • 只需将文本与复选框底部对齐
  • 不错的建议,但这让它看起来更糟。它位于底部,超过了盒子。我想 alignTop 现在应该这样做。
  • 在底部添加内边距,你可以很容易的让它看起来像带有定位和内边距的复选框。 android:paddingBottom="5dp"
【解决方案5】:

鉴于之前的答案都没有真正满足您的需求,您可能只需将 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 和 Gravity 来获得您想要的方式。

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

【讨论】:

  • 很好地呼吁使用 layout_gravity 和重力!我似乎忘记了那个属性。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
相关资源
最近更新 更多