【问题标题】:Titanium appcelerator : window focus event not triggered when returning from other windowTitanium appcelerator:从其他窗口返回时未触发窗口焦点事件
【发布时间】:2013-10-18 09:00:37
【问题描述】:

我最近开始使用钛 appcelerator 开发 Android 应用程序。并且陷入了一个问题,这对于其他精通 Titanium 的人来说似乎是非常小的问题,但它让我作为初学者感到头疼。

我有两个名为 ma​​insubwindow 的窗口。单击按钮时,我会从 ma​​in 重定向到 subwindow窗户。现在,每当我从 子窗口 按下模拟器上的返回按钮时。我已经编写了关闭当前窗口的逻辑,即 subwindow 以便我可以查看 ma​​in 窗口。即使 subwindow 成功关闭并且我可以查看 ma​​in 窗口,它也能正常工作。但是现在,如果我尝试单击 ma​​in 窗口中的按钮,则不会发生。

即使我试图在 ma​​in 窗口中捕获焦点事件,该事件发生在最初加载应用程序但当我在 subwindow 并关闭 subwindow 时, ma​​in 窗口中不会触发文件焦点事件。

这是我的代码

APP.JS

var win1 = Titanium.UI.createWindow({  
    backgroundColor:'black'
});

var btngo=Ti.UI.createButton({
    title : "Go !!!!",
    top : "30%"
});

var lbltitle=Ti.UI.createLabel({
    text : "This is home page",
    font : {fontSize : "20%"} ,
    top : "20%",
    color : 'white'
});

//Event listener for adding clicking event
btngo.addEventListener('click',function(e){
    var nextwindow=Ti.UI.createWindow({
        url : "window.js"   
    });
    nextwindow.open();
});
win1.addEventListener('focus',function(e)
{
    alert("main window focused");
});
win1.addEventListener('android:back', function (e) 
{
  win1.close();
});
win1.add(lbltitle);
win1.add(btngo);
win1.open();

WINDOW.JS

var childwindow=Ti.UI.createWindow({
    backgroundColor : "white"
});


var btnhome=Ti.UI.createButton({
    title : "HOME PAGE"
});


var lbltitle=Ti.UI.createLabel({
    text : "This is child window",
    font : {fontSize : "20%"} ,
    top : "20%",
    color : 'white'
});
//For adding event listener for detecting click on home page button
btnhome.addEventListener('click',function(e){
     childwindow.close();   
});

//Adding event listener for detcting back button click in android 
childwindow.addEventListener('android:back', function (e) {
  childwindow.close();
});

childwindow.add(lbltitle);
childwindow.add(btnhome);
childwindow.open();

希望我能尽快解决我的问题。

【问题讨论】:

  • 你能分享你的代码吗
  • 请用代码示例更新您的问题。以便我们轻松为您提供帮助。
  • 嘿,如何添加代码。 . .我也是堆栈流的新手。 .你能帮我解决那个阿南德吗? . .jst 正在搜索相同的内容。 .
  • 只需点击问题下方的编辑链接并在下方附加您的代码,然后选择代码并点击顶部的“{}”符号。你完成了:)
  • 请参考meta.stackexchange.com/questions/22186/…了解代码格式

标签: android titanium appcelerator appcelerator-mobile


【解决方案1】:

您正在 window.js 中创建一个正在打开的另一个窗口。可能这就是问题所在。尝试如下更改您的 window.js 文件

var childwindow=Ti.UI.currentWindow;
childwindow.backgroundColor = 'white'; //You may specify the same while creating the window
var btnhome=Ti.UI.createButton({
    title : "HOME PAGE"
});


var lbltitle=Ti.UI.createLabel({
    text : "This is child window",
    font : {fontSize : "20%"} ,
    top : "20%",
    color : 'white'
});
//For adding event listener for detecting click on home page button
btnhome.addEventListener('click',function(e){
     childwindow.close();   
});

//Adding event listener for detcting back button click in android 
childwindow.addEventListener('android:back', function (e) {
  childwindow.close();
});

childwindow.add(lbltitle);
childwindow.add(btnhome);

希望这能解决问题:)

【讨论】:

  • 我已经尝试了上面给出的代码。它给出了一个错误。 . window.js:2 发生异常:未捕获的 ReferenceError:未定义 childWindow
  • 我刚刚尝试删除 childWindow.backgroundColor = 'white';。已执行,但此处的窗口正在重叠。我可以在单个窗口中看到 app.js 和 window.js 的内容
  • 请查看更新后的代码。代码中有拼写错误。由于新窗口的背景色是透明的,因此窗口重叠。您可以像上面那样设置它,也可以在 app.js 中创建新窗口时将其设置为 Ti.UI.createWindow({backgroundColor:'white', url:'window.js'});
  • 非常感谢阿南德。它对我很有用。终于结束了自过去 4 到 5 天以来我面临的一个大问题。你们所有人。非常感谢你。你是天才。
  • 好的,谢谢。通过接受答案,SO 意味着答案解决了您的问题。它可能对其他人有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 2016-06-25
相关资源
最近更新 更多