如何向进度对话框添加延迟 - Java - Android - Android Studio
How can add a delay to Progress Dialog - Java - Android - Android Studio
我想让进度对话框显示 2 或 3 秒。
除了说进步之外,它实际上不会做任何事情。
我想添加一个延迟进度条。这怎么能成功?
它首先出现,但持续时间很短。
我的代码如下:
private void registerNewAccount(String username,String email, String password, String mobile, String gender){
final ProgressDialog progressDialog = new ProgressDialog( RegisterActivity.this);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setTitle("Registering New Account");
progressDialog.show();
String uRl = "http://192.168.1.6/Projects/LoginRegisterAndroid/register.php";
StringRequest request = new StringRequest(Request.Method.POST, uRl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("Successfull")){
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
finish();
}
else{
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> param = new HashMap<>();
param.put("username", username);
param.put("email", email);
param.put("psw", password);
param.put("mobile", mobile);
param.put("gender", gender);
return param;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getmInstance(RegisterActivity.this).addToRequestQueue(request);
}
}
我建议使用 Handler 也许:
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
// dismiss progress dialog here
}
}, 3000)
这可以在您的 Volley 请求中的 onRepsonse
方法中完成
我想让进度对话框显示 2 或 3 秒。
除了说进步之外,它实际上不会做任何事情。
我想添加一个延迟进度条。这怎么能成功?
它首先出现,但持续时间很短。
我的代码如下:
private void registerNewAccount(String username,String email, String password, String mobile, String gender){
final ProgressDialog progressDialog = new ProgressDialog( RegisterActivity.this);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setTitle("Registering New Account");
progressDialog.show();
String uRl = "http://192.168.1.6/Projects/LoginRegisterAndroid/register.php";
StringRequest request = new StringRequest(Request.Method.POST, uRl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("Successfull")){
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
finish();
}
else{
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> param = new HashMap<>();
param.put("username", username);
param.put("email", email);
param.put("psw", password);
param.put("mobile", mobile);
param.put("gender", gender);
return param;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getmInstance(RegisterActivity.this).addToRequestQueue(request);
}
}
我建议使用 Handler 也许:
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
// dismiss progress dialog here
}
}, 3000)
这可以在您的 Volley 请求中的 onRepsonse
方法中完成