【问题标题】:Titanium: handle back button event on textfieldTitanium:处理文本字段上的后退按钮事件
【发布时间】:2012-09-20 23:06:49
【问题描述】:

问题

我想使用钛处理文本字段上的后退按钮。

我知道有一个事件 android:back on window 但它不会在 textfield 上触发。

如何使用钛处理文本字段上的后退按钮(或键盘隐藏事件)?

编辑:这里有一些代码和说明来说明我想说的话:

重现步骤:

  1. 单击文本字段 > 触发焦点事件,显示键盘
  2. 回击按钮 > 键盘已隐藏,但未调用模糊事件且文本字段未失去焦点

代码:

var textfield = Ti.UI.createTextfield();
textfield.addEventListener('android:back', function() {
  // this method is never called, so this event does not run on textfield
});

textfield.addEventListener('focus', function() {
  // this method is called at step 1
});

textfield.addEventListener('blur', function() {
  // this method is not called at step 2 because 
  // the back button only hide the keyboard but the focus is not lost
});

// what code should I use to catch event when the keyboard is hidden 
// when pressing the back button ?

【问题讨论】:

    标签: android titanium back-button


    【解决方案1】:

    只要关闭键盘,就会触发blur 事件。 Here is the doc for it.

    假设你创建了一个textField 类型为Titanium.UI.TextField 的对象,你可以很容易地听它:

    textField.addEventListener('blur', function(e) {
        var val = e.value; // Get current value when the keyboard closed (the right way) as opposed to textField.value (the wrong way)
        alert("The text is : "+value);
    });
    

    如果你想监听android:back 事件,你必须在窗口上监听。

    var win = Ti.UI.currentWindow; 
    win.addEventListener('android:back', function(e) {
        // do crazy things
    });
    

    根据文档,这应该可以工作,如果不是 Titanium 的错误的话?

    【讨论】:

    • 谢谢,但这并不容易。当您点击后退按钮时,键盘被隐藏但焦点不会丢失(感谢 android...),因此不会触发 blur 事件:(
    • 您不应该在 android:back 事件的文本字段上收听。你应该在窗户上听。
    • 是的,我试过了,但是在显示键盘时不会触发事件。当您按下后退按钮时,键盘会被隐藏,但不会触发任何事件(文本字段上的模糊或窗口上的 android:back 都不会)。然后,当第二次点击完成时,会触发 android:back on window 事件,但我想避免这第二步。
    • 什么时候显示键盘?那将是focus 事件。我对您要完成的工作感到有些困惑。也许显示一些代码?
    • 我更新了我的帖子以澄清问题。感谢您的帮助;)
    猜你喜欢
    • 2017-03-28
    • 2011-03-26
    • 1970-01-01
    • 2016-01-14
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多