preface

For Jetpack Compose, the first step is to start with TextView. Make a note of any small problems you encounter at the beginning of your study.

I. Initial code

@Composable
fun MyText(a) {
    var text = "Ha ha ha."
    Text(
        text = text,
        modifier = Modifier.clickable {
            text = "Ha ha"})}Copy the code

Because Compose is responsive programming, you’re adding a listener to your Text. However, there is no reaction no matter how hard you try, as shown in the picture below:

2, remember, mutableStateOf initial use

After a bit of searching, I found that remember and mutableStateOf also exist, so I added them to try:

As you can see from the image above, there is a red warning. If you hover the mouse over text, you can see that text is MutableState< String > and not String. So change the code to something like this:

@Composable
fun MyText(a) {
//    var text = "哈哈哈"
    val text = remember {
        mutableStateOf("Ha ha ha.")
    }
    Text(
        text = text.value,
        modifier = Modifier.clickable {
            text.value = "Ha ha"})}Copy the code

After adding a value, we can get and set the value in MutableState< String > and see what we want:

3, The use of by

It’s a little bit of a hassle to evaluate and assign on top. Delegating by saves this trouble:

Now, you can see that text is not a String like before, so we can do it directly. Here is the complete code:

class MainActivity : ComponentActivity() {
    @SuppressLint("ResourceType")
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContent {
            MyComposeTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    MyText()
                }
            }
        }
    }
}

@Composable
fun MyText(a) {
//    var text = "哈哈哈"
    var text by remember {
        mutableStateOf("Ha ha ha.")
    }
    Text(
        text = text,
        modifier = Modifier.clickable {
            text = "Ha ha"})}Copy the code

3, By the memory

I wonder if anyone has forgotten by? I don’t want to forget the key words. But I forgot. Property delegate is implemented in Kotlin using BY. The grammatical structure is:

val/var < Property Name > : < Type > by  < Expression >
val/var A: < Type > by B
Copy the code

For a Property Name that specifies a delegate object, its implementation logic is handed over to the delegate object to handle the implementation. The delegate object (B) takes over the getter and setter operations for the property (A). So, the delegate object (B) provides getValue and setValue methods (val does not) for (A). In Compose, when val is changed to var, AS will prompt you to set setValue.

3, remember, mutableStateOf

Back to the theme remember and mutableStateOf, my understanding of these two is as follows

MutableStateOf - Listens for variables and triggers redraw when values change. Remember -- Record the value of the variable so that the next time you use the variable, it is not the initial value.Copy the code

We’ll be using them a lot in Compose’s journey.

We can see that there are many kinds of them in the prompt function of AS, and most of them are often used.

conclusion

That’s all for this article. Bye!