با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
هنگامی که کاربران با استفاده از یک قلم طراحی میکنند، مینویسند یا با یک برنامه تعامل میکنند، گاهی اوقات صفحه را با کف دست لمس میکنند. رویداد لمسی ممکن است قبل از اینکه سیستم رویداد را بهعنوان لمس تصادفی کف دست تشخیص دهد و رد کند، به برنامه شما گزارش شود.
لمس کف دست را شناسایی و نادیده بگیرید
برنامه شما باید رویدادهای لمسی خارجی را شناسایی کند و آنها را نادیده بگیرد. Android با ارسال یک شی MotionEvent به برنامه شما، لمس کف دست را لغو می کند.
اشیاء MotionEvent ارسال شده به برنامه شما را بررسی کنید. از API های MotionEvent برای تعیین ویژگی های رویداد (عملکردها و پرچم ها) استفاده کنید:
رویدادهای تک اشاره ای — ACTION_CANCEL را بررسی کنید. در Android نسخه 13 و بالاتر، FLAG_CANCELED را نیز بررسی کنید.
رویدادهای چند نشانگر — در Android نسخه 13 و بالاتر، ACTION_POINTER_UP و FLAG_CANCELED را بررسی کنید.
رویدادهای حرکتی که دارای ویژگیهای ACTION_CANCEL و ACTION_POINTER_UP / FLAG_CANCELED هستند را نادیده بگیرید.
valmyView=findViewById<View>(R.id.myView).apply{setOnTouchListener{view,event->// Process motion event.}}
جاوا
ViewmyView=findViewById(R.id.myView);myView.setOnTouchListener((view,event)->{// Process motion event.});
2. عمل رویداد و پرچم ها را تعیین کنید
ACTION_CANCEL را بررسی کنید، که نشاندهنده یک رویداد تک نقطهای در همه سطوح API است. در Android 13 و بالاتر، ACTION_POINTER_UP برای FLAG_CANCELED.
کاتلین
valmyView=findViewById<View>(R.id.myView).apply{setOnTouchListener{view,event->when(event.actionMasked){MotionEvent.ACTION_CANCEL->{//Process canceled single-pointer motion event for all SDK versions.}MotionEvent.ACTION_POINTER_UP->{if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU&&(event.flagsandMotionEvent.FLAG_CANCELED)==MotionEvent.FLAG_CANCELED){//Process canceled multi-pointer motion event for Android 13 and higher.}}}true}}
جاوا
ViewmyView=findViewById(R.id.myView);myView.setOnTouchListener((view,event)->{switch(event.getActionMasked()){caseMotionEvent.ACTION_CANCEL:// Process canceled single-pointer motion event for all SDK versions.caseMotionEvent.ACTION_UP:if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU&&(event.getFlags()&MotionEvent.FLAG_CANCELED)==MotionEvent.FLAG_CANCELED){//Process canceled multi-pointer motion event for Android 13 and higher.}}returntrue;});
3. ژست را لغو کنید
بعد از اینکه لمس کف دست را تشخیص دادید، میتوانید جلوههای ژست روی صفحه را لغو کنید.
ACTION_CANCEL : ثابت MotionEvent که نشان میدهد یک حرکت باید لغو شود.
ACTION_POINTER_UP : ثابت MotionEvent که نشان میدهد نشانگر دیگری غیر از اولین نشانگر بالا رفته است (یعنی تماس با صفحه دستگاه را قطع کرده است).
FLAG_CANCELED : ثابت MotionEvent که نشان میدهد بالا رفتن نشانگر باعث یک رویداد لمسی غیرعمدی شده است. به رویدادهای ACTION_POINTER_UP و ACTION_CANCEL در Android 13 (سطح API 33) و بالاتر اضافه شد.
نتایج
برنامه شما اکنون میتواند لمس کف دست را برای رویدادهای چند اشارهگر در Android 13 و سطوح API بالاتر و رویدادهای تک اشارهای در همه سطوح API شناسایی و رد کند.
مجموعه هایی که حاوی این راهنما هستند
این راهنما بخشی از مجموعههای راهنمای Quick Guide است که اهداف توسعه Android گستردهتری را پوشش میدهد:
بهینه سازی برای نمایشگرهای بزرگ
برنامه خود را فعال کنید تا از تجربه کاربری بهینه شده در رایانه لوحی، دستگاههای تاشو و دستگاههای ChromeOS پشتیبانی کند.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-02-06 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-02-06 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Reject stylus palm touches\n\n\u003cbr /\u003e\n\nWhen users draw, write, or interact with an app using a stylus, they sometimes\ntouch the screen with the palm of their hands. The touch event might be reported\nto your app before the system recognizes and dismisses the event as an\naccidental palm touch.\n\nIdentify and ignore palm touches\n--------------------------------\n\nYour app must identify extraneous touch events and ignore them. Android cancels\na palm touch by dispatching a [`MotionEvent`](/reference/kotlin/android/view/MotionEvent) object to your app.\n\n- Examine `MotionEvent` objects dispatched to your app. Use the `MotionEvent`\n APIs to determine event properties (actions and flags):\n\n - **Single-pointer events** --- Check for [`ACTION_CANCEL`](/reference/kotlin/android/view/MotionEvent#action_cancel). On Android 13 and higher, also check for [`FLAG_CANCELED`](/reference/kotlin/android/view/MotionEvent#flag_canceled).\n - **Multi-pointer events** --- On Android 13 and higher, check for [`ACTION_POINTER_UP`](/reference/kotlin/android/view/MotionEvent#action_pointer_up) and `FLAG_CANCELED`.\n- Ignore motion events that have the `ACTION_CANCEL` and\n `ACTION_POINTER_UP`/`FLAG_CANCELED` properties.\n\n| **Warning:** Android 12 (API level 32) and lower provide only `ACTION_POINTER_UP` for non-primary multi-pointer events. `FLAG_CANCELED` is not set for cancelable events such as palm touches. As a result, apps cannot determine whether the touch was intended or not on Android 12 and lower.\n\n### 1. Acquire motion event objects\n\nAdd an [`OnTouchListener`](/reference/kotlin/android/view/View.OnTouchListener) to your app: \n\n### Kotlin\n\n```kotlin\nval myView = findViewById\u003cView\u003e(R.id.myView).apply {\n setOnTouchListener { view, event -\u003e\n // Process motion event.\n }\n}\n```\n\n### Java\n\n```java\nView myView = findViewById(R.id.myView);\nmyView.setOnTouchListener( (view, event) -\u003e {\n // Process motion event.\n});\n```\n\n### 2. Determine the event action and flags\n\nCheck for `ACTION_CANCEL`, which indicates a single-pointer event on all API\nlevels. On Android 13 and higher, check `ACTION_POINTER_UP` for `FLAG_CANCELED.` \n\n### Kotlin\n\n```kotlin\nval myView = findViewById\u003cView\u003e(R.id.myView).apply {\n setOnTouchListener { view, event -\u003e\n when (event.actionMasked) {\n MotionEvent.ACTION_CANCEL -\u003e {\n //Process canceled single-pointer motion event for all SDK versions.\n }\n MotionEvent.ACTION_POINTER_UP -\u003e {\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.TIRAMISU &&\n (event.flags and MotionEvent.FLAG_CANCELED) == MotionEvent.FLAG_CANCELED) {\n //Process canceled multi-pointer motion event for Android 13 and higher.\n }\n }\n }\n true\n }\n}\n```\n\n### Java\n\n```java\nView myView = findViewById(R.id.myView);\nmyView.setOnTouchListener( (view, event) -\u003e {\n switch (event.getActionMasked()) {\n case MotionEvent.ACTION_CANCEL:\n // Process canceled single-pointer motion event for all SDK versions.\n case MotionEvent.ACTION_UP:\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.TIRAMISU &&\n (event.getFlags() & MotionEvent.FLAG_CANCELED) == MotionEvent.FLAG_CANCELED) {\n //Process canceled multi-pointer motion event for Android 13 and higher.\n }\n }\n return true;\n});\n```\n\n### 3. Undo the gesture\n\nAfter you've identified a palm touch, you can undo the onscreen effects of the\ngesture.\n\nYour app must keep a history of user actions so that unintended inputs such as\npalm touches can be undone. For an example of how to maintain history, see\n[Implement a basic drawing app](/codelabs/large-screens/advanced-stylus-support#2) in the [Enhance stylus support in an Android\napp](/codelabs/large-screens/advanced-stylus-support) codelab.\n\nKey points\n----------\n\n- [`MotionEvent`](/reference/kotlin/android/view/MotionEvent): Represents touch and movement events. Contains the information necessary to determine whether an event should be disregarded.\n- [`OnTouchListener#onTouch()`](/reference/kotlin/android/view/View.OnTouchListener#ontouch): Receives `MotionEvent` objects.\n- [`MotionEvent#getActionMasked()`](/reference/kotlin/android/view/MotionEvent#getactionmasked): Returns the action associated with a motion event.\n- [`ACTION_CANCEL`](/reference/kotlin/android/view/MotionEvent#action_cancel): `MotionEvent` constant that indicates a gesture should be undone.\n- [`ACTION_POINTER_UP`](/reference/kotlin/android/view/MotionEvent#action_pointer_up): `MotionEvent` constant that indicates a pointer other than the first pointer has gone up (that is, has relinquished contact with the device screen).\n- [`FLAG_CANCELED`](/reference/kotlin/android/view/MotionEvent#flag_canceled): `MotionEvent` constant that indicates that the pointer going up caused an unintentional touch event. Added to `ACTION_POINTER_UP` and `ACTION_CANCEL` events on Android 13 (API level 33) and higher.\n\nResults\n-------\n\nYour app can now identify and reject palm touches for multi-pointer events on\nAndroid 13 and higher API levels and for single-pointer events on all API\nlevels.\n\nCollections that contain this guide\n-----------------------------------\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\n### Optimize for large screens\n\nEnable your app to support an optimized user experience on tablets, foldables, and ChromeOS devices. \n[Quick guide collection](/quick-guides/collections/optimize-for-large-screens) \n\nHave questions or feedback\n--------------------------\n\nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]