Search code examples
javascriptandroidtitaniumtitanium-mobileappcelerator

How to hide android soft keyboard from option dialog click event in titanium?


Hi i have this code in titanium mobile :

function showNetworkEditDialog(/*String*/ type, /*String*/ url, /*Row*/ selectedRow){
        var optionDialog = Ti.UI.createOptionDialog({
            title:'Type : ' + type,
            buttonNames:['Cancel', 'Ok']
        });

        var dialogView = Ti.UI.createView(_styles.get('view.dialog'));

        var lblURL = Ti.UI.createLabel(_globals.get('combine')(_styles.get('label.filter.normal'), {
            text:'URL:',
            left:'4dp'
        }));

        var txtURL = Titanium.UI.createTextField(_globals.get('combine')(_styles.get('textField'), {
            width:'100%',
            height:'40dp',
            value:url
        }));

        dialogView.add(lblURL);
        dialogView.add(txtURL);

        optionDialog.setAndroidView(dialogView);

        txtURL.addEventListener('focus',function(e){
            if (txtURL.getValue().toString() !== ''){
                txtURL.setSelection(txtURL.getValue().length,txtURL.getValue().length);
            }
        });

        optionDialog.addEventListener('click',function(e){
            Ti.UI.Android.hideSoftKeyboard(); // It's not works for me
            if (e.index === 1){ /* Ok Pressed */
                selectedRow._isEdited = true;

                selectedRow._url = txtURL.value.trim();
                selectedRow.children[1].setText(txtURL.value.trim());
            }
        });

        optionDialog.show();
    }

That code is showing an option dialog with textfield inside.. all i want is to hide android soft keyboard when i press OK or CANCEL button on that option dialog..

Does anyone know how to hide android keyboard when i press button OK or Cancel from optionDialog? i've tried use Ti.UI.Android.hideSoftKeyboard() and also use txtUrl.blur() but it not works for me.. Thanks Before..


Solution

  • You can use blur method to hide the softkeyboard.

    Try to change your code as follows

    optionDialog.addEventListener('click',function(e){
        txtURL.blur(); // Hides the softkeyboard
        if (e.index === 1){ /* Ok Pressed */
              selectedRow._isEdited = true;
              selectedRow._url = txtURL.value.trim();
              selectedRow.children[1].setText(txtURL.value.trim());
        }
    });
    

    Hope it helped you