如何找出包含特定字符串的项目被单击?
How to find out which item containing a specific string was clicked?
我当前的代码从字符串数组中获取项目,但如何使用该数组对特定字符串执行特定操作?例如。如果我单击包含字符串“Anpan”的项目,我希望发生特定事件。
strings.xml
<resources>
<string name="app_name">Breads</string>
<!--Array (198 items)-->
<string-array name="array_breads">
<item>Aish merahrah</item>
...
<item>Zwieback</item>
</string-array>
</resources>
MainActivity.kt
class MainActivity : ComponentActivity() {
private var itemArray: Array<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
itemArray = resources.getStringArray(R.array.array_breads)
super.onCreate(savedInstanceState)
setContent {
AdaptiveTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MyScaffold(itemArray = itemArray as Array<out String>)
}
}
}
}
}
@Composable
fun MyScaffold(itemArray: Array<out String>) {
val context = LocalContext.current
Scaffold(
topBar = {
SmallTopAppBar(
title = {
Text(text = stringResource(id = R.string.breads))
}
)
},
content = {
val listState = rememberLazyListState()
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = PaddingValues(top = 64.dp, bottom = 80.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(itemArray) { item ->
Text(modifier = Modifier.padding(16.dp), text = item,
style = MaterialTheme.typography.titleLarge)
}
}
}
}
在您的文本组件上添加可点击修饰符:
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = PaddingValues(top = 64.dp, bottom = 80.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(itemArray) { item ->
Text(modifier = Modifier
.padding(16.dp)
.clickable{// You can obtain 'item' here which item clicked.},
text = item,
style = MaterialTheme.typography.titleLarge)
}
}
这似乎太基础了,无法成为 post 但我们开始吧。
val comparableString = stringResource(R.string.anpan) //Initialise here
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = PaddingValues(top = 64.dp, bottom = 80.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(itemArray) { item ->
Text(
modifier = Modifier
.padding(16.dp)
.clickable {
if(item == comparableString) //Compare here
performSpecificTask()
else performGeneralTask()
},
text = item,
style = MaterialTheme.typography.titleLarge)
}
}
此处doSpecificTask()
和doGeneral...
是指您希望在那边完成的任务。只是用你自己的代码块替换调用,只是为了清楚。
我当前的代码从字符串数组中获取项目,但如何使用该数组对特定字符串执行特定操作?例如。如果我单击包含字符串“Anpan”的项目,我希望发生特定事件。
strings.xml
<resources>
<string name="app_name">Breads</string>
<!--Array (198 items)-->
<string-array name="array_breads">
<item>Aish merahrah</item>
...
<item>Zwieback</item>
</string-array>
</resources>
MainActivity.kt
class MainActivity : ComponentActivity() {
private var itemArray: Array<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
itemArray = resources.getStringArray(R.array.array_breads)
super.onCreate(savedInstanceState)
setContent {
AdaptiveTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MyScaffold(itemArray = itemArray as Array<out String>)
}
}
}
}
}
@Composable
fun MyScaffold(itemArray: Array<out String>) {
val context = LocalContext.current
Scaffold(
topBar = {
SmallTopAppBar(
title = {
Text(text = stringResource(id = R.string.breads))
}
)
},
content = {
val listState = rememberLazyListState()
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = PaddingValues(top = 64.dp, bottom = 80.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(itemArray) { item ->
Text(modifier = Modifier.padding(16.dp), text = item,
style = MaterialTheme.typography.titleLarge)
}
}
}
}
在您的文本组件上添加可点击修饰符:
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = PaddingValues(top = 64.dp, bottom = 80.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(itemArray) { item ->
Text(modifier = Modifier
.padding(16.dp)
.clickable{// You can obtain 'item' here which item clicked.},
text = item,
style = MaterialTheme.typography.titleLarge)
}
}
这似乎太基础了,无法成为 post 但我们开始吧。
val comparableString = stringResource(R.string.anpan) //Initialise here
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = PaddingValues(top = 64.dp, bottom = 80.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
items(itemArray) { item ->
Text(
modifier = Modifier
.padding(16.dp)
.clickable {
if(item == comparableString) //Compare here
performSpecificTask()
else performGeneralTask()
},
text = item,
style = MaterialTheme.typography.titleLarge)
}
}
此处doSpecificTask()
和doGeneral...
是指您希望在那边完成的任务。只是用你自己的代码块替换调用,只是为了清楚。