|
1 | 1 | package com.jetbrains.kmm.androidApp |
2 | 2 |
|
3 | | -import androidx.appcompat.app.AppCompatActivity |
4 | 3 | import android.os.Bundle |
5 | | -import android.text.Editable |
6 | | -import android.text.TextWatcher |
7 | | -import android.widget.EditText |
8 | | -import com.jetbrains.kmm.shared.Greeting |
| 4 | +import androidx.activity.ComponentActivity |
| 5 | +import androidx.activity.compose.setContent |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.Row |
| 9 | +import androidx.compose.foundation.layout.fillMaxSize |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.layout.width |
| 12 | +import androidx.compose.foundation.text.KeyboardOptions |
| 13 | +import androidx.compose.material3.Text |
| 14 | +import androidx.compose.material3.TextField |
| 15 | +import androidx.compose.runtime.getValue |
| 16 | +import androidx.compose.runtime.mutableStateOf |
| 17 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 18 | +import androidx.compose.runtime.setValue |
| 19 | +import androidx.compose.ui.Alignment |
| 20 | +import androidx.compose.ui.Modifier |
| 21 | +import androidx.compose.ui.text.input.KeyboardType |
| 22 | +import androidx.compose.ui.unit.dp |
9 | 23 | import com.jetbrains.kmm.shared.Calculator |
10 | | -import android.widget.TextView |
11 | | -import com.jetbrains.androidApp.R |
| 24 | +import com.jetbrains.kmm.shared.Greeting |
12 | 25 |
|
13 | 26 | fun greet(): String { |
14 | 27 | return Greeting().greeting() |
15 | 28 | } |
16 | 29 |
|
17 | | -class MainActivity : AppCompatActivity() { |
| 30 | +class MainActivity : ComponentActivity() { |
18 | 31 | override fun onCreate(savedInstanceState: Bundle?) { |
19 | 32 | super.onCreate(savedInstanceState) |
20 | | - setContentView(R.layout.activity_main) |
| 33 | + setContent { |
| 34 | + Box( |
| 35 | + modifier = Modifier.fillMaxSize(), |
| 36 | + contentAlignment = Alignment.Center |
| 37 | + ) { |
| 38 | + Column( |
| 39 | + horizontalAlignment = Alignment.Start, |
| 40 | + ) { |
| 41 | + Text(greet(), Modifier.padding(8.dp)) |
21 | 42 |
|
22 | | - val tv: TextView = findViewById(R.id.textView) |
23 | | - tv.text = greet() |
| 43 | + var firstNumber by rememberSaveable { mutableStateOf("") } |
| 44 | + var secondNumber by rememberSaveable { mutableStateOf("") } |
24 | 45 |
|
25 | | - val numATV: EditText = findViewById(R.id.editTextNumberDecimalA) |
26 | | - val numBTV: EditText = findViewById(R.id.editTextNumberDecimalB) |
| 46 | + Row(verticalAlignment = Alignment.CenterVertically) { |
| 47 | + TextField( |
| 48 | + value = firstNumber, |
| 49 | + onValueChange = { firstNumber = it }, |
| 50 | + modifier = Modifier.width(100.dp), |
| 51 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |
| 52 | + ) |
| 53 | + Text(text = "+", modifier = Modifier.padding(4.dp)) |
| 54 | + TextField( |
| 55 | + value = secondNumber, |
| 56 | + onValueChange = { secondNumber = it }, |
| 57 | + modifier = Modifier.width(100.dp), |
| 58 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |
| 59 | + ) |
27 | 60 |
|
28 | | - val sumTV: TextView = findViewById(R.id.textViewSum) |
29 | | - |
30 | | - val textWatcher = object: TextWatcher { |
31 | | - override fun afterTextChanged(s: Editable?) { |
32 | | - try { |
33 | | - val numA = Integer.parseInt(numATV.text.toString()) |
34 | | - val numB = Integer.parseInt(numBTV.text.toString()) |
35 | | - sumTV.text = "= " + Calculator.sum(numA, numB).toString() |
36 | | - } catch(e: NumberFormatException) { |
37 | | - sumTV.text = "= 🤔" |
| 61 | + val first = firstNumber.toIntOrNull() |
| 62 | + val second = secondNumber.toIntOrNull() |
| 63 | + Text( |
| 64 | + text = if (first != null && second != null) { |
| 65 | + "= ${Calculator.sum(first, second)}" |
| 66 | + } else { |
| 67 | + "= \uD83E\uDD14" |
| 68 | + }, |
| 69 | + modifier = Modifier |
| 70 | + .width(100.dp) |
| 71 | + .padding(4.dp) |
| 72 | + ) |
| 73 | + } |
38 | 74 | } |
39 | 75 | } |
40 | | - |
41 | | - override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} |
42 | | - |
43 | | - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} |
44 | 76 | } |
45 | | - |
46 | | - numATV.addTextChangedListener(textWatcher) |
47 | | - numBTV.addTextChangedListener(textWatcher) |
48 | | - |
49 | 77 | } |
50 | 78 | } |
0 commit comments