【问题标题】:How can i change the value of a text field in sencha touch?如何更改 sencha touch 中文本字段的值?
【发布时间】:2012-07-25 10:30:39
【问题描述】:

我正在使用 phonegap 中的条形码扫描仪插件扫描条形码。获得值后,我想在文本字段中显示它。以下是我的文本字段代码:

{
     xtype : 'textfield',
      id : 'barcodetextfield',
     width : 300,
     margin : '0 0 0 10',
     labelWidth : '40%',
     label : 'Enter Barcode'                                             
}

在控制器中,我试图在文本字段中设置条形码值。

window.plugins.barcodeScanner.scan(function(result) {
    this.getBarcodebox.setValue(""+result.text);
 }, function(error) {
     alert("Scanning failed: " + error);
 });

我把 ref 放在控制器中,如下所示:

barcodebox : '#barcodetextfield',

这是我的整个控制器代码:::

Ext.define('MyApp.controller.MyController', {
extend : 'Ext.app.Controller',
config : {
    refs : {
        TitlePanel : 'login',
        dashboardpanel : 'dashboard',
        ConsumerSignup : 'consumersignup',
        AddtoWishlog : 'addwishlog',
        wishlogsummary : 'wishlogsummarylist',
        FeedbackSummary : 'feedbacksummarylist',
        ConsumerSignin : 'Consumersignin',
        barcodebox : '#barcodetextfield',
        labelid : 'title'
    },
    views : [ 'TitlePanel', 'dashboardpanel', 'ConsumerSignup',
            'AddtoWishlog', 'wishlogsummary', 'FeedbackSummary',
            'ConsumerSignin' ],

    control : {
        "#LoginBtn" : {
            tap : 'onLoginButtonTap'
        },

        "#wishloghomebutton" : {
            tap : 'onWishlogHomeButtonTap'
        },

        "#feedbacksummaryhomebutton" : {
            tap : 'onFeedbackHomeButtonTap'
        },

        "#wishlogbtn" : {
            tap : 'onWishlogButtonTap'
        },

        "#Feedbackbtn" : {
            tap : 'onFeedbackButtonTap'
        },

        "#signup" : {
            tap : 'onSignupButtonTap'
        },

        "#capturebtn" : {
            tap : 'onCaptureButtonTap'
        },

        "#consumersignuphomebutton" : {
            tap : 'onConsumerSignupHomeButtonTap'
        },

        "#selectphoto" : {
            tap : 'onSelectPhotoButtonTap'
        },

        "#scanbutton" : {
            tap : 'onScanButtonTap'
        }

    }
},

slideLeftTransition : {
    type : 'slide',
    direction : 'left'
},
slideRightTransition : {
    type : 'slide',
    direction : 'right'
},

onLoginButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideLeftTransition);
    // this.getLabelid.setHtml('Dashboard');
},

onWishlogButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getWishlogsummary(),
            this.slideLeftTransition);
},

onFeedbackButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getFeedbackSummary(),
            this.slideLeftTransition);
},

onSignupButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getConsumerSignup(),
            this.slideLeftTransition);
},

onWishlogHomeButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideRightTransition);
},

onFeedbackHomeButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideRightTransition);
},

onConsumerSignupHomeButtonTap : function(button, e, options) {

    Ext.Viewport.setActiveItem(this.getDashboardpanel(),
            this.slideRightTransition);
},

onCaptureButtonTap : function(button, e, options) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality : 100,
        destinationType : Camera.DestinationType.FILE_URI
    });

    function onPhotoURISuccess(imageURI) {
        // console.log(imageURI);
        var largeImage = document.getElementById('capturedimage');
        largeImage.style.display = 'block';
        largeImage.src = imageURI;
    }

    function getPhoto(source) {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, {
            quality : 100,
            destinationType : destinationType.FILE_URI,
            sourceType : source
        });
    }
    function onFail(message) {
        alert('It is failed: ' + message);
    }

},

onSelectPhotoButtonTap : function(button, e, options) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality : 50,
        destinationType : navigator.camera.DestinationType.FILE_URI,
        sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY
    });

    function onPhotoURISuccess(imageURI) {
        // console.log(imageURI);
        var largeImage = document.getElementById('capturedimage');
        largeImage.style.display = 'block';
        largeImage.src = imageURI;
    }

    function getPhoto(source) {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, {
            quality : 100,
            destinationType : destinationType.FILE_URI,
            sourceType : source
        });
    }
    function onFail(message) {
        alert('It is failed: ' + message);
    }
},

onScanButtonTap : function(button, e, options) {
    window.plugins.barcodeScanner.scan(function(result) {
        this.getBarcodebox().setValue(""+result.text);

// alert("我们有条码\n" + // "结果:" + result.text); },函数(错误){ alert("扫描失败:" + error); }); } });

当我在对话框中显示值时,它会正确显示。但无法在文本框中设置。我如何设置条形码?请帮忙。

【问题讨论】:

  • 我得到以下错误:无法调用未定义的方法'setValue'

标签: android cordova sencha-touch sencha-touch-2 barcode-scanner


【解决方案1】:

应该:

    this.getBarcodebox.setValue(""+result.text);

不是方法调用?

    this.getBarcodebox().setValue(""+result.text);

当然,假设您在控制器中添加了barcodebox 作为ref

    refs: {
        barcodebox: '#barcodtext'
    }

注意id: #barcodtext 中的错字(缺少'e')并确保它等于ref 选择器指定的值。

-- 编辑

看起来this 超出了您的回调范围,因此不引用您的控制器。您可以尝试以下方法:

window.plugins.barcodeScanner.scan(function(result) {
    var controller = Ext.app.Application.getController('MyController');
    controller.getBarcodebox.setValue(""+result.text);
}, function(error) {
    alert("Scanning failed: " + error);
});

【讨论】:

  • 我编辑了代码..你可以检查..但仍然出现错误说:成功回调错误:BarcodeScanner2 = TypeError:无法调用未定义的方法'setValue'
  • 在您编辑的代码中,我仍然看不到 getBarcodebox () 后面的括号?你加了这些吗?
  • 正如我在回答中所写,您的代码应该看起来像 this.getBarcodebox().setValue(""+result.text);,所以使用 (),因为它是一个方法调用,而不是一个字段。
  • 成功回调错误:BarcodeScanner2 = TypeError: Object # has no method 'getBarcodebox' 这是我在添加大括号后得到的..
  • 你能用完整的控制器代码更新你的帖子吗?也许我们在这里遗漏了一些重要的东西。
【解决方案2】:

不要在refs 中使用barcodebox : '#barcodetextfield', 尝试使用getCmp()

var bc = Ext.getCmp('barcodetextfield');

在那之后bc.setvalue(''+result.text);

我希望这会有所帮助..

【讨论】:

  • 以这种方式尝试过..虽然它没有显示任何错误但它没有给我返回文本框中的条形码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多