TextFieldBuffer


A text buffer that can be edited, similar to StringBuilder.

This class provides methods for changing the text, such as:

This class also stores and tracks the cursor position or selection range. The cursor position is just a selection range with zero length. The cursor and selection can be changed using methods such as:

To get one of these, and for usage samples, see TextFieldState.edit. Every change to the buffer is tracked in a ChangeList which you can access via the changes property.

Summary

Nested types

The ordered list of non-overlapping and discontinuous changes performed on a TextFieldBuffer during the current edit or filter operation.

Public functions

open Appendable
append(char: Char)
Cmn
open Appendable
Cmn
open Appendable
append(text: CharSequence?, start: Int, end: Int)
Cmn
CharSequence

Returns a CharSequence backed by this buffer.

Cmn
Char
charAt(index: Int)

Returns the Char at index in this buffer.

Cmn
Unit

Places the cursor after the character at the given index.

Cmn
Unit

Places the cursor before the character at the given index.

Cmn
Unit
replace(start: Int, end: Int, text: CharSequence)

Replaces the text between start (inclusive) and end (exclusive) in this value with text, and records the change in changes.

Cmn
Unit

Revert all changes made to this value since it was created.

Cmn
open String
Cmn

Public properties

TextFieldBuffer.ChangeList

The ChangeList represents the changes made to this value and is inherently mutable.

Cmn
Boolean

True if the selection range has non-zero length.

Cmn
Int

The number of characters in the text field.

Cmn
TextRange

Original selection before the changes.

Cmn
CharSequence

Original text content of the buffer before any changes were applied.

Cmn
TextRange

The selected range of characters.

Cmn

Extension functions

Unit
TextFieldBuffer.delete(start: Int, end: Int)

Delete the text between start (inclusive) and end (exclusive).

Cmn
Unit
TextFieldBuffer.insert(index: Int, text: String)

Insert text at the given index in this value.

Cmn
Unit

Places the cursor at the end of the text.

Cmn
Unit

Places the selection around all the text.

Cmn

Public functions

append

open fun append(char: Char): Appendable

append

open fun append(text: CharSequence?): Appendable

append

open fun append(text: CharSequence?, start: Int, end: Int): Appendable

asCharSequence

fun asCharSequence(): CharSequence

Returns a CharSequence backed by this buffer. Any subsequent changes to this buffer will be visible in the returned sequence as well.

charAt

fun charAt(index: Int): Char

Returns the Char at index in this buffer.

placeCursorAfterCharAt

fun placeCursorAfterCharAt(index: Int): Unit

Places the cursor after the character at the given index.

If index is inside a surrogate pair or other invalid run, the cursor will be placed at the nearest later index.

To place the cursor at the end of the field, after the last character, pass index TextFieldBuffer.length or call placeCursorAtEnd.

Parameters
index: Int

Character index to place cursor after, should be in range 0 (inclusive) to TextFieldBuffer.length (exclusive).

placeCursorBeforeCharAt

fun placeCursorBeforeCharAt(index: Int): Unit

Places the cursor before the character at the given index.

If index is inside a surrogate pair or other invalid run, the cursor will be placed at the nearest earlier index.

To place the cursor at the beginning of the field, pass index 0. To place the cursor at the end of the field, after the last character, pass index TextFieldBuffer.length or call placeCursorAtEnd.

Parameters
index: Int

Character index to place cursor before, should be in range 0 to TextFieldBuffer.length, inclusive.

replace

fun replace(start: Int, end: Int, text: CharSequence): Unit

Replaces the text between start (inclusive) and end (exclusive) in this value with text, and records the change in changes.

Parameters
start: Int

The character offset of the first character to replace.

end: Int

The character offset of the first character after the text to replace.

text: CharSequence

The text to replace the range [start, end) with.

See also
append
insert
delete

revertAllChanges

fun revertAllChanges(): Unit

Revert all changes made to this value since it was created.

After calling this method, this object will be in the same state it was when it was initially created, and changes will be empty.

toString

open fun toString(): String

Public properties

changes

@ExperimentalFoundationApi
val changesTextFieldBuffer.ChangeList

The ChangeList represents the changes made to this value and is inherently mutable. This means that the returned ChangeList always reflects the complete list of changes made to this value at any given time, even those made after reading this property.

import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.forEachChange
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.text.substring

// Print a log message every time the text is changed.
BasicTextField(
    state = rememberTextFieldState(),
    inputTransformation = {
        changes.forEachChange { sourceRange, replacedLength ->
            val newString = asCharSequence().substring(sourceRange)
            println("""$replacedLength characters were replaced with "$newString"""")
        }
    }
)
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.delete
import androidx.compose.foundation.text.input.forEachChange
import androidx.compose.foundation.text.input.forEachChangeReversed
import androidx.compose.foundation.text.input.insert
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.Text
import androidx.compose.runtime.remember

// Make a text field behave in "insert mode" – inserted text overwrites the text ahead of it
// instead of being inserted.
BasicTextField(
    state = rememberTextFieldState(),
    inputTransformation = {
        changes.forEachChangeReversed { range, originalRange ->
            if (!range.collapsed && originalRange.collapsed) {
                // New text was inserted, delete the text ahead of it.
                delete(
                    range.end.coerceAtMost(length),
                    (range.end + range.length).coerceAtMost(length)
                )
            }
        }
    }
)

hasSelection

val hasSelectionBoolean

True if the selection range has non-zero length. If this is false, then the selection represents the cursor.

See also
selection

length

val lengthInt

The number of characters in the text field.

originalSelection

val originalSelectionTextRange

Original selection before the changes. Calling revertAllChanges will set the selection to this value.

originalText

val originalTextCharSequence

Original text content of the buffer before any changes were applied. Calling revertAllChanges will set the contents of this buffer to this value.

selection

var selectionTextRange

The selected range of characters.

Places the selection around the given range in characters.

If the start or end of TextRange fall inside surrogate pairs or other invalid runs, the values will be adjusted to the nearest earlier and later characters, respectively.

To place the start of the selection at the beginning of the field, set this value to TextRange.Zero. To place the end of the selection at the end of the field, after the last character, pass TextFieldBuffer.length. Passing a zero-length range is the same as calling placeCursorBeforeCharAt.

Extension functions

fun TextFieldBuffer.delete(start: Int, end: Int): Unit

Delete the text between start (inclusive) and end (exclusive). Pass 0 as start and TextFieldBuffer.length as end to delete everything in this buffer.

Parameters
start: Int

The character offset of the first character to delete.

end: Int

The character offset of the first character after the deleted range.

See also
replace
append
insert
fun TextFieldBuffer.insert(index: Int, text: String): Unit

Insert text at the given index in this value. Pass 0 to insert text at the beginning of this buffer, and pass TextFieldBuffer.length to insert text at the end of this buffer.

This is equivalent to calling replace(index, index, text).

Parameters
index: Int

The character offset at which to insert text.

text: String

The text to insert.

See also
replace
append
delete

placeCursorAtEnd

fun TextFieldBuffer.placeCursorAtEnd(): Unit

Places the cursor at the end of the text.

fun TextFieldBuffer.selectAll(): Unit

Places the selection around all the text.