开发者

Appcelerator - Checking which textfield has focus

开发者 https://www.devze.com 2023-03-11 22:19 出处:网络
I have a keyboard toolbar setup with a PREVIOUS and NEXT button. I\'m trying to figure out which textfield has focus so I can allow the user to jump to the next field or go back to the previous field.

I have a keyboard toolbar setup with a PREVIOUS and NEXT button. I'm trying to figure out which textfield has focus so I can allow the user to jump to the next field or go back to the previous field. My code currently looks like this:

//Setup keyboard toolbar.
var flexSpace = Titanium.UI.createButton({
    systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});

var navButtons = Titanium.UI.createButtonBar({
    labels:['Previous','Next'],
    backgroundColor:'#336699',
    top:100,
    style:Titanium.UI.iPhone.SystemB开发者_运维技巧uttonStyle.BAR,
    height:25,
    width:'auto'
});

navButtons.addEventListener('click',function(e){
    // Previous Button, index 0.
    Titanium.API.info(e.index);
    if (e.index == 0){
        if (nameTxt.hasFocus == "nameTxt"){
            phoneTxt.focus();
        }
    }

    // Next Button, index 1.
    if (e.index == 1){
        if (nameTxt.hasFocus){
            nameTxt.hasFocus = false;
            phoneTxt.hasFocus = true;
        }/*else{
            done.fireEvent('click');
        }*/
    }

}); 

I think I'm close to getting this working, so just need the final push. Any help appreciated.

Regards, Stephen


you could just set the keyboard to have a next button on it?

it won't solve the forward and back, but it is easier than the solution below.

The hard way is to track which field has focus in a separate property. Whenever the focus changes, update the property to the new item that has focus


Have you tried using addEventListener with blur and focus? https://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.TextField-object


Extracted from here:

http://developer.appcelerator.com/question/120797/textfield---how-to-check-the-focus

function CreateTextField(options) {
    var tf = Ti.UI.createTextField(options);
    //define flag
    tf.hasFocus = false;

    tf.addEventListener('focus', function() {
        this.hasFocus = true;
    });

    tf.addEventListener('blur', function() {
        this.hasFocus = false;
    });
    return tf;
}

And then just call the CreateTextField like:

email=CreateTextField({
          width: 150,
          left: 110,
          height: 35,
          hintText: 'login_email',
          borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE,
          keyboardType : Titanium.UI.KEYBOARD_EMAIL,
          keyboardToolbar : [navButtons, flexSpace, done],
          color: '#949495'
        })

After that, you can do:

Titanium.API.info(email.hasFocus);

if (e.index == 0){
  if (email.hasFocus){
    email.focus();
  }
}

Hope this help you!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号