简体   繁体   English

从ExpandableListView编辑文本视图子级(删除线)

[英]Edit textView child from ExpandableListView (Strikethrough)

I have a ExpandableListView with a OnChildClickListener but how can I add strikethrough to the TextView when it is clicked? 我有一个带有OnChildClickListenerExpandableListView ,但是当单击TextView时如何将删除线添加到TextView

mExpandableList.setOnChildClickListener(new OnChildClickListener() { mExpandableList.setOnChildClickListener(new OnChildClickListener(){

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            Toast.makeText(getApplicationContext(), 
                    "Group: " + groupPosition + " Child: " + childPosition, Toast.LENGTH_SHORT).show();

            //Do something (i.e. add strikethourgh) to the TextView clicked..

            return false;
        }
    });

My layout file for the activity is 我的活动布局文件是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/calc_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ExpandableListView
        android:id="@+id/calc_expandable_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:listSelector="@android:color/transparent"
        android:transcriptMode="alwaysScroll" />

</LinearLayout>

while the children of the ExpandableList is 而ExpandableList的子级是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_child"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/list_item_text_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:padding="10dp"
        android:textSize="20sp" />
</LinearLayout>

The error from LogCat is: LogCat的错误是:

W/dalvikvm(28424): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) E/AndroidRuntime(28424): FATAL EXCEPTION: main
E/AndroidRuntime(28424): java.lang.ClassCastException: android.widget.LinearLayout E/AndroidRuntime(28424):     at com.test.skylderhverandre.Calc$1.onChildClick(Calc.java:185)
E/AndroidRuntime(28424):    at
android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:588)
E/AndroidRuntime(28424):    at
android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
E/AndroidRuntime(28424):    at
android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
E/AndroidRuntime(28424):    at
android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(28424):    at
android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(28424):    at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(28424):    at
android.app.ActivityThread.main(ActivityThread.java:4385)
E/AndroidRuntime(28424):    at
java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28424):    at
java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(28424):    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
E/AndroidRuntime(28424):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime(28424):    at dalvik.system.NativeStart.main(Native Method)

You must use SpannableString like this : 您必须像这样使用SpannableString

CharSequence value = ((TextView)((LinearLayout)v).getChildAt(0)).getText();
if(value!=null){
    SpannableString spannableString = new SpannableString(value);
    spannableString.setSpan(new StrikethroughSpan(), 0, value.length(), 0);
    ((TextView)((LinearLayout)v).getChildAt(0)).setText(spannableString);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM