सेगमेंट वाले बटन का इस्तेमाल करके, उपयोगकर्ताओं को विकल्पों के सेट में से चुनने की सुविधा दें. सेगमेंट किए गए बटन दो तरह के होते हैं:
- एक विकल्प चुनने वाला बटन: इससे उपयोगकर्ता एक विकल्प चुन सकते हैं.
- एक से ज़्यादा विकल्प चुनने की सुविधा देने वाला बटन: इससे उपयोगकर्ता, दो से पांच आइटम चुन सकते हैं. ज़्यादा जटिल विकल्पों या पांच से ज़्यादा आइटम के लिए, चिप्स का इस्तेमाल करें.
एपीआई सरफ़ेस
सेगमेंट किए गए बटन बनाने के लिए, SingleChoiceSegmentedButtonRow और MultiChoiceSegmentedButtonRow लेआउट का इस्तेमाल करें. ये लेआउट, SegmentedButtons की पोज़िशन और साइज़ को सही तरीके से सेट करते हैं. साथ ही, इनमें ये मुख्य पैरामीटर एक जैसे होते हैं:
space: इससे बटन के बीच ओवरलैप को अडजस्ट किया जाता है.content: इसमें सेगमेंट वाले बटन की लाइन का कॉन्टेंट शामिल होता है. आम तौर पर, यहSegmentedButtonका क्रम होता है.
एक विकल्प चुनने की सुविधा वाला सेगमेंटेड बटन बनाना
इस उदाहरण में, एक विकल्प चुनने की सुविधा देने वाला सेगमेंटेड बटन बनाने का तरीका बताया गया है:
@Composable fun SingleChoiceSegmentedButton(modifier: Modifier = Modifier) { var selectedIndex by remember { mutableIntStateOf(0) } val options = listOf("Day", "Month", "Week") SingleChoiceSegmentedButtonRow { options.forEachIndexed { index, label -> SegmentedButton( shape = SegmentedButtonDefaults.itemShape( index = index, count = options.size ), onClick = { selectedIndex = index }, selected = index == selectedIndex, label = { Text(label) } ) } } }
कोड के बारे में अहम बातें
- चुने गए बटन इंडेक्स को ट्रैक करने के लिए,
rememberऔरmutableIntStateOfका इस्तेमाल करके,selectedIndexवैरिएबल को शुरू करता है. - यह
optionsकी सूची तय करता है, जो बटन के लेबल दिखाती है. SingleChoiceSegmentedButtonRowमें सिर्फ़ एक बटन चुना जा सकता है.forEachIndexedलूप के अंदर, हर विकल्प के लिए एकSegmentedButtonबनाता है:shape,SegmentedButtonDefaults.itemShapeका इस्तेमाल करके, बटन के इंडेक्स और विकल्पों की कुल संख्या के आधार पर बटन का आकार तय करता है.onClick, क्लिक किए गए बटन के इंडेक्स के साथselectedIndexको अपडेट करता है.selected,selectedIndexके आधार पर बटन के चुने जाने की स्थिति सेट करता है.label,Textकंपोज़ेबल का इस्तेमाल करके बटन का लेबल दिखाता है.
नतीजा
एक से ज़्यादा विकल्प चुनने की सुविधा देने वाला सेगमेंटेड बटन बनाना
इस उदाहरण में, आइकॉन के साथ कई विकल्पों वाला सेगमेंटेड बटन बनाने का तरीका बताया गया है. इससे उपयोगकर्ता कई विकल्प चुन सकते हैं:
@Composable fun MultiChoiceSegmentedButton(modifier: Modifier = Modifier) { val selectedOptions = remember { mutableStateListOf(false, false, false) } val options = listOf("Walk", "Ride", "Drive") MultiChoiceSegmentedButtonRow { options.forEachIndexed { index, label -> SegmentedButton( shape = SegmentedButtonDefaults.itemShape( index = index, count = options.size ), checked = selectedOptions[index], onCheckedChange = { selectedOptions[index] = !selectedOptions[index] }, icon = { SegmentedButtonDefaults.Icon(selectedOptions[index]) }, label = { when (label) { "Walk" -> Icon( painter = rememberVectorPainter(AppIcons.DirectionsWalk), contentDescription = "Directions Walk" ) "Ride" -> Icon( painter = rememberVectorPainter(AppIcons.DirectionsBus), contentDescription = "Directions Bus" ) "Drive" -> Icon( painter = rememberVectorPainter(AppIcons.DirectionsCar), contentDescription = "Directions Car" ) } } ) } } }
कोड के बारे में अहम बातें
- कोड,
rememberऔरmutableStateListOfका इस्तेमाल करके,selectedOptionsवैरिएबल को शुरू करता है. इससे हर बटन की चुनी गई स्थिति को ट्रैक किया जाता है. - कोड में बटन शामिल करने के लिए,
MultiChoiceSegmentedButtonRowका इस्तेमाल किया जाता है. forEachIndexedलूप के अंदर, कोड हर विकल्प के लिए एकSegmentedButtonबनाता है:shape, बटन के इंडेक्स और विकल्पों की कुल संख्या के आधार पर, बटन का आकार तय करता है.checked,selectedOptionsमें मौजूद वैल्यू के आधार पर, बटन की 'चुनी गई' स्थिति सेट करता है.- बटन पर क्लिक करने पर,
onCheckedChangeselectedOptionsमें मौजूद आइटम की चुनी गई स्थिति को टॉगल करता है. icon,SegmentedButtonDefaults.Iconऔर बटन की चुनी गई स्थिति के आधार पर एक आइकॉन दिखाता है.label, लेबल के हिसाब से आइकॉन दिखाता है. इसके लिए, वह सही इमेज वेक्टर और कॉन्टेंट के ब्यौरे के साथIconकंपोज़ेबल का इस्तेमाल करता है.
नतीजा
अन्य संसाधन
- Material 3: सेगमेंट किए गए बटन