Jetpack Compose, Text
में इंटरैक्टिविटी की सुविधा देता है. लेख चयन है
अब यह ज़्यादा सुविधाजनक है. साथ ही, इसे कंपोज़ेबल लेआउट में इस्तेमाल किया जा सकता है. उपयोगकर्ता के इंटरैक्शन
टेक्स्ट में, कंपोज़ेबल लेआउट से अलग होते हैं, क्योंकि इसमें मॉडिफ़ायर नहीं जोड़ा जा सकता
Text
कंपोज़ेबल के किसी हिस्से में. इस पेज पर एपीआई के बारे में जानकारी दी गई है
जिनसे उपयोगकर्ता के इंटरैक्शन चालू रहते हैं.
टेक्स्ट चुनें
डिफ़ॉल्ट रूप से, कंपोज़ेबल को नहीं चुना जा सकता. इसका मतलब है कि लोग
अपने ऐप्लिकेशन से टेक्स्ट चुनें और उसे कॉपी करें. टेक्स्ट चुनने की सुविधा चालू करने के लिए, रैप करें
SelectionContainer
कंपोज़ेबल में आपके टेक्स्ट एलिमेंट:
@Composable fun SelectableText() { SelectionContainer { Text("This text is selectable") } }
ऐसा हो सकता है कि आप चुने जा सकने वाले हिस्से के खास हिस्सों को चुनने की सुविधा बंद करना चाहें. ऐसा करें
इसलिए, चुने नहीं जा सकने वाले हिस्से को DisableSelection
के साथ रैप करें
कंपोज़ेबल:
@Composable fun PartiallySelectableText() { SelectionContainer { Column { Text("This text is selectable") Text("This one too") Text("This one as well") DisableSelection { Text("But not this one") Text("Neither this one") } Text("But again, you can select this one") Text("And this one too") } } }
टेक्स्ट पर क्लिक की स्थिति जानना
Text
पर क्लिक सुनने के लिए, clickable
जोड़ें
कार्रवाई बदलने वाली कुंजी. हालांकि, अगर आपको किसी क्लिक की पोज़िशन की
अलग-अलग कार्रवाइयां इस मामले में Text
कंपोज़ेबल.
टेक्स्ट के अलग-अलग हिस्सों को शामिल किया है, तो आपको ClickableText
का इस्तेमाल करना होगा
इसके बजाय:
@Composable fun SimpleClickableText() { ClickableText(text = AnnotatedString("Click Me"), onClick = { offset -> Log.d("ClickableText", "$offset -th character is clicked.") }) }
एनोटेशन के साथ क्लिक करें
जब कोई उपयोगकर्ता, Text
कंपोज़ेबल पर क्लिक करता है, तो हो सकता है कि आपको अतिरिक्त अटैचमेंट अटैच करना पड़े
Text
वैल्यू के किसी हिस्से में जानकारी, जैसे कि किसी खास वैल्यू से जुड़ा यूआरएल
उदाहरण के लिए, किसी ब्राउज़र में खोलने के लिए शब्द. ऐसा करने के लिए, आपको
एनोटेशन, जो एक टैग (String
), एक आइटम (String
), और एक टेक्स्ट रेंज लेता है
पैरामीटर के तौर पर. किसी AnnotatedString
से, इन एनोटेशन को इसके साथ फ़िल्टर किया जा सकता है
अपने टैग या टेक्स्ट रेंज. यहां एक उदाहरण दिया गया है:
@Composable fun AnnotatedClickableText() { val annotatedText = buildAnnotatedString { append("Click ") // We attach this *URL* annotation to the following content // until `pop()` is called pushStringAnnotation( tag = "URL", annotation = "https://developer.android.com" ) withStyle( style = SpanStyle( color = Color.Blue, fontWeight = FontWeight.Bold ) ) { append("here") } pop() } ClickableText(text = annotatedText, onClick = { offset -> // We check if there is an *URL* annotation attached to the text // at the clicked position annotatedText.getStringAnnotations( tag = "URL", start = offset, end = offset ).firstOrNull()?.let { annotation -> // If yes, we log its value Log.d("Clicked URL", annotation.item) } }) }
आपके लिए सुझाव
- ध्यान दें: JavaScript बंद होने पर लिंक टेक्स्ट दिखता है
- कंपोज़ में सेमैंटिक्स
- Compose में सुलभता
- Compose में मटीरियल डिज़ाइन 2