我正在尝试找到暂时停用然后重新启动上传按钮的正确位置,因此用户在等待上传时不会多次单击它。 到目前为止,这是我的方法:
private void uploadFile() {
if (mImageUri != null) {
StorageReference sR = mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
mButtonUpload.setClickable(false);
sR.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_SHORT).show();
Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
taskSnapshot.getDownloadUrl().toString());
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(upload);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
mProgressBar.setProgress((int) progress);
}
})
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(0);
}
}, 500);
mButtonUpload.setClickable(true);
}
});
} else {
Toast.makeText(MainActivity.this, "No file selected", Toast.LENGTH_SHORT).show();
}
}
但是,根据this,onCompleteListener
仅在有请求时才会被调用。所以它可能没有被调用(例如,如果没有互联网连接)。那么重新激活我的按钮的适当位置在哪里?
修改
你们怎么看待将StorageTask存储在成员变量中,然后检查按钮点击?我注意到,当没有连接时,上传任务仍然保留在队列中,所以只是重新激活按钮不是一个好方法。
mUploadTask = fileReference.putFile(mImageUri)
和
mButtonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(MainActivity.this, "Upload in progress", Toast.LENGTH_SHORT).show();
} else {
uploadFile();
}
}
});
答案 0 :(得分:0)
在android:clickable = "true"
处于可见状态时禁用用户互动。你可以用两种方式做到这一点,
首先 - 只需在progress bar
xml中设置<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>
即可。例如,
progress bar
第二个或者当getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
可见时,在java中以编程方式禁用用户交互。例如,
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
要再次启用用户互动,
FirebaseNetworkException
修改如何捕获firebaseAuth.signInWithCredential(mAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// task complete succesfully
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
if (exception instanceof FirebaseNetworkException){
// no network connection
} else {
// any other exception occur
}
}
});
。试试这个,
Firebase Exception
有关.forEach()
的详细信息,请查看此链接$out