實(shí)現(xiàn)效果
長按AlertDialog的文本支持選擇
彈窗文本長按選擇
代碼
AlertDialog的默認(rèn)文本無法直接長按選擇劝评,但是為了這么一個(gè)小功能使用自定義布局有點(diǎn)得不償失壹店。這里我提供一種方法:
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.annotation.IdRes
/**
* 允許彈窗文本選擇
*/
fun enableMessageSelection(
alertDialog: AlertDialog,
@IdRes messageTextViewId: Int = android.R.id.message,
) {
alertDialog.window?.findViewById<TextView>(messageTextViewId)?.setTextIsSelectable(true)
}
/**
* 禁止彈窗文本選擇
*/
fun disableMessageSelection(
alertDialog: AlertDialog,
@IdRes messageTextViewId: Int = android.R.id.message,
) {
alertDialog.window?.findViewById<TextView>(messageTextViewId)?.setTextIsSelectable(false)
}
說明
AlertDialog默認(rèn)的TextView有一個(gè)名為message
的ID僚稿,但它是屬于安卓系統(tǒng)自帶的,所以我們需要使用android.R.id.message
獲取它。接著就可以調(diào)用setTextIsSelectable(Boolean)
方法設(shè)置其是否可以選擇了名挥。