如何在Android中的自定义TextView控件中更改字母的颜色?

时间:2014-01-03 22:54:57

标签: android textview android-custom-view

我正在关注android首先开发的电子书;在某一点上,目标是周期性地改变每个字母的颜色。

这个例子是用碎片写的,我不清楚如何将它们粘合好。

注意:我在未放置的书中插入了selfchange的定义。

那么错误在哪里?这样我的应用程序崩溃了。 我应该将@Override放在onTextChanged方法上吗?

我是这样写的:

public class CustomTextView extends TextView
{
    public CustomTextView(Context context,AttributeSet attributeSet,int defSytle)
    {
        super(context, attributeSet, defSytle);
    }

    public CustomTextView(Context context,AttributeSet attributeSet)
    {
        super(context, attributeSet);
    }

    public CustomTextView(Context context) 
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    protected void onTextChanged(CharSequence text,
    int start, int lengthBefore, int lengthAfter)
    {
        if(selfChange)
        {
            selfChange = false;
            return;
        }
        selfChange=true;

        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        builder.clearSpans();
        ForegroundColorSpan colorSpan;
        int color;
        for(int i=0; i < text.length(); i++)
        {
            //pick the next color
            color = colorArray[i%colorArray.length];
            //Create the color span
            colorSpan = new ForegroundColorSpan(color);
            //Add the color span for this one char
            builder.setSpan(colorSpan,
            i, i,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        setText(builder);
    }

    boolean selfChange = false;

    int colorArray[] = new int[]
    {
        Color.WHITE,
        Color.RED,
        Color.YELLOW,
        Color.GREEN,
        Color.BLUE,
        Color.MAGENTA,
        Color.CYAN,
        Color.DKGRAY
    };
}

logcat的:

01-04 00:17:34.560: D/AndroidRuntime(3149): Shutting down VM
01-04 00:17:34.560: W/dalvikvm(3149): threadid=1: thread exiting with uncaught exception (group=0x40fbe2a0)
01-04 00:17:34.575: E/AndroidRuntime(3149): FATAL EXCEPTION: main
01-04 00:17:34.575: E/AndroidRuntime(3149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lux.myproject/com.lux.myproject.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class com.lux.myproject.CustomTextView
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.os.Looper.loop(Looper.java:137)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.main(ActivityThread.java:4898)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Method.invoke(Method.java:511)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at dalvik.system.NativeStart.main(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class com.lux.myproject.CustomTextView
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.Activity.setContentView(Activity.java:1924)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.lux.myproject.MainActivity.onCreate(MainActivity.java:53)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.Activity.performCreate(Activity.java:5206)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-04 00:17:34.575: E/AndroidRuntime(3149):     ... 11 more
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: java.lang.reflect.InvocationTargetException
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-04 00:17:34.575: E/AndroidRuntime(3149):     ... 22 more
01-04 00:17:34.575: E/AndroidRuntime(3149): Caused by: java.lang.NullPointerException
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.lux.myproject.CustomTextView.onTextChanged(CustomTextView.java:46)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.setText(TextView.java:3723)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.setText(TextView.java:3577)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.<init>(TextView.java:1224)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at android.widget.TextView.<init>(TextView.java:566)
01-04 00:17:34.575: E/AndroidRuntime(3149):     at com.lux.myproject.CustomTextView.<init>(CustomTextView.java:20)
01-04 00:17:34.575: E/AndroidRuntime(3149):     ... 25 more

1 个答案:

答案 0 :(得分:1)

protected void onTextChanged(CharSequence text,
    int start, int lengthBefore, int lengthAfter)
{
    if (text.lenght > 0)
    //YOUR CODE
}

尝试检查文本的长度

screen.setText is breaking on use. uncaught expection (group=0xb3a539@0) (Android)