Java活用生活

ログイン

オンライン状況

オンラインユーザー1人
ログインユーザー0人
登録ユーザー2人

カウンタ

COUNTER335455

日誌

Kotlin事始め >> 記事詳細
2021/05/30

Jetpack Compose基本サンプル

固定リンク | by:aramaki
Jetpackの基本サンプで、2021 5/30日時点で、エラーなく動くサンプルアプリをを掲載する。書籍サンプルもネットサンプルも動かないため、約1日半費やした。

MainActivity.kt
--------------------------------------------------
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mysecondcomposeapp.ui.theme.MySecondComposeAppTheme

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {

            MySecondComposeAppTheme {
                Surface(color = MaterialTheme.colors.background) {
                    MainContent()
                }
            }
        }
    }

    @Composable
    fun MainContent(){
        Column(
            Modifier
                .padding(25.dp)
        ) {
            val checkedState = remember { mutableStateOf(false)}

            var message = remember { mutableStateOf("Hello!") }

            var txtState = remember { mutableStateOf("0")}
            Row(
                Modifier.padding(10.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = message.value,
                    style= TextStyle(
                        color= Color.Red,
                        fontSize = 28.sp
                    )
                )
                Checkbox(
                    checked = checkedState.value,
                    onCheckedChange = { checkedState.value = it },
                )

                Text(
                    text = "Do you like coffee?",
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(start = 10.dp),
                )
            }

            var result = "unchecked"
            var textColor = Color.Red
            if (checkedState.value){
                result = "checked"
                textColor = Color.Blue
            }

            Text(
                text = "Checkbox is $result.",
                fontSize = 28.sp,
                fontStyle = FontStyle.Normal,
                fontWeight = FontWeight.Normal,
                fontFamily = FontFamily.SansSerif,
                modifier = Modifier
                    .padding(20.dp),
                textAlign = TextAlign.Center,
                color = textColor
            )
            Button(onClick = {

            }) {
                Text(
                    text = "Click",
                    style= TextStyle(
                        color= Color.White,
                        fontSize = 20.sp
                    )
                )
                val n = txtState.value.toInt()
                var total = 0
                for (n in 1..n) {
                    total += n
                }
                message.value = "total: $total"
            }

        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        MainContent()
    }
}

22:55 | 投票する | 投票数(0)