如何在按下CheckBox时突出显示TextView?

时间:2013-01-22 07:00:25

标签: android

我有一个包含CheckBox和TextView的LinearLayout。我想在按下CheckBox时突出显示TextView。

布局和选择器(在可绘制文件夹中)

<LinearLayout
        android:orientation="vertical"
        android:gravity="center" >
        <CheckBox
            android:id="@+id/chkPlayStop"
            android:button="@drawable/play_stop_state" />
        <TextView
            android:id="@+id/tvPlayStop"
            android:clickable="true"
            android:textColor="@drawable/text_view_state" />

</LinearLayout>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="@color/white" android:state_pressed="true"/>
    <item android:color="@color/grey" />
</selector>

目标:当我在布局中点击时 - 复选框更改状态和文本视图突出显示。

感谢。

3 个答案:

答案 0 :(得分:0)

您必须在OnCheckedChangeListener上设置checkbox,如下所示:

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if (isChecked)
        {
            //Highlight the text
            textView.setTypeface(null, Typeface.BOLD_ITALIC);
        } else
        {
            //Unhighlight the text
            textView.setTypeface(null, Typeface.NORMAL);
        }
    }
});

答案 1 :(得分:0)

请尝试以下代码:

TextView textView = (TextView) findViewById(R.id.yourTextView);
cb = (CheckBox) findViewById(R.id.yourCheckBox);

cb.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
    if (((CheckBox) v).isChecked()) {
        textView.setBackgroundColor(Color.RED);
    }else{
        textView.setBackgroundColor(Color.BLACK);
    }

  }
});

我不知道你想如何突出显示textview,所以这个例子只包含一个新的背景颜色。

答案 2 :(得分:0)

您可以通过更改文字颜色,放置背景或更改文字类型(如粗体等)来突出显示文字视图。

要更改背景,您可以使用textView.setBackgroundColor(your color code)

要更改文字类型,您可以使用textView.setTypeface(null,Typeface.BOLD)

要更改文字颜色,您可以使用textView.setTextColor(your color code

在checkChangedListener中的上述答案中显示的位置使用此代码。

相关问题