预览
Composable
import androidx. compose. foundation. background
import androidx. compose. foundation. layout. Column
import androidx. compose. foundation. layout. Row
import androidx. compose. foundation. layout. fillMaxWidth
import androidx. compose. foundation. layout. padding
import androidx. compose. foundation. shape. RoundedCornerShape
import androidx. compose. material. Button
import androidx. compose. material. ButtonDefaults
import androidx. compose. material3. BasicAlertDialog
import androidx. compose. material3. ExperimentalMaterial3Api
import androidx. compose. material3. Text
import androidx. compose. material3. TimePicker
import androidx. compose. material3. TimePickerLayoutType
import androidx. compose. material3. rememberTimePickerState
import androidx. compose. runtime. Composable
import androidx. compose. ui. Alignment
import androidx. compose. ui. Modifier
import androidx. compose. ui. graphics. Color
import androidx. compose. ui. tooling. preview. Preview
import androidx. compose. ui. unit. TextUnit
import androidx. compose. ui. unit. TextUnitType
import androidx. compose. ui. unit. dp
import java. util. Calendar @OptIn ( ExperimentalMaterial3Api :: class )
@Composable
@Preview
fun TimePickerExample ( ) { val currentTime = Calendar . getInstance ( ) val state = rememberTimePickerState ( initialHour = currentTime. get ( Calendar . HOUR_OF_DAY ) , initialMinute = currentTime. get ( Calendar . MINUTE ) , is24Hour = true , ) val hour = state. hourval minute = state. minuteval selectedValue = "$hour:$minute" Text ( selectedValue, modifier = Modifier , fontSize = TextUnit ( 18f , TextUnitType. Sp ) ) BasicAlertDialog ( modifier = Modifier , onDismissRequest = { } , content = { Column ( modifier = Modifier . background ( color = Color. White , shape = RoundedCornerShape ( 8.d p) ) . padding ( start = 10.d p, end = 10.d p, top = 5.d p) ) { Row ( verticalAlignment = Alignment. CenterVertically , modifier = Modifier . padding ( bottom = 5.d p, start = 20.d p, end = 20.d p) ) { Text ( "选择时间" , modifier = Modifier . weight ( 1f ) , fontSize = TextUnit ( 18f , TextUnitType. Sp ) ) Button ( onClick = { state. hour} , shape = RoundedCornerShape ( 90.d p) , colors = ButtonDefaults . buttonColors ( backgroundColor = Color ( 0xff6750A4 ) ) ) { Text ( "确认" , color = Color. White ) } } TimePicker ( state, modifier = Modifier . fillMaxWidth ( ) , layoutType = TimePickerLayoutType. Vertical ) } } , )
}