简体   繁体   中英

Assignment as parameter in jetpack compose

What is the difference between Text(text="Hi") and Text("Hi")?
How can I make CustomText( text = "Hi")

class MainActivity : Base() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        MaterialTheme {
            Column {
                Text(text = "Hi")
                Text("Hi")

                //CustomText(text = "Hi")
                CustomText("Hi")
            }
        }
    }
}
}

open class Base : AppCompatActivity() {
    fun CustomText(inputString: String) {
        Text(
            inputString,
            style = TextStyle(color = Color.Green, fontSize = 22.sp)
        )
    }
}

What is the difference between Text(text="Hi") and Text("Hi")?

There is no difference between these. The text="Hi" version is simply using a named argument .

How can I make CustomText( text = "Hi")

Your CustomText needs a String argument named text . That is, you should change

fun CustomText(inputString: String) {

to

fun CustomText(text: String) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM