From cd5eb0b290dc675b72e11d343ffa33b10f943aba Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Thu, 1 Dec 2016 12:35:10 +0200 Subject: [PATCH] Android button active state fixed to work after app suspend and resume --- tns-core-modules/ui/button/button.android.ts | 41 +++++++++++--------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/tns-core-modules/ui/button/button.android.ts b/tns-core-modules/ui/button/button.android.ts index aec1ffe6e..c10e83fc4 100644 --- a/tns-core-modules/ui/button/button.android.ts +++ b/tns-core-modules/ui/button/button.android.ts @@ -4,6 +4,9 @@ import dependencyObservable = require("ui/core/dependency-observable"); import style = require("ui/styling/style"); import { TextBaseStyler as TBS } from "ui/text-base/text-base-styler"; import {device} from "platform"; +import {GestureTypes, TouchGestureEventData, TouchAction} from "ui/gestures"; +import {PseudoClassHandler} from "ui/core/view"; + let styleHandlersInitialized: boolean; global.moduleMerge(common, exports); @@ -11,6 +14,7 @@ global.moduleMerge(common, exports); export class Button extends common.Button { private _android: android.widget.Button; private _isPressed: boolean; + private _highlightedHandler: (args: TouchGestureEventData) => void; constructor() { super(); @@ -44,24 +48,6 @@ export class Button extends common.Button { } } })); - - this._android.setOnTouchListener(new android.view.View.OnTouchListener( - { - get owner() { - return that.get(); - }, - - onTouch: function (v, ev) { - if (ev.getAction() === 0) { // down - this.owner._goToVisualState("highlighted"); - } - else if (ev.getAction() === 1) { // up - this.owner._goToVisualState("normal"); - } - return false; - } - } - )); } public _onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) { @@ -88,6 +74,25 @@ export class Button extends common.Button { this.android.setText(newText); } } + + @PseudoClassHandler("normal", "highlighted", "pressed", "active") + _updateHandler(subscribe: boolean) { + if (subscribe) { + this._highlightedHandler = this._highlightedHandler || ((args: TouchGestureEventData) => { + switch(args.action) { + case TouchAction.up: + this._goToVisualState("normal"); + break; + case TouchAction.down: + this._goToVisualState("highlighted"); + break; + } + }); + this.on(GestureTypes.touch, this._highlightedHandler); + } else { + this.off(GestureTypes.touch, this._highlightedHandler); + } + } } export class ButtonStyler implements style.Styler {