Jetpack撰写 是Google提供的新工具包。这对于设计漂亮的UI设计很有用。按钮是Android中的UI组件,用于在不同屏幕之间导航,并且在按钮的帮助下,用户可以与您的应用进行交互。借助按钮,用户可以在您的应用程序内执行多项操作。在本文中,我们将介绍使用Jetpack compose在Android中实现按钮的方法。
按钮小部件的属性
Attribute |
Description |
---|---|
onClick | To perform an action when your button is clicked by the app user. |
modifier | this parameter is used to add padding to our button. |
enabled | to enable or disable your button. |
border | this parameter is used to add border stroke to our button. |
shape | this parameter is used to add shape to our button. |
Text() | this parameter will be used to add text which is to be displayed on your button. |
分步实施
第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.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
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.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.res.colorResource
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 {
GFGAppTheme {
// A surface container using the 'background'
// color from the theme
Surface(color = MaterialTheme.colors.background) {
// at below line we are calling
// our function for button.
MyButton();
}
}
}
}
}
// @Preview function is use to see preview
// for our composable function in preview section.
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
GFGAppTheme {
// we are passing our composable
// function to display its preview.
MyButton();
}
}
@Composable
fun MyButton() {
Column(
// we are using column to align our
// imageview to center of the screen.
modifier = Modifier.fillMaxWidth().fillMaxHeight(),
// below line is used for specifying
// vertical arrangement.
verticalArrangement = Arrangement.Center,
// below line is used for specifying
// horizontal arrangement.
horizontalAlignment = Alignment.CenterHorizontally,
) {
// below line is use to get
// the context for our app.
val context = ContextAmbient.current
// below line is use to create a button.
Button(
// below line is use to add onclick
// parameter for our button onclick
onClick = {
// when user is clicking the button
// we are displaying a toast message.
Toast.makeText(context, "Welcome to Geeks for Geeks", Toast.LENGTH_LONG).show()
},
// in below line we are using modifier
// which is use to add padding to our button
modifier = Modifier.padding(all = Dp(10F)),
// below line is use to set or
// button as enable or disable.
enabled = true,
// below line is use to
// add border to our button.
border = BorderStroke(width = 1.dp, brush = SolidColor(Color.Blue)),
// below line is use to add shape for our button.
shape = MaterialTheme.shapes.medium,
)
// below line is use to
// add text on our button
{
Text(text = "Geeks for Geeks", color = Color.White)
}
}
}
输出:
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!