Android TextView長按復制功能失效解決辦法

應用中使用RecyclerView列表來展現動態簡介,這時候需要添加長按復制的功能來提升用戶的體驗。使用Android自帶的功能就能滿足該需求,無需自己實現,方式很簡單,一句話解決:
<pre>
TextView.setTextIsSelectable(true);
</pre>但是其他地方使用該代碼能正常使用,然而在這里卻出現無法復制的問題。仔細體驗幾次發現:長按時出現了震動的情況,可并沒有出現選擇條,說明該代碼確實添加到了控件上,沒有出現復制條一定是哪個地方攔截或者阻止了。打印日志發現系統報了異常:
<pre>
TextView does not support text selection. Action mode cancelled.
</pre>就拿著該異常去網上找解決辦法,發現不只是我本人遇到了該問題,好多人提問。但是網上的回答卻是千奇百怪、各種出招、各種不靠譜。但是有一個人的答案(hungkk)被遺漏在無人關注的角落。本著試一試的心態進行測試,發現真的可行,就拿過來分享。
其實方式很簡單,應該是Android的一個bug,就是在TextView外部嵌套一個布局,讓TextView不要使用android:layout_width=”match_parent”,而是改成使用android:layout_width="wrap_content"
即可。更改后發現完美解決。代碼如下:
修復之前相關代碼:
<pre>
<TextView
android:id="@+id/tv_helinfo_comment_introduction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_helinfo_comment_avatar"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/sayhi_content_bg"
android:textColor="#999999"/>
</pre>修復后相關代碼:
<pre>
<LinearLayout
android:id="@+id/ll_helinfo_comment_introduction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_helinfo_comment_avatar"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/sayhi_content_bg">
<TextView
android:id="@+id/tv_helinfo_comment_introduction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#131313"/>
</LinearLayout>
</pre>添加復制功能的代碼:
<pre>
holder.tvIntroduction = (TextView) view.findViewById(R.id.tv_helinfo_comment_introduction);
holder.tvIntroduction.setTextIsSelectable(true);
</pre>總結一下本次經歷:
1,Android是一個逐漸完善和趨向完美的生態系統。但不能說現在的Android已經完美了,它會有缺陷,漏洞和不足,這需要我們包容和修復。
2,善于使用網絡。對于開發工作來說,常說的一句話就是避免開發相同的輪子。開源社區和各種論壇是一個很強大、很有效的工具,每一個合格的開發人員和軟件工程師都應該學會使用這個工具來完善自己,繼而提升自己。
3,善于分享。作為開發工作一線人員,我們每個人都有責任維護平臺的良性循環和逐漸完善。每位開發者都應該盡量將自己在平臺的收獲分享出來。開放平臺對每一種需求都可能有很多種實現方式,但是不可否認,最好的往往只有一種!只有將最好的實現方式廣為人知,我們在生活中使用的應用才可能都是優秀應用,我們的平臺才可能是一個優秀的平臺。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容