Android Compose 九:interactionSource 的使用

先上官方文檔

InteractionSource
InteractionSource represents a stream of Interactions corresponding to events emitted by a component. These Interactions can be used to change how components appear in different states, such as when a component is pressed or dragged.
翻譯
InteractionSource表示與組件發出的事件相對應的交互流。這些交互可用于更改組件在不同狀態下的顯示方式,例如按下或拖動組件時。

也就是說它應該是用來記錄不同的交互狀態
官方示例簡化

val interactionSource = remember { MutableInteractionSource() }//拖拽
val draggable = Modifier.draggable(interactionSource = interactionSource,orientation = Orientation.Horizontal,state = rememberDraggableState { /* update some business state here */ }
)//點擊
val clickable = Modifier.clickable(interactionSource = interactionSource,indication = LocalIndication.current
) { /* update some business state here */ }//狀態值變化結果
val isDragged by interactionSource.collectIsDraggedAsState()
val isPressed by interactionSource.collectIsPressedAsState()//定義變化后的 text 和 color  下方Box 中 border使用了color    Text 使用了text
val (text, color) = when {isDragged && isPressed -> "Dragged and pressed" to Color.RedisDragged -> "Dragged" to Color.GreenisPressed -> "Pressed" to Color.Blue// Default / baseline stateelse -> "Drag me horizontally, or press me!" to Color.Black
}Box(Modifier.fillMaxSize().wrapContentSize().size(width = 240.dp, height = 80.dp)
) {Box(Modifier.fillMaxSize().then(clickable).then(draggable).border(BorderStroke(3.dp, color)).padding(3.dp)) {Text(text, style = LocalTextStyle.current.copy(textAlign = TextAlign.Center),modifier = Modifier.fillMaxSize().wrapContentSize())}
}

將以上代碼放入項目運行效果

  • 按下時 文字變化 邊框邊藍
  • 拖拽時 文字變化 邊框變綠
    請添加圖片描述

官方示例2
以下是省略代碼

 val interactions = remember { mutableStateListOf<Interaction>() }  //創建了個 集合
//用來記錄 交互事件    添加或從集合中移除LaunchedEffect(interactionSource) {interactionSource.interactions.collect { interaction ->when (interaction) {is PressInteraction.Press -> interactions.add(interaction)is PressInteraction.Release -> interactions.remove(interaction.press)is PressInteraction.Cancel -> interactions.remove(interaction.press)is DragInteraction.Start -> interactions.add(interaction)is DragInteraction.Stop -> interactions.remove(interaction.start)is DragInteraction.Cancel -> interactions.remove(interaction.start)}}}//集合狀態變化 文字變化val text = when (interactions.lastOrNull()) {is DragInteraction.Start -> "Dragged"is PressInteraction.Press -> "Pressed"else -> "No state"}//判斷集合中 交互狀態  顯示不同的效果val pressed = interactions.any { it is PressInteraction.Press }Text(text = if (pressed) "Pressed" else "Not pressed",style = LocalTextStyle.current.copy(textAlign = TextAlign.Center),modifier = Modifier.fillMaxSize().wrapContentSize())
val dragged = interactions.any { it is DragInteraction.Start }Text(text = if (dragged) "Dragged" else "Not dragged",style = LocalTextStyle.current.copy(textAlign = TextAlign.Center),modifier = Modifier.fillMaxSize().wrapContentSize())

效果
請添加圖片描述

使用

前面我們玩過的
TextField
Button
Switch
等組件 都有 interactionSource 屬性
并且
Modifier.indication(
interactionSource = interactionSource,
indication = LocalIndication.current
)
也可以設置interactionSource

下面我們就簡單玩玩它

實現輸入框獲取到焦點時 提示文字的改變
  • collectIsDraggedAsState 拖拽交互
  • collectIsFocusedAsState 焦點交互
  • collectIsHoveredAsState 懸停交互
  • collectIsPressedAsState 點擊交互

創建兩個TextField 可以用來切換焦點
直接上代碼吧

   val inPut = remember {mutableStateOf("")}val interactionSource = remember { MutableInteractionSource() }//獲取到當前焦點狀態val isFocused by interactionSource.collectIsFocusedAsState()Column(modifier = Modifier.padding(10.dp)) {TextField(value = inPut.value,onValueChange ={inPut.value = it},modifier = Modifier.fillMaxWidth().height(50.dp),shape = CircleShape,colors = TextFieldDefaults.textFieldColors(focusedIndicatorColor = Color.Transparent,disabledIndicatorColor = Color.Transparent,unfocusedIndicatorColor = Color.Transparent,),placeholder = {Text(text = "${if(isFocused)"請輸入內容" else "這是一個提示語"}")},interactionSource = interactionSource,)Spacer(modifier = Modifier.height(20.dp))TextField(value = inPut.value,onValueChange ={inPut.value = it},modifier = Modifier.fillMaxWidth().height(50.dp),shape = CircleShape,colors = TextFieldDefaults.textFieldColors(focusedIndicatorColor = Color.Transparent,disabledIndicatorColor = Color.Transparent,unfocusedIndicatorColor = Color.Transparent,),placeholder = {Text(text = "這是一個提示語")},)}

效果 上邊的TextField 獲取和失去焦點時,文字改變
請添加圖片描述
更多的用法一般是
當TextField 獲取到焦點時邊框或者背景變化 用以表示我們選中了該輸入框
于是 我們包一層


@ExperimentalMaterial3Api
@Composable
fun MyTextField(value: String,onValueChange: (String) -> Unit,modifier: Modifier = Modifier,placeholder: @Composable (() -> Unit)? = null,
) {val interactionSource = remember { MutableInteractionSource() }//獲取到當前焦點狀態val isFocused by interactionSource.collectIsFocusedAsState()val color = when{isFocused -> Color.Redelse -> Color.Black}TextField(value = value,onValueChange = onValueChange,modifier = modifier.border(3.dp,color, shape = CircleShape),shape = CircleShape,colors = TextFieldDefaults.textFieldColors(focusedIndicatorColor = Color.Transparent,disabledIndicatorColor = Color.Transparent,unfocusedIndicatorColor = Color.Transparent,),placeholder = placeholder,interactionSource = interactionSource,)}

然后使用

   val inPut = remember {mutableStateOf("")}Column(modifier = Modifier.padding(10.dp)) {MyTextField(value = inPut.value,onValueChange ={inPut.value = it},modifier = Modifier.fillMaxWidth().height(50.dp),placeholder = {Text(text = "這是一個提示語")})Spacer(modifier = Modifier.height(20.dp))MyTextField(value = inPut.value,onValueChange ={inPut.value = it},modifier = Modifier.fillMaxWidth().height(50.dp),placeholder = {Text(text = "這是一個提示語")})}

效果如下
請添加圖片描述

那么問題來了,你們叫它什么名字呢 狀態選擇器么

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/18344.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/18344.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/18344.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據庫技術都涵蓋那些內容

數據庫技術涵蓋了關系型數據庫&#xff08;RDBMS&#xff09;、非關系型數據庫&#xff08;NoSQL&#xff09;以及數據庫管理系統&#xff08;DBMS&#xff09;的其他方面。以下是一些我熟悉的數據庫技術&#xff1a; 關系型數據庫&#xff08;RDBMS&#xff09; MySQL&#…

溫故而知新-Spring篇【面試復習】

溫故而知新-Spring篇【面試復習】 前言版權推薦溫故而知新-Spring篇IOCAOP循環依賴springboot如果要對屬性文件中的賬號密碼加密如何實現&#xff1f;SpringBoot的優點Spring Boot 的核心注解是哪個&#xff1f;它主要由哪幾個注解組成的&#xff1f; 最后 前言 2023-7-31 15:…

Java RMI

RMI - 安全篇 RMI分為三個主體部分&#xff1a; *Client-客戶端*&#xff1a;客戶端調用服務端的方法 *Server-服務端*&#xff1a;遠程調用方法對象的提供者&#xff0c;也是代碼真正執行的地方&#xff0c;執行結束會返回給客戶端一個方法執行的結果。 *Registry-注冊中心…

詞嵌入nn.embedding的解釋

一、embedding如何處理文本 在NLP任務中&#xff0c;首先要對文本進行處理&#xff0c;將文本進行編碼轉換&#xff0c;形成向量表達&#xff0c;embedding處理文本的流程如下&#xff1a; &#xff08;1&#xff09;輸入一段文本&#xff0c;中文會先分詞&#xff08;如jieb…

python雙色球選號程序的實現與解析

新書上架~&#x1f447;全國包郵奧~ python實用小工具開發教程http://pythontoolsteach.com/3 歡迎關注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目錄 一、引言&#xff1a;雙色球選號游戲的魅力 二、程序設計與實現 1. 生成紅色球號碼 2. 生…

3.游戲中自定義數據類型的解讀分析

知識來源于騰訊課堂易道云 結構的解釋&#xff1a; 計算機里的所有東西都是用二進制表示的&#xff0c;二進制是數字&#xff0c;我們用的阿拉伯數字0-9這個數字是十進制&#xff0c;計算機用的是二進制只有0或1&#xff0c;然后都是一堆0或1的數字&#xff0c;游戲中怎么把這…

AD使用問題

設計流程&#xff1a; 1.先創建項目——添加原理圖&#xff0c;原理圖庫&#xff0c;PCB&#xff0c;PCB庫 2.畫原理圖庫和封裝庫 主要有三種方法&#xff1a; &#xff08;1&#xff09;手動畫庫和封裝&#xff0c;常常用于嘉立創查詢不到的器件 &#xff08;2&#xff0…

雙機多網口配置同網段地址,可以通過目的IP確定接收數據的網卡嗎?

環境 兩臺機器兩網卡同網段接入同一個二層交換機。 機器A ens38 00:0c:29:a4:8b:fb 10.0.0.11/24 ens39 00:0c:29:a4:8b:05 10.0.0.12/24 機器B ens38 00:0c:29:4f:a6:c4 10.0.0.21/24 ens39 00:0c:29:4f:a6:ce 10.0.0.22/24 初始ARP表 只有管理口接口的ARP表項&#xff0c…

浙江大學數據結構MOOC-課后習題-第十講-排序4 統計工齡

題目匯總 浙江大學數據結構MOOC-課后習題-拼題A-代碼分享-2024 題目描述 測試點 思路分析 這道題很明顯就是利用桶排序的思路 受到課程內容的影響&#xff0c;我一開始是想著建立一個鏈表數組&#xff0c;數組內每個元素下方都存放鏈表&#xff0c;最后再遍歷統計輸出。 但是&…

【華為OD機試-C卷D卷-200分】反射計數(C++/Java/Python)

【華為OD機試】-(A卷+B卷+C卷+D卷)-2024真題合集目錄 【華為OD機試】-(C卷+D卷)-2024最新真題目錄 題目描述 給定一個包含 0 和 1 的二維矩陣。 給定一個初始位置和速度,一個物體從給定的初始位置出發,在給定的速度下進行移動,遇到矩陣的邊緣則發生鏡面發射。 無論物體…

算法訓練營第四十二天 | LeetCode 42 不同路徑、LeetCode 63 不同路徑 II

LeetCode 62 不同路徑 這題首先確定下dp數組下標和含義。主要有兩種方式&#xff0c;一種是按照位置在數組中下標直接確定&#xff0c;另一種是依據遞推時邊上的位置需要再往上和往左遞推時會出界&#xff0c;將位置設為序號而非下標。這一題第二種方式會比較好一些。遞推邏輯也…

Android和flutter交互,maven庫的形式導入aar包

記錄遇到的問題&#xff0c;在網上找了很多資料&#xff0c;都是太泛泛了&#xff0c;使用后&#xff0c;還不能生效&#xff0c;缺少詳細的說明&#xff0c;或者關鍵代碼缺失&#xff0c;我遇到的問題用紅色的標注了 導入aar包有兩種模式 1.比較繁瑣的&#xff0c;手動將aar…

The Sandbox DAO:投票決定元宇宙的未來!

賦予用戶治理權&#xff0c;打造由社群運營的開放式數碼國度 隨著The Sandbox DAO的啟動&#xff0c;我們邀請全球社群——這個新數字國度的公民們——提出建議并參與治理&#xff0c;共同塑造開放元宇宙的未來。 介紹 在The Sandbox&#xff0c;我們正在建立一個開放的元宇宙…

聚酯輸送帶的原材料

揭秘聚酯輸送帶原材料&#xff1a;高效耐用背后的秘密武器 在現代化工業生產中&#xff0c;聚酯輸送帶以其出色的耐用性和穩定性&#xff0c;成為眾多行業不可或缺的傳輸工具。然而&#xff0c;你是否好奇&#xff0c;究竟是什么原材料賦予了聚酯輸送帶如此卓越的性能&#xf…

opencv c++編程基礎

1、圖片的本質 圖像在 OpenCV 中的本質 在 OpenCV 中&#xff0c;圖像被表示為一個多維數組&#xff0c;其中每個元素對應于圖像中的單個像素。圖像的維度取決于其通道數和像素數。 **通道數&#xff1a;**圖像可以有多個通道&#xff0c;每個通道存儲圖像的不同信息。例如&…

一維掃描線,有多少對相交線段

D - Intersecting Intervals 目錄 正向&#xff1a; 反向&#xff1a; 正向&#xff1a; 從左往右掃描&#xff0c;記錄當前邊數。 來了新邊&#xff0c;它此刻與當前邊數相交&#xff0c;加到總數中。邊結束&#xff0c;當前邊數中減去即可。 const int maxn 5e55; int …

Uniapp橫豎屏切換讓某一個頁面只能橫屏或者豎屏

先看官方屬性 plus.screen.lockOrientation(default); // 默認橫豎屏切換 plus.screen.lockOrientation(portrait-primary);// 豎屏展示 plus.screen.lockOrientation(landscape-primary); // 強制橫屏簡單需求&#xff1a;允許橫豎屏切換 在 page.json增加以下代碼 "gl…

李廉洋:5.22黃金原油高位震蕩,今日最新行情分析策略。

黃金消息面分析&#xff1a;根據4月份的通脹數據&#xff0c;加拿大央行6月5日降息應該是“理所當然的”。加拿大的整體通貨膨脹率在4月份降至2.7%&#xff0c;為自2021年初以來的最低水平&#xff0c;核心CPI中加拿大央行的兩項首選數據均降至3%以下。加拿大央行在決定降息之前…

鴻蒙學習第一課--認識目錄結構

項目結構介紹 module.json5 src > main > module.json5&#xff1a;Stage模型模塊配置文件。主要包含HAP包的配置信息、應用/服務在具體設備上的配置信息以及應用/服務的全局配置信息。具體的配置文件說明&#xff0c;詳見module.json5配置文件。 資源分類和訪問 關于s…

vue使用asiox 下載后端返回的excel數據流

一、前端代碼 <template><div class"hello"><h1>{{ msg }}</h1><button style"color: brown" click"exportExcel">excel導出</button></div> </template><script> import axios from &q…