Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

checkbox - android: CheckedTextView cannot be checked?

Initially I wanted a checkmark where the text is placed on the left of the checkmark. After searching on this site I found out the best workaround is android:CheckedTextView? However, I found out that the checkmark cannot be changed manually by users. Is it by design?

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/autoupdatecheckboxview" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical" 
 android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
 android:paddingLeft="6dip" 
 android:paddingRight="6dip" 
 android:text="Pop up a message when new data available" 
 android:typeface="sans" android:textSize="16dip"/> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is possible, and somewhat simple to implement what you are looking for. Yes, CheckedTextView is used primarily for having a single Checkable view in the row of a ListView, which controls its children's checkable states using choiceMode. However, since CheckBox does not appear to support a right-aligned checkbox on its own, and CheckedTextView is a right-aligned checkbox, it makes sense to want to use what's there.

Because ListView controls the checked state of a list item, the CheckedTextView itself does not respond to click events, and is not clickable or focusable by default. It does respond to pressed and focused states, however -- that means it can receive focus and click events, and looks correct as far as a checkbox should look. The only thing missing is that it does not toggle its checked state on click. Therefore, a quick OnClickListener that calls .toggle() will give you the end result you're looking for.

In summary, you need 3 things: clickable, focusable, and onClickListener:

    CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
    chkBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            ((CheckedTextView) v).toggle();
        }
    });

and layout file:

<CheckedTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/CheckedTextView01"
    android:checked="true"
    android:clickable="true"
    android:focusable="true"
    android:text="Label on Left Side of Checkbox."
    />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...