sitelink1 https://aroundck.tistory.com/8193 
sitelink2  
sitelink3  

Android View 시스템의 EditText 는 Compose 의 'TextField' 에 매칭된다.

 

#

TextField 의 function signature 는 아래와 같다.

fun TextField(
    value: TextFieldValue,
    onValueChange: (TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    label: @Composable (() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    isError: Boolean = false,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions(),
    singleLine: Boolean = false,
    maxLines: Int = Int.MAX_VALUE,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    shape: Shape =
        MaterialTheme.shapes.small.copy(bottomEnd = ZeroCornerSize, bottomStart = ZeroCornerSize),
    colors: TextFieldColors = TextFieldDefaults.textFieldColors()
)

 

#

TextField 의 일반적 사용 예시는 아래와 같다.

var text by remember { mutableStateOf(TextFieldValue("")) }
TextField(
	value = text,
	onValueChange = { newText -> text = newText },
	modifier = Modifier.fillMaxWidth(),
	placeholder = { Text(stringResource(R.string.hint)) },
	singleLine = true,
)

EditText  에 비해 해줘야 하는 일이 많은데, value 영역에 TextFieldValue 를 넣어줘야 하는 것 때문이다.

그냥 TextField 만 그려놓으면, 키보드 입력을 해도 그 값이 변경되지 않는다.

 

#

TextField 에 hint text 를 설정하려면 placeHolder 를 사용하면 된다.

단, placeHolder 는 string 을 받는 것이 아니라 composable 을 받기에.. 좀 더 자유롭게 hint 를 구성할 수 있다.

TextField(
	...
	placeholder = { Text(stringResource(R.string.hint)) },
)

 

#

TextField 에 single line 을 지정하려면 singleLine 을 사용하면 된다.

TextField(
	...
	singleLine = true,
)

 

#

TextField 에 imeOption 을 지정하려면 keyboardOptions 의 keyboardType 을 사용한다.

TextField(
	...
	keyboardOptions = KeyboardOptiopns(keyboardType = KeyboardType.Number),
)

KeyboardType 에는 Text, Ascii, Number, Phone, Uri, Email, Password, NumberPassword 가 있다.

 

#

TextField 에 imeAction 을 지정하려면 keyboardOptions 의 imeAction 과 keyboardActions 을 사용한다.

여기서는 focus 를 관리하는 방법도 함께 본다.

val focusManager = FocusManager.current
TextField(
	...
	keyboardOptions = KeyboardOptiopns(
    		keyboardType = KeyboardType.Number, 
        	imeAction = ImeAction.Next,
    ),
    keyboardActions = KeyboardActions(
    	onNext = { focusManager.moveFocus(FocusDirection.Down) },
        onDone = { focusManager.clearFocus() },
    )
)

ImeAction 의 const 로는 Default, None, Go, Search, Send, Previous, Next, Done 이 있다.

 

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 2
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 1
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 3
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 0
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 1
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 0
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 1
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 0
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 5
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 2
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 1
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 0
» [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 0
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 0
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 1
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 0
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 0
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 1
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 0
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 1