📌  相关文章
📜  Android中使用Jetpack Compose的复选框

📅  最后修改于: 2021-05-09 18:26:30             🧑  作者: Mango

复选框是可组合的函数,用于表示Android中任何项目的两种状态。它用于区分项目和项目列表。在本文中,我们将介绍使用Jetpack Compose在Android中实现Simple Checkbox的方法。

复选框的属性

Attributes

Uses

checked  this is used to set our checkbox checked or unchecked on app launch.
onCheckedChange  this is a callback that will receive when there is a change in events whether the checkbox is checked or unchecked.
modifier  this is used to add beautification to our checkbox in the sense of padding, margin, and other properties. 
color  this parameter is used to add color to our checkbox in default case the checkbox color is secondary color in the app theme.

分步实施

步骤1:创建一个新项目

要在Android Studio Canary版本中创建新项目,请参阅如何使用Jetpack Compose在Android Studio Canary版本中创建新项目。

步骤2:使用MainActivity.kt文件

导航到应用程序> Java >您的应用程序的程序包名称,然后打开MainActivity.kt文件。在该文件内,添加以下代码。在代码内部添加了注释,以更详细地了解代码。

Kotlin
import android.graphics.drawable.shapes.Shape
import android.media.Image
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.*
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.gfgapp.ui.GFGAppTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.semantics.SemanticsProperties.ToggleableState
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column {
                // in below line we are 
                  // calling a checkbox method. 
                SimpleCheckboxComponent()
            }
        }
    }
}
  
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    GFGAppTheme {
        SimpleCheckboxComponent();
    }
}
  
  
@Composable
fun SimpleCheckboxComponent() {
    // in below line we are setting
     // the state of our checkbox.
    val checkedState = remember { mutableStateOf(true) }
    // in below line we are displaying a row
      // and we are creating a checkbox in a row. 
    Row {
        Checkbox(
            // below line we are setting 
              // the state of checkbox.
            checked = checkedState.value,
            // below line is use to add padding
              // to our checkbox. 
            modifier = Modifier.padding(16.dp),
            // below line is use to add on check 
              // change to our checkbox. 
            onCheckedChange = { checkedState.value = it },
        )
        // below line is use to add text to our check box and we are 
         // adding padding to our text of checkbox
        Text(text = "Checkbox Example", modifier = Modifier.padding(16.dp))
    }
}


现在运行您的应用程序,并查看该应用程序的输出。

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!