当前位置: 首页 > news >正文

Jetpack Compose中使用Android View

使用AndroidView创建日历

@Composable
fun AndroidViewPage() {AndroidView(factory = {CalendarView(it)},modifier = Modifier.fillMaxWidth(),update = {it.setOnDateChangeListener { view, year, month, day ->Toast.makeText(view.context, "${year}年${month + 1}月${day}日",Toast.LENGTH_LONG).show()}})
}

使用Android原生视图需要引入AndroidView组件,factory属性中加入CalendarView日历视图组件,update属性中设置日历的点击事件。

使用WebView

 

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "SetJavaScriptEnabled")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun WebViewPage() {val webView = rememberWebViewWithLifecycle()Scaffold(content = {AndroidView(factory = {webView}, modifier = Modifier.fillMaxSize().background(Color.Red),update = { webView ->// 设置支持JavaScriptval webSettings = webView.settingswebSettings.javaScriptEnabled = truewebView.loadUrl("https://www.baidu.com")})})
}@Composable
fun rememberWebViewWithLifecycle(): WebView {val context = LocalContext.currentval webView = remember {WebView(context)}val lifecycleObserver = rememberWebViewLifecycleObserver(webView)val lifecycle = LocalLifecycleOwner.current.lifecycleDisposableEffect(lifecycle) {lifecycle.addObserver(lifecycleObserver)onDispose {lifecycle.removeObserver(lifecycleObserver)}}return webView
}@Composable
private fun rememberWebViewLifecycleObserver(webView: WebView): LifecycleEventObserver =remember(webView) {LifecycleEventObserver { _, event ->when (event) {Lifecycle.Event.ON_RESUME -> webView.onResume()Lifecycle.Event.ON_PAUSE -> webView.onPause()Lifecycle.Event.ON_DESTROY -> webView.destroy()else -> Log.e("WebView", event.name)}}}

使用Android布局

使用Android布局用到了Viewbinding,这里需要引入ui-viewbinding库

implementation "androidx.compose.ui:ui-viewbinding:1.5.0-alpha01"

这是android_view.xml原生页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><EditTextandroid:id="@+id/editName"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="30dp"android:hint="name" /><EditTextandroid:id="@+id/editPassword"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginHorizontal="30dp"android:hint="password"android:inputType="textPassword" /><Buttonandroid:id="@+id/btnLogin"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:textAllCaps="false"android:layout_marginTop="30dp"android:text="Login" />
</LinearLayout>

Compose中使用原生布局并操作

@Composable
fun AndroidViewBindingPage() {val context = LocalContext.currentAndroidViewBinding(factory = { inflate, parent, attachToParent ->AndroidViewBinding.inflate(inflate, parent, attachToParent)},modifier = Modifier.fillMaxSize(),update = {btnLogin.setOnClickListener {val name = editName.text.toString().trim()val password = editPassword.text.toString().trim()toLogin(context, name, password)}})
}fun toLogin(context: Context, name: String, password: String) {if (name.isEmpty() || password.isEmpty()) {Toast.makeText(context, "请输入完整信息", Toast.LENGTH_SHORT).show()return}Toast.makeText(context, "登录信息为:name:${name}, password:${password}", Toast.LENGTH_SHORT).show()
}

引用非常简单,只要通过ViewBinding组件在factory属性中添加页面的引用就行,update属性下对页面进行操作逻辑。

http://www.lryc.cn/news/268945.html

相关文章:

  • 《MySQL》事务篇
  • 高阶组件和高阶函数是什么
  • 初步认识API安全
  • MybatisX逆向工程方法
  • 每日一题:LeetCode-LCR 179. 查找总价格为目标值的两个商品
  • SpringBoot 3.2.0 基于Logback定制日志框架
  • 微软发布安卓版Copilot,可免费使用GPT-4、DALL-E 3
  • 【STM32】程序在SRAM中运行
  • docker 部署mysql
  • 科荣AIO ReportServlet存在目录遍历漏洞
  • Ubuntu Desktop 22.04 桌面主题配置
  • SuperMap iServer发布的ArcGIS REST 地图服务如何通过ArcGIS API进行要素查询
  • H5向微信小程序发送信息(小程序web-view打开H5)
  • 白话机器学习的数学-1-回归
  • ubuntu22下安装minconda
  • 如何借助边缘网关打造智慧配电房安全方案
  • k8s的二进制部署
  • Python基础语法总结
  • 矩阵理论基本知识
  • 《深入理解Java虚拟机(第三版)》读书笔记:Java内存区域与内存溢出异常、垃圾收集器与内存分配策略
  • android 手机主界面侧滑退出app问题
  • spring boot 配置全局日期和时间格式
  • GoLang学习之路,对Elasticsearch的使用,一文足以(包括泛型使用思想)(二)
  • 鸿蒙APP的代码规范
  • 蓝桥杯-每日刷题-027
  • 安装Node修改Node镜像地址搭建Vue脚手架创建Vue项目
  • git 学习 之一个规范的 commit 如何写
  • 2023 年人工智能研究与技术排名前 10 的国家
  • 留言板(Mybatis连接数据库版)
  • 第十二章 Sleuth分布式请求链路跟踪