0

我正在 Android 设备上开发 Sencha Touch 2 应用程序。我的安卓设备不是普通的手机,所以它也有硬件键。我可以对 sencha touch 中的 keydown、keyup 或 keypress 事件做出反应吗?如何?其中一个就足够了。

如果有帮助:在普通 JavaScript 中,键可以由 onkeydown 监听器处理,并且 keyCode 为 0。

[更新] 我为 sencha touch 编写本教程。我已经完成了所有步骤,并且效果很好。现在我想抓住硬件钥匙,我上面描述的。我在Handle Android Hardware...中放置了一个 addEventListener 。

所以我的代码看起来像:

Ext.application({
    name: "NotesApp",
    models: ["Note"],
    stores: ["Notes"],
    controllers: ["Notes"],
    views: ["NotesList", "NotesListContainer", "NoteEditor"],
    launch: function () {           
        var notesListContainer = {
                xtype: "noteslistcontainer"
        };
        var noteEditor = {
                xtype: "noteeditor"
            };
        Ext.Viewport.add(notesListContainer, noteEditor);

        document.addEventListener("keydown", Ext.bind(onKeyDown, this), false); 

        function onKeyDown(e) {     
           // you are at the home screen
           if (Ext.Viewport.getActiveItem().xtype == notesListContainer.xtype ) {
              alert('aha'); // give out 'aha'
                  //  LABEL1

           } else {}

        }
    }


});

如何调用位置 LABEL1 上的函数。我要调用的函数在类 controllers.Notes 中,它的名称是 onNewScanCommand:

Ext.define("NotesApp.controller.Notes", {

extend: "Ext.app.Controller",
config: {
    refs: {
        // We're going to lookup our views by xtype.
        notesListContainer: "noteslistcontainer",
        noteEditor: {
            selector: 'noteeditor',
            xtype: 'noteeditor',
            autoCreate: true 
       }
    },
    control: {
        notesListContainer: {
            // The commands fired by the notes list container.
            newNoteCommand: "onNewNoteCommand",
            editNoteCommand: "onEditNoteCommand",
            newScanCommand: "onNewScanCommand"
        }
    }
},
onNewScanCommand: function () {
    console.log("onNewScanCommand");

    var now = new Date();
    var noteId = (now.getTime()).toString() + (this.getRandomInt(0, 100)).toString();

    var newNote = Ext.create("NotesApp.model.Note", {
        id: noteId,
        dateCreated: now,
        title: "",
        narrative: ""
    });

    // here I call a function in PhnoeGap, then this call a nativ function
    scan("echome", function(echoValue) {
        newNote.set("title", echoValue.scanNummer);
        newNote.set("narrative", echoValue.scanType);
    });



    var notesStore = Ext.getStore("Notes");
    /*
    if (null == notesStore.findRecord('id', newNote.data.id)) {
        notesStore.add(newNote);
    }*/
    notesStore.add(newNote);

    notesStore.sync();

    notesStore.sort([{ property: 'dateCreated', direction: 'DESC'}]);

    //this.activateNotesList();
},
// Base Class functions.
launch: function () {
    this.callParent(arguments);
    Ext.getStore("Notes").load();
    console.log("launch");
},
init: function () {
    this.callParent(arguments);
    console.log("init");
}

});

4

2 回答 2

2

在活动上试试这个

// 按下键

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
     if (i == KeyEvent.KEYCODE_BACK) {
          //back button key down
     }
return super.onKeyDown(keyCode, event);
}

// 向上键

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
      if (i == KeyEvent.KEYCODE_BACK) {
          //back button key up
      }
return super.onKeyUp(keyCode, event);
}

你可以看到所有关键事件 -> 这里

于 2012-11-08T14:57:05.357 回答
0

我有解决办法!我的控制器正在监视事件。事件在视图NotesListContainer中触发。

我向我的变量 notesListContainer 添加了一个 id,因此我可以获得 NotesListContainer 的对象触发 myevent newScanCommand

Ext.application({
        name: "NotesApp",
        models: ["Note"],
        stores: ["Notes"],
        controllers: ["Notes"],
        views: ["NotesList", "NotesListContainer", "NoteEditor"],
        launch: function () {

            var notesListContainer = {
                id: 'nlistcontainer',
                xtype: "noteslistcontainer"
            };
             var noteEditor = {
                xtype: "noteeditor"
            };
            Ext.Viewport.add(notesListContainer, noteEditor);

            document.addEventListener("keydown", Ext.bind(onBackKeyDown, this ), false); 

            function onBackKeyDown(e) {

                // you are at the home screen
                if (Ext.Viewport.getActiveItem().xtype == notesListContainer.xtype ) {

                    //the solution
                    Ext.getCmp('nlistcontainer').fireEvent("newScanCommand", this);

                } else {
                    this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {
                        url: 'noteslistcontainer'
                    }));
                }

            }
        }


    });

所以,这是我的救援:Ext.getCmp('nlistcontainer') ;-) 也许,它提供了一个更好的解决方案。

于 2012-11-09T16:44:34.727 回答