Код: Выделить всё
IconButton(onClick = { webView.evaluateJavascript("console.log('Hello World');", null) })Код: Выделить всё
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
PDFJSTheme {
MainContent()
}
}
}
}
@SuppressLint("SetJavaScriptEnabled", "UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainContent() {
Scaffold(
topBar = {
TopAppBar(
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
titleContentColor = MaterialTheme.colorScheme.primary,
),
title = {
Text("PDF.js")
},
actions = {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = "image"
)
}
)
},
bottomBar = {
BottomAppBar(
actions = {
/* Freehand Highlighter */
IconButton(onClick = {
webView.evaluateJavascript("console.log('Hello World');", null)
}) {
Icon(painterResource(id = R.drawable.sharp_ink_highlighter_24), contentDescription = "Localized description")
}
/* Text Highlighter */
IconButton(onClick = { /* do something */ }) {
Icon(
Icons.Default.TextFormat,
contentDescription = "Localized description",
)
}
/* Draw */
IconButton(onClick = { /* do something */ }) {
Icon(
Icons.Default.Draw,
contentDescription = "Localized description",
)
}
/* Eraser */
IconButton(onClick = { /* do something */ }) {
Icon(painterResource(id = R.drawable.sharp_ink_eraser_24), contentDescription = "Localized description")
}
/* Add Image */
IconButton(onClick = { /* do something */ }) {
Icon(
Icons.Default.Image,
contentDescription = "Localized description",
)
}
}
)
},
) { innerPadding ->
Column(
modifier = Modifier
.padding(innerPadding),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
MyWebView()
}
}
}
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun MyWebView() {
AndroidView(
factory = { context ->
WebView(context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
settings.javaScriptEnabled = true
webViewClient = WebViewClient()
WebView.setWebContentsDebuggingEnabled(true)
settings.loadWithOverviewMode = true
settings.useWideViewPort = true
settings.allowFileAccess = true
settings.mixedContentMode = MIXED_CONTENT_ALWAYS_ALLOW
settings.allowFileAccessFromFileURLs = true
settings.allowUniversalAccessFromFileURLs = true
}
},
update = { webView ->
webView.loadUrl("file:///android_asset/web/viewer.html")
}
)
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... iconbutton
Мобильная версия