ফোকাস প্রতিক্রিয়া
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
সহজ ফোকাস ভিজ্যুয়ালাইজেশনের জন্য চাক্ষুষ সংকেত প্রদান করুন
যদিও মেটেরিয়াল থিমের সমস্ত ফোকাসযোগ্য উপাদানগুলির ইতিমধ্যেই একটি ফোকাস স্টাইল রয়েছে যা থিমের সাথে মেলে, আপনাকে ফোকাস করা উপাদানটিকে সহজে চিহ্নিত করতে কিছু ভিজ্যুয়াল উপাদান যুক্ত করতে হতে পারে৷ একটি ভাল সমাধান হল আপনার উপাদানের সীমানা এমন একটি রঙের সাথে পরিবর্তন করা যা ব্যাকগ্রাউন্ডের সাথে একটি ভাল বৈসাদৃশ্য রয়েছে:
var color by remember { mutableStateOf(Color.White) }
Card(
modifier = Modifier
.onFocusChanged {
color = if (it.isFocused) Red else White
}
.border(5.dp, color)
) {}
এই উদাহরণে, recompositions জুড়ে সীমানার রঙ সংরক্ষণ করতে remember
ব্যবহার করা হয়, এবং উপাদানটির রূপরেখা প্রতিবার আপডেট করা হয় যখন উপাদানটি ফোকাস লাভ করে বা হারায়।
উন্নত চাক্ষুষ সংকেত প্রয়োগ করুন
জেটপ্যাক কম্পোজের মাধ্যমে, আপনি আরও পরিশীলিত এবং উন্নত ভিজ্যুয়াল সংকেত তৈরি করতে পারেন যা আপনার UI এর সাথে আরও ভাল মেলে।
- প্রথমে, একটি
IndicationInstance
তৈরি করুন যা দৃশ্যত আপনার UI-তে আপনার কাঙ্খিত কিউ আঁকে: private class MyHighlightIndicationNode(private val interactionSource: InteractionSource) :
Modifier.Node(), DrawModifierNode {
private var isFocused = false
override fun onAttach() {
coroutineScope.launch {
var focusCount = 0
interactionSource.interactions.collect { interaction ->
when (interaction) {
is FocusInteraction.Focus -> focusCount++
is FocusInteraction.Unfocus -> focusCount--
}
val focused = focusCount > 0
if (isFocused != focused) {
isFocused = focused
invalidateDraw()
}
}
}
}
override fun ContentDrawScope.draw() {
drawContent()
if (isFocused) {
drawRect(size = size, color = Color.White, alpha = 0.2f)
}
}
}
- এরপরে, একটি
Indication
তৈরি করুন এবং ফোকাসড অবস্থা মনে রাখুন: object MyHighlightIndication : IndicationNodeFactory {
override fun create(interactionSource: InteractionSource): DelegatableNode {
return MyHighlightIndicationNode(interactionSource)
}
override fun hashCode(): Int = -1
override fun equals(other: Any?) = other === this
}
indication()
সংশোধকের মাধ্যমে UI-তে Indication
এবং একটি InteractionSource
উভয়ই যোগ করুন: var interactionSource = remember { MutableInteractionSource() }
Card(
modifier = Modifier
.clickable(
interactionSource = interactionSource,
indication = MyHighlightIndication,
enabled = true,
onClick = { }
)
) {
Text("hello")
}
ফোকাসের অবস্থা বুঝুন
সাধারণত, যখনই ফোকাসের অবস্থা পরিবর্তিত হয়, একটি FocusEvent
গাছ থেকে উড়িয়ে দেওয়া হয় এবং একটি focusable()
মডিফায়ারের পিতামাতারা onFocusChanged()
মডিফায়ার ব্যবহার করে এটি শুনতে পারেন।
আপনার যদি ফোকাসের অবস্থা জানতে হয়, তাহলে আপনি onFocusChanged
সংশোধকের সাথে এই APIগুলি ব্যবহার করতে পারেন:
- যদি সংশোধকটির সাথে সংশোধকটি সংযুক্ত থাকে তা ফোকাস করা হলে
isFocused
true
দেখায় -
hasFocus
isFocused
এর মতো একইভাবে কাজ করে, কিন্তু একটি উল্লেখযোগ্য পার্থক্যের সাথে: শুধুমাত্র বর্তমান পরীক্ষা করার পরিবর্তে, এটি উপাদান বা তার সন্তানদের মধ্যে একটি ফোকাস করা হয়েছে কিনা তা পরীক্ষা করে - যখনই ফোকাস রাখা হয় তখনই
isCaptured
করা true
হয়। এটি ঘটে, উদাহরণস্বরূপ, যখন একটি TextField
ভুল ডেটা থাকে, যাতে অন্য উপাদানগুলিকে ফোকাস করার চেষ্টা করা ফোকাসটি পরিষ্কার করে না।
এই ক্ষেত্রগুলি নীচে দেখানো হয়েছে:
Modifier.onFocusChanged {
val isFocused = it.isFocused
val hasFocus = it.hasFocus
val isCaptured= it.isCaptured
}
{% শব্দার্থে %}
{% endverbatim %} আপনার জন্য প্রস্তাবিত
{% শব্দার্থে %} {% endverbatim %}
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-08-23 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-08-23 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["# React to focus\n\nProvide visual cues for easier focus visualization\n--------------------------------------------------\n\nWhile all the focusable elements from Material Theme already have a focus style\nthat matches the theme, you might need to add some visual elements to make the\nfocused element easier to spot. A good solution would be to change the border of\nyour element with a color that has a good contrast with the background:\n\n\n```kotlin\nvar color by remember { mutableStateOf(Color.White) }\nCard(\n modifier = Modifier\n .onFocusChanged {\n color = if (it.isFocused) Red else White\n }\n .border(5.dp, color)\n) {}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt#L427-L434\n```\n\n\u003cbr /\u003e\n\nIn this example, `remember` is used to store the color of the border across\nrecompositions, and the outline of the element is updated every time the element\ngains or loses focus.\n\n### Implement advanced visual cues\n\nWith Jetpack Compose, you can also create more sophisticated and advanced visual\ncues that match better with your UI.\n\n1. First, create an `IndicationInstance` that visually draws the cue you want in your UI: \n\n ```kotlin\n private class MyHighlightIndicationNode(private val interactionSource: InteractionSource) :\n Modifier.Node(), DrawModifierNode {\n private var isFocused = false\n\n override fun onAttach() {\n coroutineScope.launch {\n var focusCount = 0\n interactionSource.interactions.collect { interaction -\u003e\n when (interaction) {\n is FocusInteraction.Focus -\u003e focusCount++\n is FocusInteraction.Unfocus -\u003e focusCount--\n }\n val focused = focusCount \u003e 0\n if (isFocused != focused) {\n isFocused = focused\n invalidateDraw()\n }\n }\n }\n }\n\n override fun ContentDrawScope.draw() {\n drawContent()\n if (isFocused) {\n drawRect(size = size, color = Color.White, alpha = 0.2f)\n }\n }\n }\n https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt#L439-L467\n ```\n2. Next, create an `Indication` and remember the focused state: \n\n ```kotlin\n object MyHighlightIndication : IndicationNodeFactory {\n override fun create(interactionSource: InteractionSource): DelegatableNode {\n return MyHighlightIndicationNode(interactionSource)\n }\n\n override fun hashCode(): Int = -1\n\n override fun equals(other: Any?) = other === this\n }https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt#L471-L479\n ```\n3. Add both the `Indication` and an `InteractionSource` to the UI, via the `indication()` modifier: \n\n ```kotlin\n var interactionSource = remember { MutableInteractionSource() }\n\n Card(\n modifier = Modifier\n .clickable(\n interactionSource = interactionSource,\n indication = MyHighlightIndication,\n enabled = true,\n onClick = { }\n )\n ) {\n Text(\"hello\")\n }https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt#L485-L497\n ```\n\nUnderstand the state of the focus\n---------------------------------\n\nGenerally, every time a state of the focus changes, a `FocusEvent` is fired up\nthe tree, and the parents of a `focusable()` modifier can listen to it using the\n`onFocusChanged()` modifier.\n\nIf you need to know the state of the focus,you can use these APIs in conjunction\nwith the `onFocusChanged` modifier:\n\n- `isFocused` returns `true` if the composable to which the modifier is attached is focused\n- `hasFocus` works similarly to `isFocused`, but with a substantial difference: rather than checking only the current, it checks if the element or one of its children is focused\n- `isCaptured` returns `true` whenever the focus is held. This happens, for instance, when a `TextField` contains incorrect data, so that trying to focus other elements will not clear the focus.\n\nThese fields are shown below: \n\n Modifier.onFocusChanged {\n val isFocused = it.isFocused\n val hasFocus = it.hasFocus\n val isCaptured= it.isCaptured\n }\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Change focus behavior](/develop/ui/compose/touch-input/focus/change-focus-behavior)\n- [Material Design 2 in Compose](/develop/ui/compose/designsystems/material)\n- [Handle user input](/develop/ui/compose/text/user-input)"]]