页面·免费
登录界面(Compose)
Material 3 的邮箱和密码登录界面。点按 Sign in 时检查两个输入框,显示服务器返回的错误,还可以加上“忘记密码”和指纹按钮。BiometricPrompt 需要你自己调用。
免费安装。
如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。
预览
LoginScreen.kt · 176 行
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
/**
* Email and password sign-in screen in Material 3.
*
* - Checks the email format and a minimum password length when the user taps Sign in, then shows
* the error under the field.
* - [errorMessage] is for the server's answer ("Wrong password"); screen readers announce it.
* - [isLoading] shows a spinner in the button and blocks a second tap.
* - [onBiometricSignIn]: pass it and a "Use fingerprint or face" button appears. This file does
* not call BiometricPrompt: do that in your callback with androidx.biometric.
*
* The texts are in English inside the code; move them to strings.xml for your app.
* Only needs androidx.compose.material3.
*/
@Composable
fun LoginScreen(
onSignIn: (email: String, password: String) -> Unit,
modifier: Modifier = Modifier,
isLoading: Boolean = false,
errorMessage: String? = null,
onForgotPassword: (() -> Unit)? = null,
onBiometricSignIn: (() -> Unit)? = null,
) {
var email by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
var passwordVisible by rememberSaveable { mutableStateOf(false) }
var submitted by rememberSaveable { mutableStateOf(false) }
val focusManager = LocalFocusManager.current
val emailError = submitted && !isValidEmail(email)
val passwordError = submitted && password.length < MIN_PASSWORD_LENGTH
val submit: () -> Unit = {
submitted = true
if (isValidEmail(email) && password.length >= MIN_PASSWORD_LENGTH && !isLoading) {
focusManager.clearFocus()
onSignIn(email.trim(), password)
}
}
Column(此预览已截短。安装后可获得完整内容。
1.0.0 的变更
首个版本。