Android: 在 Textview 中启用复制

Android: enable copy in Textview

linearlayout 有很多文本视图,我需要启用复制到剪贴板。 实际上我需要从布局中复制但我不知道如何?

我尝试使用 android:textIsSelectable。在 textview 但我收到错误消息:

01-06 16:58:18.976: D/AndroidRuntime(2991): Shutting down VM
01-06 16:58:18.976: W/dalvikvm(2991): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-06 16:58:18.986: E/AndroidRuntime(2991): FATAL EXCEPTION: main
01-06 16:58:18.986: E/AndroidRuntime(2991): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quraan.tajweed/com.quraan.tajweed.esti3azah}: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.ActivityThread.access0(ActivityThread.java:127)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.os.Looper.loop(Looper.java:137)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at java.lang.reflect.Method.invoke(Method.java:511)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at dalvik.system.NativeStart.main(Native Method)
01-06 16:58:18.986: E/AndroidRuntime(2991): Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.Activity.setContentView(Activity.java:1835)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at com.quraan.tajweed.esti3azah.onCreate(esti3azah.java:37)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.Activity.performCreate(Activity.java:4465)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-06 16:58:18.986: E/AndroidRuntime(2991):     ... 11 more
01-06 16:58:18.986: E/AndroidRuntime(2991): Caused by: java.lang.reflect.InvocationTargetException
01-06 16:58:18.986: E/AndroidRuntime(2991):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-06 16:58:18.986: E/AndroidRuntime(2991):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
01-06 16:58:18.986: E/AndroidRuntime(2991):     ... 24 more
01-06 16:58:18.986: E/AndroidRuntime(2991): Caused by: java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
01-06 16:58:18.986: E/AndroidRuntime(2991):     at com.quraan.tajweed.SimpleTextView.<init>(SimpleTextView.java:54)
01-06 16:58:18.986: E/AndroidRuntime(2991):     ... 27 more
01-06 16:58:19.076: D/dalvikvm(2991): GC_CONCURRENT freed 233K, 4% free 9270K/9607K, paused 2ms+2ms

在文件 SimpleTextView.java 的第 54 行,您正在将 SpannableString 转换为简单的字符串。

如果 SpannableString 中有一个字符串,那么您可以使用 .toString() 方法而不是强制转换。