Compose输入框

TextField

label设置提示内容,TextFieldDefaults.textFieldColors设置输入框背景颜色

TextField(
    modifier = Modifier
        .fillMaxWidth()
        .padding(5.dp),
    value = text.value,
    onValueChange = { text.value = it },
    label = { Text(text = "请输入内容") },
    colors = TextFieldDefaults.textFieldColors(
        //容器颜色
        containerColor = colorResource(id = R.color.white),
    )
)

OutlinedTextField

OutlinedTextField(
    modifier = Modifier
        .fillMaxWidth()
        .padding(5.dp),
    value = text.value,
    onValueChange = { text.value = it },
    label = { Text("请输入内容") },
    colors = TextFieldDefaults.textFieldColors(
        //容器颜色
        containerColor = colorResource(id = R.color.white),
    )
)

自定义TextField

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TextFieldTest() {
    val text = remember { mutableStateOf("你好") }
    val context = LocalContext.current
    TextField(
        value = text.value,
        onValueChange = { text.value = it },
        label = { Text("请输入内容") },
        maxLines = 2,
        textStyle = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold),
        modifier = Modifier
            .fillMaxWidth()
            .padding(5.dp),
        colors = TextFieldDefaults.textFieldColors(
            //容器颜色
            containerColor = colorResource(id = R.color.white),
        ),
        //键盘设置
        keyboardOptions = KeyboardOptions(
            //设置全部字符大写
            capitalization = KeyboardCapitalization.Characters,
            //输入类型设置为Email
            keyboardType = KeyboardType.Email,
            //开启自动更正
            autoCorrect = true,
            //IME动作设置为搜索
            imeAction = ImeAction.Search
        ),
        keyboardActions = KeyboardActions(onSearch = {
            Toast.makeText(context, "Compose,${text.value}", Toast.LENGTH_SHORT).show()
        })
    )
}

完整代码:

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.cwj.composedemo.ui.theme.ComposeDemoTheme

/**
 * EditText案例
 */
class EditTextActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Home()
                }
            }
        }
    }
}

@Composable
fun Home() {
    Column(
        modifier = Modifier.fillMaxSize(),
    ) {
        TextField()

        OutlinedTextField()

        TextFieldTest()
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TextField() {
    val text = remember { mutableStateOf("你好") }
    TextField(
        modifier = Modifier
            .fillMaxWidth()
            .padding(5.dp),
        value = text.value,
        onValueChange = { text.value = it },
        label = { Text(text = "请输入内容") },
        colors = TextFieldDefaults.textFieldColors(
            //容器颜色
            containerColor = colorResource(id = R.color.white),
        )
    )
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun OutlinedTextField() {
    val text = remember { mutableStateOf("你好") }
    OutlinedTextField(
        modifier = Modifier
            .fillMaxWidth()
            .padding(5.dp),
        value = text.value,
        onValueChange = { text.value = it },
        label = { Text("请输入内容") },
        colors = TextFieldDefaults.textFieldColors(
            //容器颜色
            containerColor = colorResource(id = R.color.white),
        )
    )
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TextFieldTest() {
    val text = remember { mutableStateOf("你好") }
    val context = LocalContext.current
    TextField(
        value = text.value,
        onValueChange = { text.value = it },
        label = { Text("请输入内容") },
        maxLines = 2,
        textStyle = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold),
        modifier = Modifier
            .fillMaxWidth()
            .padding(5.dp),
        colors = TextFieldDefaults.textFieldColors(
            //容器颜色
            containerColor = colorResource(id = R.color.white),
        ),
        //键盘设置
        keyboardOptions = KeyboardOptions(
            //设置全部字符大写
            capitalization = KeyboardCapitalization.Characters,
            //输入类型设置为Email
            keyboardType = KeyboardType.Email,
            //开启自动更正
            autoCorrect = true,
            //IME动作设置为搜索
            imeAction = ImeAction.Search
        ),
        keyboardActions = KeyboardActions(onSearch = {
            Toast.makeText(context, "Compose,${text.value}", Toast.LENGTH_SHORT).show()
        })
    )
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun GreetingPreview() {
    ComposeDemoTheme {
       Home()
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

举儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值