Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import androidx.compose.material.TextField
//noinspection UsingMaterialAndMaterial3Libraries
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
Expand Down Expand Up @@ -144,21 +143,16 @@ fun TextFieldInitialState() {
// [END android_compose_state_text_7]
}

@Preview(showBackground = true)
@Composable
fun TextFieldBuffer() {
// [START android_compose_state_text_8]
val phoneNumberState = rememberTextFieldState()

LaunchedEffect(phoneNumberState) {
phoneNumberState.edit { // TextFieldBuffer scope
append("123456789")
}
}
val phoneNumberState = rememberTextFieldState("123456789")

TextField(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest two changes to this sample: Specify keyboard options (since it will be copy/paste) and add maxLength

TextField(
    state = phoneNumberState,
    keyboardOptions = KeyboardOptions(
        keyboardType = KeyboardType.Phone
    ),
    inputTransformation = InputTransformation.maxLength(10).then {
        if (asCharSequence().isDigitsOnly()) {
            revertAllChanges()
        }
    },
    outputTransformation = OutputTransformation {
        if (length > 0) insert(0, "(")
        if (length > 4) insert(4, ")")
        if (length > 8) insert(8, "-")
    }
)

state = phoneNumberState,
inputTransformation = InputTransformation { // TextFieldBuffer scope
if (asCharSequence().isDigitsOnly()) {
if (!asCharSequence().isDigitsOnly()) {
revertAllChanges()
}
},
Expand All @@ -174,32 +168,37 @@ fun TextFieldBuffer() {
@Preview
@Composable
fun EditTextFieldState() {
// [START android_compose_state_text_9]
val usernameState = rememberTextFieldState("I love Android")
editTFState(usernameState)
}

fun editTFState(textFieldState: TextFieldState) {
// [START android_compose_state_text_9]
// Initial textFieldState text passed in is "I love Android"
// textFieldState.text : I love Android
// textFieldState.selection: TextRange(14, 14)
usernameState.edit { insert(14, "!") }
textFieldState.edit { insert(14, "!") }
// textFieldState.text : I love Android!
// textFieldState.selection: TextRange(15, 15)
usernameState.edit { replace(7, 14, "Compose") }
textFieldState.edit { replace(7, 14, "Compose") }
// textFieldState.text : I love Compose!
// textFieldState.selection: TextRange(15, 15)
usernameState.edit { append("!!!") }
textFieldState.edit { append("!!!") }
// textFieldState.text : I love Compose!!!!
// textFieldState.selection: TextRange(18, 18)
usernameState.edit { selectAll() }
textFieldState.edit { selectAll() }
// textFieldState.text : I love Compose!!!!
// textFieldState.selection: TextRange(0, 18)
// [END android_compose_state_text_9]

// [START android_compose_state_text_10]
usernameState.setTextAndPlaceCursorAtEnd("I really love Android")
textFieldState.setTextAndPlaceCursorAtEnd("I really love Android")
// textFieldState.text : I really love Android
// textFieldState.selection : TextRange(21, 21)
// [END android_compose_state_text_10]

// [START android_compose_state_text_11]
usernameState.clearText()
textFieldState.clearText()
// textFieldState.text :
// textFieldState.selection : TextRange(0, 0)
// [END android_compose_state_text_11]
Expand Down