Close KeyBoard Android - Not Working


#1

I´m trying to hide Keyboard Input and i haven´t made to work,

I am using this code in controller.js

if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.close();
}

Please help.

Thanks


#2

Not sure how you’ve installed it but, apart from the defensive code, I just call Keyboard.close();

Have you tested to see if the line cordova.plugins.Keyboard.close(); is executing, or if it’s being ignored because your check is failing?


#3

If i put in a button works great…

I have put a message so i know that is executed. I don´t know if exists a “post event” that fires agains the keyboard,

I want to close “automatically”, is some kind of a finish event that i can close keyboard?


#4

So are you trying to close the keyboard when someone presses enter on an input field? It’s hard to know what’s going on without a bit more information about what your trying to achieve.

The keyboard usually pops up as soon as an editable field receives the focus; I’d guess that’s what’s happening to you.


#5

I want to close keyboard when i have login information.

The keyboard keeps popping (i have used blur() to clear focus but same situation)

This is my code

.controller(‘LoginCtrl’, function ($scope, HeliotextilWSDL, $ionicLoading, $ionicPopup, $rootScope, $state, $timeout, $ionicViewService) {

// GOOGLE ANALITYCS
ga('send', 'pageview', {
    'page': 'VendasHeliotextil/Login'
});

//===//

// se INTERNET explorer AVISA QUE NÃO É totalmente SUPORTADO
if (bowser.msie) {
    $ionicPopup.alert({
        title: '<b>Aviso</b>',
        template: 'Internet Explorer não é totalmente suportado (problemas nos Alertas, etc.)<br><br><b>Use outro browser<br>(Ex: Google Chrome)</b>'
    });
}

// ------------------------
// ION-TOGGLE - dados login
// ------------------------

$scope.dadosLogin = {
    checked: true
};

// CHANGE

$scope.DadosLogin_Change = function () {
    $timeout(function () {
        window.localStorage.setItem("guardadados_login", $scope.dadosLogin.checked);
        //alert('Push Notification Change: ' + $scope.dadosLogin.checked);
    }, 0);
};

// -------------------------------------------------
// VÊ A OPÇÃO GUARDADA (ion-toogle) NO html5 STORAGE
// -------------------------------------------------

if (window.localStorage.getItem("guardadados_login") != null) {

    $scope.dadosLogin.checked = false;
    if (window.localStorage.getItem("guardadados_login") == 'true') {
        $scope.dadosLogin.checked = true;
    }
}

//===//
//===//

// LIMPA o LOCAL STORAGE (usado no controller das VENDAS)

LimpaHtml5_Storage_Global();
// login NOK
window.localStorage.setItem("login", "nok");

// layouts

$scope.Layouts = [
    {
        text: "Smartphone",
        value: "SM"
    },
    {
        text: "Tablet/Desktop",
        value: "TB_DK"
    }
];

// TEM username no STORAGE ?

if (window.localStorage.getItem("username") != null) {

    // decripta os dados
    $scope.txtUsername = CryptoJS.AES.decrypt(window.localStorage.getItem("username"), "sdhfgsjddj").toString(CryptoJS.enc.Latin1);
    $scope.txtPassword = CryptoJS.AES.decrypt(window.localStorage.getItem("password"), "fsdhfkjsddjk").toString(CryptoJS.enc.Latin1);
    
    if (window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.close();
    }

}
else {
    
    document.getElementById("username").focus(); //old school
}
    
// -------------------
// tipo de dispositivo
// -------------------

// smartphone
if (device.mobile()) {

    $scope.data = {
        LayoutSeleccionado: 'SM'
    }
}

// tablet
if (device.tablet()) {

    $scope.data = {
        LayoutSeleccionado: 'TB_DK'
    }
}

// desktop
if (device.desktop()) {

    $scope.data = {
        LayoutSeleccionado: 'TB_DK'
    }
}

// -*-*-*-*-*-*-*
// click do botão
// -*-*-*-*-*-*-*

$scope.login = function () {

    // NET ?

    // função definida no APP.JS
    if (!LigadoaInternet()) {

        $ionicPopup.alert({
            title: '<b>Aviso</b>',
            template: 'Não foi possível ligar ao Servidor da Heliotextil.<br>Verifique que está ligado à Internet.'
        });

        // "morre" aqui
        throw ("Sem Internet");
    }

    // "EMPURRÃO" NOS DADOS

    $scope.txtUsername = $('#username').val();
    $scope.txtPassword = $('#password').val();

    //

    if ($scope.txtUsername == "" || $scope.txtPassword == "") {

        $ionicPopup.alert({
            title: '<b>Aviso</b>',
            template: 'Preencha os Dados'
        });

        // "morre" aqui
        throw ("Sem Dados");
    }

    // ----
    // WSDL
    // ----

    $ionicLoading.show({
        template: '<i class="icon ion-loading-c"></i>&nbsp;&nbsp;A Carregar ...'
    });

    //

    HeliotextilWSDL.Login(1, $scope.txtUsername, $scope.txtPassword).success(function (response) {

        $scope.dados = response;

        // loader

        $ionicLoading.hide();

        // login VÁLIDO

        if (response.oLoginValido == true) {

            window.localStorage.setItem("login", "ok");
            // se guardar os dados de login
            if ($scope.dadosLogin.checked == true) {
                window.localStorage.setItem("username", CryptoJS.AES.encrypt($scope.txtUsername, "sfdsdf")); //encriptado
                window.localStorage.setItem("password", CryptoJS.AES.encrypt($scope.txtPassword, "sfdfsdfd")); //encriptado
            }

            // se não guardar apaga
            else {
                window.localStorage.removeItem("username");
                window.localStorage.removeItem("password");
            }

            // ----------------------------------------
            // vai para a página do LAYOUT seleccionado
            // ----------------------------------------

            if ($scope.data.LayoutSeleccionado == "SM") {
                $state.go("gerais_smartphone");
            }

            if ($scope.data.LayoutSeleccionado == "TB_DK") {
                $state.go("gerais_tablet");
            }

            // login NÃO VÁLIDO

        } else {

            window.localStorage.setItem("login", "nok");
            window.localStorage.removeItem("username");
            window.localStorage.removeItem("password");
            //window.localStorage.setItem("username", "");
            //window.localStorage.setItem("password", "");

            var alertPopup = $ionicPopup.alert({
                title: '<b>Aviso</b>',
                template: 'Login Incorreto'
            });
            alertPopup.then(function (res) {
                if (res) {
                    //$('#password').focus();
                    //document.getElementById("password").focus();
                }
            })
        }
    })
} // fim scope login

#6

Formatting makes it a bit difficult to see (and the code’s a bit of a mixture of old style jQuery / new Angular) but it looks as if you want to ensure that the keyboard isn’t shown if the username and password are already prepopulated from previous values in local storage?

And you can’t just disable the input fields because you may want to allow the username / password to be corrected?

Is that correct?

If it is there are a couple of things you could do to get the effect you want:

  1. disable the username and password fields whenever the username and password are prepopulated. That will prevent focus and should stop the keyboard popping up.

In your controller set:

$scope.passwordDisabled = false;
$scope.usernameDisabled = false;

at the top, and then if you fill these values in from local storage set them both to to true.

In the fields themselves, add ng-disabled=“passwordDisabled” and ng-disabled=“usernameDisabled” to the appropriate input fields.

Theres another step to take care of when you want to edit them, but let me know if that part works first.


#7

Your thinks are correct. I have made that you asked and the keyboard still pops!!

Very Weird. The “front-end”.


#8

Hmmm, are the username and password fields definitely disabled and not editable?


#9

Yes. I cannot click on input. In print-screen you can see a gray in inputs.


#10

I wonder if this is timing? Maybe the keyboard is shown before the disabled flag is evaluated.

If you ignore the variables and just set ng-disabled=“true” on both fields, do you still get the keyboard?


#11

Yes. The keyboard stills Pop.


#12

Odd. Now I’d say you need to comment out the input fields on that form altogether, one by one, until the keyboard stops appearing.


#13

I will try tomorrow. I will give feedback,


#14

I haven´t be able to hide the keyboard.

I use another aproach. I hide the keyboard by default on config.xml.

In the first time that the user opens the app he clicks in the input to open the keyboard. (After login i save credentials).

After that the credentials are automatically filled and the user only needs click in the button. (no annoying keyboard)

Thanks for your help.

:slight_smile:


#15

Baffled, but glad you got it working.