gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:11:21 24 4
gpt4 key购买 nike

我正在关注一本关于 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

最佳答案

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)

关于android - 如何在 android 的自定义 TextView 控件中更改字母的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20914207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com