1

我想用Spannanle. 以下是我的活动文件:

package com.Myapp;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.widget.EditText;
import android.widget.TextView;

public class MyappActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView vw = (TextView)findViewById(R.id.request);
        Spannable str = (Spannable)vw.getText();
        str.setSpan(new ForegroundColorSpan(Color.BLUE), 10, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


    }
}

我的 main.xml 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="73dp"
        android:layout_height="40dp"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="My APP"
        android:textColor="@color/black"
        android:textSize="19sp"
        android:typeface="serif" />

    <TextView
        android:id="@+id/request"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        android:text="I want to collect from" />
</Linearlayout>

我得到的错误是:

05-23 22:40:54.279: E/AndroidRuntime(451): FATAL EXCEPTION: main
05-23 22:40:54.279: E/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Myapp/com.Myapp.MyappActivity}: java.lang.ClassCastException: java.lang.String
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.os.Looper.loop(Looper.java:130)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-23 22:40:54.279: E/AndroidRuntime(451):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 22:40:54.279: E/AndroidRuntime(451):  at java.lang.reflect.Method.invoke(Method.java:507)
05-23 22:40:54.279: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-23 22:40:54.279: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-23 22:40:54.279: E/AndroidRuntime(451):  at dalvik.system.NativeStart.main(Native Method)
05-23 22:40:54.279: E/AndroidRuntime(451): Caused by: java.lang.ClassCastException: java.lang.String
05-23 22:40:54.279: E/AndroidRuntime(451):  at com.Myapp.MyappActivity.onCreate(MyappActivity.java:21)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

我错过了什么吗?

4

1 回答 1

2

使用以下代码:

Spannable str = new SpannableString(vw.getText().toString());

或者,

SpannableString str = new SpannableString(vw.getText().toString());

或者,您可以使用工厂:

Spannable str = Spannable.Factory.getInstance().newSpannable(vw.getText());

您还需要在代码末尾添加以下行以获得效果:

 vw.setText(str);

顺便说一句,vw.getText()vw.getText().toString()都可以正常工作:)

于 2012-05-24T01:54:17.077 回答