Android下划线文字&保存到文本文件

时间:2014-12-27 08:30:41

标签: java android text underline

我试图在Java中强调文本。 attendeeList项目来自我的数据库。这是我的代码:

for (int i = 0; i < attendeeList.size(); i++) {
                attendeeCounterStr = "\nAttendee " + counter;
                attendeeStrBuilder.append(attendeeCounterStr);
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);

                AttributedString as = new AttributedString(
                        attendeeCounterStr);
                as.addAttribute(TextAttribute.FONT, plainFont);
                as.addAttribute(TextAttribute.UNDERLINE,
                        TextAttribute.UNDERLINE_ON, 0, 10);
                g.drawString(as.getIterator(), 24, 70);

                counter++;
            }

我的文字示例包括:Attendee 1Attendee 2等。但是,我收到错误消息:

12-27 16:26:34.921: D/BounceScrollRunnableDefault(26975): start(1439.7937), mBounceExtent:0.0
12-27 16:26:34.991: D/BounceScrollRunnableDefault(26975): mBounceExtentCoef = -118.639
12-27 16:26:35.585: W/dalvikvm(26975): VFY: unable to resolve static field 11624 (KEY_ANTIALIASING) in Ljava/awt/RenderingHints;
12-27 16:26:35.585: D/dalvikvm(26975): VFY: replacing opcode 0x62 at 0x010e
12-27 16:26:35.585: I/dalvikvm(26975): DexOpt: unable to optimize static field ref 0x2d69 at 0x110 in Lnyp/edu/eneighbourhood/EventDetailChart$4;.onClick
12-27 16:26:35.585: D/dalvikvm(26975): DexOpt: unable to opt direct call 0x717e at 0x121 in Lnyp/edu/eneighbourhood/EventDetailChart$4;.onClick
12-27 16:26:37.054: D/AndroidRuntime(26975): Shutting down VM
12-27 16:26:37.054: W/dalvikvm(26975): threadid=1: thread exiting with uncaught exception (group=0x40c561f8)
12-27 16:26:37.249: E/AndroidRuntime(26975): FATAL EXCEPTION: main
12-27 16:26:37.249: E/AndroidRuntime(26975): java.lang.NoClassDefFoundError: java.awt.RenderingHints
12-27 16:26:37.249: E/AndroidRuntime(26975):    at nyp.edu.eneighbourhood.EventDetailChart$4.onClick(EventDetailChart.java:227)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at android.view.View.performClick(View.java:3620)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at android.view.View$PerformClick.run(View.java:14292)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at android.os.Handler.handleCallback(Handler.java:605)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at android.os.Looper.loop(Looper.java:137)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at android.app.ActivityThread.main(ActivityThread.java:4512)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at java.lang.reflect.Method.invokeNative(Native Method)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at java.lang.reflect.Method.invoke(Method.java:511)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
12-27 16:26:37.249: E/AndroidRuntime(26975):    at dalvik.system.NativeStart.main(Native Method)
12-27 16:27:11.726: I/Process(26975): Sending signal. PID: 26975 SIG: 9

2 个答案:

答案 0 :(得分:1)

这是在Android上为文字加下划线的正确方法。你想要这样的东西:

TextView textView = (TextView) view.findViewById(R.id.viewid);
SpannableString content = new SpannableString("Something to be underlined");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

答案 1 :(得分:0)

setRenderingHint()是一个AWT(桌面图形)API,它并不存在于Android上。它也与文本下划线无关。只需删除此电话即可。