這篇文章將為大家詳細講解有關怎么在Android應用中利用textview實現一個長按復制功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
這里有一個大坑,我的viewGroup中有一個這個屬性android:descendantFocusability=”blocksDescendants”
開始沒有注意到,試了好多方法都不行,后來才看到這個屬性,順便科普一下,這個屬性有三個值~
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
這里使用afterDescendants就好了,
標題有一點誤區,其實這里的textView是editText,只是樣子像textView而已,直接上代碼
<EditText android:id="@+id/order_details_tv_order_number" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="@null" android:editable="false" android:textIsSelectable="true" android:gravity="right|center_vertical" android:lines="1" android:textColor="@color/pub_color_black" android:textCursorDrawable="@null" android:textSize="@dimen/text_h26" tools:text="2382787482347" />
其中需要注意的是這幾個屬性
android:background=”@null”//背景設為null,去掉editText下劃線
android:textCursorDrawable=”@null”//游標設為null,去掉游標
android:editable=”false”//不可編輯
android:textIsSelectable=”true”//文字內容可選
測試發現部分rom會彈不出來系統自帶復制按鈕,加入下面代碼可以保證復制成功
tvView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { ClipboardManager cm =(ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE); cm.setText(orderDetailsTvOrderNumber.getText().toString()); //ToastUtil.toastSth(getContext() , "訂單號已復制到剪切板,快去粘貼吧~"); return false; } });
關于怎么在Android應用中利用textview實現一個長按復制功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。