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 단말기 고유값 구하는 방법들 황제낙엽 2019.03.03 11884
127 저장소 파일 불러올 때 권한 요청 설정 file 황제낙엽 2018.08.21 2925
126 icudtl.dat (Microsoft Office Access 2010 14를 위해 Microsoft가 생성한 Dynamic Link Library) 황제낙엽 2021.07.07 2110
125 Emulator: audio: Failed to create voice `adc' 황제낙엽 2018.08.06 1888
124 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1825
123 고유 식별자의 모범 사례 (Android Developers) 황제낙엽 2019.03.03 1247
122 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-06 file 황제낙엽 2018.08.19 1132
121 HTTP 프로토콜을 이용한 Json Post 보내고 받기 file 황제낙엽 2017.08.03 926
120 Error:Execution failed for task ':app:lintVitalRelease'. 황제낙엽 2018.01.29 921
119 파일 입출력(내장 메모리, 외장메모리) 황제낙엽 2018.08.19 853
118 HttpURLConnection 에서 세션 유지하기(쿠키 사용) 황제낙엽 2017.08.03 785
117 STT 학습 링크 모음 (sample link) 황제낙엽 2018.10.11 774
116 [성공샘플] HttpURLConnection 을 이용하여 JSON 데이터 보내기 예제 황제낙엽 2018.11.10 759
115 Image to byte Array (바로 사용가능한 JPEG 파일) 황제낙엽 2018.07.24 757
114 ABI 관리 황제낙엽 2017.03.28 638
113 TTS 를 위한 스마트폰 설정 및 TTS 샘플 file 황제낙엽 2019.02.16 591
112 안드로이드 스튜디오(Android Studio) 최적화 file 황제낙엽 2018.02.07 568
111 android.webkit.CookieManager 를 이용한 웹뷰와의 세션 공유 황제낙엽 2019.04.26 496
110 동적 레이아웃 생성과 자동 줄바꿈 구현 file 황제낙엽 2018.12.26 441
109 안드로이드 기기 식별 방법 - UUID(Universally unique identifier) 황제낙엽 2019.03.03 404