Android 隐藏提交按钮,直到成功上传

Android 隐藏提交按钮,直到成功上传,android,iphone,appcelerator,Android,Iphone,Appcelerator,嗨,我是Appcelerator的新手,我需要一点帮助来使用我正在开发的应用程序。该应用程序包含一个带有视图的表单,在该表单中可以从相机中拍摄图像或从图库中选择图像。此视图有两个按钮。第一个按钮是上传图像,第二个按钮是提交表单。我想看看是否有人可以帮助修改代码,这样我就可以隐藏提交按钮,直到图像成功上传。提前感谢你的帮助。以下是我正在使用的代码: var win = Ti.UI.createView({ width : Ti.UI.FILL, height : Ti.UI.SIZ

嗨,我是Appcelerator的新手,我需要一点帮助来使用我正在开发的应用程序。该应用程序包含一个带有视图的表单,在该表单中可以从相机中拍摄图像或从图库中选择图像。此视图有两个按钮。第一个按钮是上传图像,第二个按钮是提交表单。我想看看是否有人可以帮助修改代码,这样我就可以隐藏提交按钮,直到图像成功上传。提前感谢你的帮助。以下是我正在使用的代码:

var win = Ti.UI.createView({
    width : Ti.UI.FILL,
    height : Ti.UI.SIZE,
    top : 20,
    title : L('header_title'),
});

var topbar = Ti.UI.createView({
    width : Ti.UI.FILL,
    height : 50,
    top : 0,
    title : L('header_title'),
    backgroundImage : Ti.Filesystem.resourcesDirectory + "image/topbar.png",
});

var back_btn = Ti.UI.createButton({
    width : 80,
    height : 30,
    title : "Customer",
    backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button_image.png",
    left : 5,
    top : 10,
    font : {
        fontSize : 12
    },
});

var home_btn = Ti.UI.createButton({
    width : 60,
    height : 30,
    title : "Home",
    backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button.png",
    right : 5,
    top : 10,
    font : {
        fontSize : 12
    },

});
back_btn.addEventListener('click', function() {
    self.close();

});

home_btn.addEventListener('click', function(e) {
    var Window;
    Window = require('ui/handheld/ApplicationWindow');
    new Window().open();
});

var header_title = Ti.UI.createLabel({
    color : 'white',
    font : {
        fontSize : 22
    },
    text : 'Forms',
    textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
    top : 10,
    width : Ti.UI.SIZE,
    height : Ti.UI.SIZE
});

topbar.add(header_title);
topbar.add(back_btn);
topbar.add(home_btn);
win.add(topbar);

var scroll = Ti.UI.createScrollView({
    width : Ti.UI.FILL,
    height : Ti.UI.FILL,
    top : "10%",
    contentHeight : 420,
    right : '2%',
    left : '3%',
});

var submit_img1 = Ti.UI.createImageView({
    width : 100,
    height : 100,
    top : 10,
    left : 10,
    backgroundDisabledColor : true,
});

var submit_btn1 = Ti.UI.createButton({
    width : 100,
    height : 30,
    top : 120,
    left : 10,
    font : {
        fontSize : 14
    },      
    backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button.png",
    title : 'Image1',
});


var basicSwitch = Ti.UI.createSwitch({
    top : 330,
    value : true,
    left : -100,
    width : 100
});

basicSwitch.addEventListener('change', function(e) {
    Ti.API.info('Switch value: ' + basicSwitch.value);

});

var upload_btn = Ti.UI.createButton({
    width : 100,
    height : 30,
    top : 330,
    right : 100,
    font : {
        fontSize : 14
    },      
    backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button.png",
    title : 'Upload First',
});

var SubmitForm_btn = Ti.UI.createButton({
    width : Ti.UI.FILL,
    height : 30,
    top : 380,
    right : 10,
    left : 10,
    font : {
        fontSize : 14
    },
    backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button.png",
    title : 'Submit Form',
    enable : 'false',
});

var style;
if (Ti.Platform.name === 'iPhone OS'){
  style = Ti.UI.iPhone.ActivityIndicatorStyle.DARK;
}
else {
  style = Ti.UI.ActivityIndicatorStyle.DARK;
}
activityIndicator = Ti.UI.createActivityIndicator({
  color: 'black',
  font: {fontFamily:'Helvetica Neue', fontSize:26, fontWeight:'bold'},
  message: 'Loading...',
  style:style,
  top:'35%',
  left:"25%",
  height:Ti.UI.SIZE,
  width:Ti.UI.SIZE
});

//image Uploding --Image 1
submit_btn1.addEventListener('click', function(e) {

    ///For Option dialog
    var sourceSelect1 = Titanium.UI.createOptionDialog({
        options : [L('Take Picture'), L('Choose Existing'), L('Cancel')],
        cancel : 2
    });
    sourceSelect1.show();
    sourceSelect1.addEventListener('click', function(e) {
        Ti.App.pic = Ti.App.Properties.getInt('pic');
        if (e.index === 0) {//Camera
            Titanium.Media.showCamera({
                success : function(e) {
                    var image = e.media.imageAsResized(e.media.width / 2, e.media.height / 2);
                    if (e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
                        Ti.App.Properties.setInt('pic', Ti.App.pic + 1);
                        Ti.App.pic = Ti.App.Properties.getInt('pic');
                        var picFileName = 'pic' + Ti.App.pic + '.png';
                        submit_img1.image = image;
                        image_value[0] = e.image;
                        image1_flag = true;
                        totalCount++;
                    }
                },
                error : function(error) {
                    if (error.code == Titanium.Media.NO_CAMERA) {
                        alert(L('no_camera'));
                    }
                },
                saveToPhotoGallery : true,
                allowEditing : true,
                mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
            });
        } else if (e.index === 1) {//PhotoCallery
            Ti.Media.openPhotoGallery({
                success : function(e) {
                    var image = e.media.imageAsResized(e.media.width / 2, e.media.height / 2);
                    if (e.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
                        Ti.App.Properties.setInt('pic', Ti.App.pic + 1);
                        Ti.App.pic = Ti.App.Properties.getInt('pic');
                        var picFileName = 'pic' + Ti.App.pic + '.png';
                        submit_img1.image = image;
                        image_value[0] = e.image;
                        image1_flag = true
                        totalCount++;
                    }
                },
                error : function(error) {
                    alert(error.code)
                },
                allowEditing : false,
                mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
            });
        }
    });
});

var ind = Titanium.UI.createProgressBar({
    width : 200,
    height : 50,
    min : 0,
    max : 1,
    value : 0,
    style : Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
    top : 200,
    message : 'Uploading Image',
    font : {
        fontSize : 12,
        fontWeight : 'bold'
    },
    color : '#888'
});

upload_btn.addEventListener('click', function(e) {

    pb.show();

    for(var i=0; i<=7; i++ ) {

        count_image =i+1;
        UploadPhotoToServer(count_image);

    }
});

var createReq = Titanium.Network.createHTTPClient();
SubmitForm_btn.addEventListener('click', function(e) {

activityIndicator.show();
Ti.API.info('Form Submitting');

var sender = Ti.App.Properties.getString('Sender');
var phone = Ti.App.Properties.getString('Phone');
var nts = Ti.App.Properties.getString('Notes');     
var email = Ti.App.Properties.getString('Email');

var st;
Ti.API.info('Switech state:' +basicSwitch.value);

if(basicSwitch.value==true)
{
    st='1';
}
else
{
    st='0';
}


if (Ti.Network.online == false) {
    var alertDL = Ti.UI.createAlertDialog({
        title : L('connection_error_title'),
        message : L('connection_error_msg'),
        buttonNames : ['OK']
    });
    alertDL.show();
} else {

var sender = Ti.App.Properties.getString('Sender');
var phone = Ti.App.Properties.getString('Phone');
var nts = Ti.App.Properties.getString('Notes');     
var email = Ti.App.Properties.getString('Email');

var params = {
//Server : Titanium
sender : sender,
phone : phone,
notes: nts,             
email : email,
enlist:st,
images:finalName,
Action:"coming_from_iphone"
};

//First View

console.log(params);
createReq.open("POST", 'http://www.mysite.com/ajax1.php');
createReq.setRequestHeader('Content-type', 'application/json; charset=utf-8');
createReq.send(params);

createReq.onload = function() {

    Ti.API.info('res@loginReq.onload:\n' + this.responseText);
    //alert(this.responseText);

    var Window;

    Window = require('ui/handheld/ApplicationWindow');

    new Window().open();

}

    createReq.onerror = function(e) {
        //Ti.App.fireEvent('server_error_msg');
        var alertDL = Ti.UI.createAlertDialog({
            title : L('error'),
            message : L('server_error_msg'),
            buttonNames : ['OK']
        });
        alertDL.show();
    }
}

});

var pb=Titanium.UI.createProgressBar({
    top:'50%',
    width:250,
    height:'auto',
    min:0,
    max:80,
    value:0,
    color:'#fff',
    message:'Image Uploading....',

    font:{fontSize:14, fontWeight:'bold'},
    style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
});
function UploadPhotoToServer(args) {

Ti.API.info('Image Nimber:',args);
if (Titanium.Network.online == true) {

    var xhr = Titanium.Network.createHTTPClient();
    xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
// On lo
    xhr.onerror = function(e) {
        Ti.API.info('IN ERROR ' + e.error);
        alert('Sorry, we could not upload your photo! Please try again.');
    };

    xhr.onload = function() {
    //alert(this.responseText);

    Ti.API.info('Final Name'+this.responseText);
    if(name_count ==0)
    {
        var res=this.responseText;
        finalName+=res;
        name_count++;
        Ti.API.info('Final Name'+finalName);
    }
    else
    {
        var res=this.responseText;
        finalName+=","+res;
        name_count++;

        //finalName =String(finalName ,+ this.responseText());
        name_count++;

        Ti.API.info('Final Name'+finalName);
    }
    // alert(finalName);        

    if (count_upload <= 70) {
            var s=80;


            count_upload += s/totalCount;
            pb.value= count_upload;

            if (count_upload == 80) {

                pb.hide();
            }
        } else {
            pb.hide();
        };
        Ti.API.info('Response From Server:'+this.responseText);
        Ti.API.info('IN ONLOAD ' + this.responseText + " " + this.status + ' readyState ' + this.readyState);

    };

    xhr.onsendstream = function(e) {
        Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
        if (Ti.Platform.osname == 'android') {
            //android doesn't support the "progress" variable during onsendstream yet :(
            //we're going to dummy up a progress value for this based on each packet being about 2.5% of the total upload progress
            //it won't be totally accurate, but it will give the user a good indicator that the upload is working
            if (androidUploadProgress < 1) {
                androidUploadProgress += 0.025;
            }
        } else {
            //else on ios devices, calculate the progress of the upload using e.progress
            if (Math.round(e.progress * 100) <= 100)
             {

            //pb.value+=1.0;

            }
            else
            {
                //pb.hide();
            }
        }
    };
    xhr.open('POST', 'http://www.mysite.com/ajax1.php?Action=upload_image');


        if (args==1)
             {
                if (image1_flag == true)
                {
                    xhr.send({
                        userfile : submit_img1.image,
                });
                }
                else
                {
                    //alert('no image');
                }
             };
    } 

    else {
        alert('You must have a valid Internet connection in order to upload this photo.');
    }
}

function createRandomString (argument) 
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 20; i++ ) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
}
scroll.add(submit_btn1);
scroll.add(submit_img1);
scroll.add(basicSwitch);
scroll.add(upload_btn);
scroll.add(SubmitForm_btn);

win.add(scroll);

self.add(win);
win.add(activityIndicator);
win.add(pb);

win.add(ind);

return self;
var-win=Ti.UI.createView({
宽度:Ti.UI.FILL,
高度:Ti.UI.SIZE,
前20名,
标题:L(“标题”),
});
var topbar=Ti.UI.createView({
宽度:Ti.UI.FILL,
身高:50,
排名:0,
标题:L(“标题”),
背景图片:Ti.Filesystem.resourcesDirectory+“image/topbar.png”,
});
var back\u btn=Ti.UI.createButton({
宽度:80,
身高:30,
标题:“客户”,
背景图片:Ti.Filesystem.resourcesDirectory+“image/button\u image.png”,
左:5,,
前10名,
字体:{
字体大小:12
},
});
var home\u btn=Ti.UI.createButton({
宽度:60,
身高:30,
标题:“家”,
背景图片:Ti.Filesystem.resourcesDirectory+“image/button.png”,
右:5,,
前10名,
字体:{
字体大小:12
},
});
back_btn.addEventListener('单击',函数()){
self.close();
});
home_btn.addEventListener('click',函数(e){
var窗口;
窗口=需要('ui/手持设备/应用程序窗口');
新窗口().open();
});
var header_title=Ti.UI.createLabel({
颜色:'白色',
字体:{
尺寸:22
},
正文:“表格”,
TEXT对齐:Ti.UI.TEXT\u对齐\u中心,
前10名,
宽度:Ti.UI.SIZE,
高度:Ti.UI.SIZE
});
顶栏。添加(标题和标题);
顶栏。添加(背面);
顶栏。添加(主页\u btn);
添加(顶栏);
var scroll=Ti.UI.createScrollView({
宽度:Ti.UI.FILL,
高度:Ti.UI.FILL,
顶部:“10%”,
内容高度:420,
对:"2%",,
左:“3%”,
});
var submit\u img1=Ti.UI.createImageView({
宽度:100,
身高:100,
前10名,
左:10,,
背景颜色:对,
});
var submit_btn1=Ti.UI.createButton({
宽度:100,
身高:30,
排名:120,
左:10,,
字体:{
尺寸:14
},      
背景图片:Ti.Filesystem.resourcesDirectory+“image/button.png”,
标题:“图像1”,
});
var basicSwitch=Ti.UI.createSwitch({
top:330,
价值观:正确,
左:-100,
宽度:100
});
basicSwitch.addEventListener('change',函数(e){
Ti.API.info('开关值:'+基本开关值);
});
var upload_btn=Ti.UI.createButton({
宽度:100,
身高:30,
top:330,
右:100,,
字体:{
尺寸:14
},      
背景图片:Ti.Filesystem.resourcesDirectory+“image/button.png”,
标题:“先上传”,
});
var SubmitForm_btn=Ti.UI.createButton({
宽度:Ti.UI.FILL,
身高:30,
top:380,
右:10,,
左:10,,
字体:{
尺寸:14
},
背景图片:Ti.Filesystem.resourcesDirectory+“image/button.png”,
标题:“提交表格”,
启用:“false”,
});
var风格;
如果(Ti.Platform.name===‘iPhone操作系统’){
style=Ti.UI.iPhone.ActivityIndicatorStyle.DARK;
}
否则{
style=Ti.UI.ActivityIndicatorStyle.DARK;
}
activityIndicator=Ti.UI.createActivityIndicator({
颜色:'黑色',
字体:{fontFamily:'Helvetica Neue',fontSize:26,fontWeight:'bold'},
消息:“正在加载…”,
风格:风格,
最高:35%,
左:“25%”,
高度:Ti.UI.SIZE,
宽度:Ti.UI.SIZE
});
//图像上传——图像1
submit_btn1.addEventListener('click',函数(e){
///用于选项对话框
var sourceSelect1=Titanium.UI.createOptionDialog({
选项:[L('Take Picture')、L('Choose Existing')、L('Cancel')],
取消:2
});
sourceSelect1.show();
sourceSelect1.addEventListener('click',函数(e){
Ti.App.pic=Ti.App.Properties.getInt('pic');
如果(e.index==0){//Camera
钛合金.媒体.展示照相机({
成功:职能(e){
var image=e.media.imageAsResized(e.media.width/2,e.media.height/2);
如果(e.mediaType==Ti.Media.Media\u TYPE\u PHOTO){
Ti.App.Properties.setInt('pic',Ti.App.pic+1);
Ti.App.pic=Ti.App.Properties.getInt('pic');
var picFileName='pic'+Ti.App.pic+'.png';
提交\ img1.image=图像;
图像_值[0]=e.image;
image1_标志=真;
totalCount++;
}
},
错误:函数(错误){
if(error.code==tianium.Media.NO_摄像头){
警报(L(“无摄像头”);
}
},
saveToPhotoGallery:对,
允许:是的,
媒体类型:[Ti.Media.Media\u TYPE\u PHOTO]
});
}如果(e.index==1){//photocalery
Ti.Media.openPhotoGallery({
成功:职能(e){
var image=e.media.imageAsResized(e.media.width/2,e.media.height/2);
如果(e.mediaType==Ti.Media.Media\u TYPE\u PHOTO){
Ti.App.Properties.setInt('pic',Ti.App.pic+1);
Ti.App.pic=Ti.App.Properties.getInt('pic');
var picFileName='pic'+Ti.App.pic+'.png';
提交\ img1.image=图像;
图像_值[0]=e.image;
image1_标志=真
totalCount++;
}
},
错误:函数(错误){
警报(错误代码)
},
允许:错误,
Button button.setEnable(false);
button.setEnable(true);
 Titanium.App.addEventListener('hideButton', function(e) {
            yourButton.hide();
        });
Titanium.App.fireEvent('hideButton');