黑莓:RichTextField 的问题

发布于 2024-10-22 05:23:51 字数 1126 浏览 4 评论 0原文

我想在屏幕上添加一些文本。由于此文本会根据单词改变颜色,因此我决定使用 RichTextField。 这是我的代码:

String productName = product.getProductName()+": ";
String price = String.valueOf(product.getPrice());
Font[] fonts = new Font[2];
fonts[0] = Font.getDefault().derive(Font.PLAIN, 12);
fonts[1] = fonts[0].derive(Font.BOLD);
byte[] attribs = new byte[] {0, 1};
int[] offsets = new int[] {0, productName.length(), productName.length() + price.length()};
int[] fontColors = new int[] {Color.BLACK, Color.GREEN};
int[] backColors = new int[] {Color.WHITE, Color.WHITE};
RichTextField labelPrice = new RichTextField(productName + price, offsets, attribs, fonts, Field.FIELD_LEFT|FIELD_VCENTER);
labelPrice.setAttributes(fontColors, backColors);

我可以添加文本,除了背景之外没有问题。我的product的第一个字母有蓝色背景,我没有在其他地方添加任何蓝色,即使我是,我也希望它会被backColors覆盖。有谁知道为什么会发生这种情况?

提前致谢!


编辑:我只是在这里修复 raukodraug 为像我这样的新手指出的代码

RichTextField labelPrice = new RichTextField(productName + price, offsets, attribs, fonts, Field.FIELD_LEFT|FIELD_VCENTER|Field.NON_FOCUSABLE);

I want to add some text to a screen. Since this text changes color according to the words I decided to use a RichTextField.
Here is my code:

String productName = product.getProductName()+": ";
String price = String.valueOf(product.getPrice());
Font[] fonts = new Font[2];
fonts[0] = Font.getDefault().derive(Font.PLAIN, 12);
fonts[1] = fonts[0].derive(Font.BOLD);
byte[] attribs = new byte[] {0, 1};
int[] offsets = new int[] {0, productName.length(), productName.length() + price.length()};
int[] fontColors = new int[] {Color.BLACK, Color.GREEN};
int[] backColors = new int[] {Color.WHITE, Color.WHITE};
RichTextField labelPrice = new RichTextField(productName + price, offsets, attribs, fonts, Field.FIELD_LEFT|FIELD_VCENTER);
labelPrice.setAttributes(fontColors, backColors);

I can add the text and there is no problem but the background. The first letter of my product has a blue background, im not adding any blue anywhere else, and even if i was, i would expect that it would be overriden with backColors. Does anyone know why this is happening?

Thanks in advance!


EDIT: I'm just fixing the code here that raukodraug pointed out for rookies like me

RichTextField labelPrice = new RichTextField(productName + price, offsets, attribs, fonts, Field.FIELD_LEFT|FIELD_VCENTER|Field.NON_FOCUSABLE);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2024-10-29 05:23:51

好吧,我认为你的问题不在于它有蓝色背景,而在于它的焦点。尝试移动拨轮,您就会看到。
您应该将 Field.NON_FOCUSABLE 添加到 RichTextField 样式中。我很确定这会解决你的问题。
正如您在此处看到的 你应该这样做,这样它就无法接受焦点。
我希望这有帮助

Well, I think your problem is not that it has a blue background but that it is focused. Try to move the trackwheel and you will see.
You should add Field.NON_FOCUSABLE to the RichTextField styles. I'm pretty sure that will fix your problem.
As you can see here you should do this so it cannot accept focus.
I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文