无法关闭 Android 中的对话框
Can't dismiss dialog in Android
我不知道为什么我的对话框不能关闭。实际上,我在我的数据库中看到,我们成功获取的数据已保存在该数据库中。只是对话框关闭代码不起作用。
这是我的代码:
package com.app.kfstore.EmailLoginRegister;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.app.kfstore.MainActivity;
import com.app.kfstore.OperationRetrofitApi.ApiClient;
import com.app.kfstore.OperationRetrofitApi.ApiInterface;
import com.app.kfstore.OperationRetrofitApi.Users;
import com.app.kfstore.R;
import com.blogspot.atifsoftwares.animatoolib.Animatoo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class EmailRegisterActivity extends AppCompatActivity {
private EditText name,email,password;
private Button regBtn;
public static ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_register);
//////hide status bar code//////
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//////end code for hide status bar//////
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
init();
}
private void init() {
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
regBtn = (Button) findViewById(R.id.button2);
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Registration();
}
});
}
private void Registration() {
String user_name = name.getText().toString().trim();
String user_email = email.getText().toString().trim();
String user_password = password.getText().toString().trim();
if(TextUtils.isEmpty(user_name)){
name.setError("Name is required!");
}
else if(TextUtils.isEmpty(user_email)){
email.setError("Email is required!");
}
else if(TextUtils.isEmpty(user_password)){
password.setError("Password is required!");
}
else {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Registering...");
dialog.setMessage("Please wait while we adding your credentials");
dialog.show();
dialog.setCanceledOnTouchOutside(false);
Call<Users> call = apiInterface.performEmailRegistration(user_name,user_email,user_password);
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if(response.body().getResponse().equals("ok")){
Toast.makeText(EmailRegisterActivity.this, "Your account has been created", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("failed!")){
Toast.makeText(EmailRegisterActivity.this, "Something went wrong, Please try again!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("already")){
Toast.makeText(EmailRegisterActivity.this, "This email is already exists, Please try another email", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
}
});
}
}
//////link code for text go to login page//////
public void goToLogin(View view) {
Intent intent = new Intent(EmailRegisterActivity.this, EmailLoginActivity.class);
startActivity(intent);
Animatoo.animateSlideRight(this);
finish();
}
///////end of code//////
//////link code for back button go to main page//////
public void goToMainPage(View view) {
Intent intent = new Intent(EmailRegisterActivity.this, MainActivity.class);
startActivity(intent);
Animatoo.animateSlideRight(this);
finish();
}
//////end of code//////
}
这是为了便于查看代码(只是相同的代码,但我只是剪切了顶部和底部的代码)
else {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Registering...");
dialog.setMessage("Please wait while we adding your credentials");
dialog.show();
dialog.setCanceledOnTouchOutside(false);
Call<Users> call = apiInterface.performEmailRegistration(user_name,user_email,user_password);
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if(response.body().getResponse().equals("ok")){
Toast.makeText(EmailRegisterActivity.this, "Your account has been created", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("failed!")){
Toast.makeText(EmailRegisterActivity.this, "Something went wrong, Please try again!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("already")){
Toast.makeText(EmailRegisterActivity.this, "This email is already exists, Please try another email", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
}
});
}
}
结果只有 运行 个对话框,没有关闭对话框:
如果您有任何想法或建议,我将不胜感激。
谢谢
刚刚尝试了您的代码,它运行良好。
确保 onResponse
中有 'else' 子句,并关闭 onFailure
中的对话框
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if(response.body().getResponse().equals("ok")){}
else if(response.body().getResponse().equals("failed!")){}
else if(response.body().getResponse().equals("already")){}
else {
Toast.makeText(EmailRegisterActivity.this, "Do'h, I forgot to put and 'else' clause", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
Toast.makeText(EmailRegisterActivity.this, "Do'h, Failure :(", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
我不知道为什么我的对话框不能关闭。实际上,我在我的数据库中看到,我们成功获取的数据已保存在该数据库中。只是对话框关闭代码不起作用。
这是我的代码:
package com.app.kfstore.EmailLoginRegister;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.app.kfstore.MainActivity;
import com.app.kfstore.OperationRetrofitApi.ApiClient;
import com.app.kfstore.OperationRetrofitApi.ApiInterface;
import com.app.kfstore.OperationRetrofitApi.Users;
import com.app.kfstore.R;
import com.blogspot.atifsoftwares.animatoolib.Animatoo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class EmailRegisterActivity extends AppCompatActivity {
private EditText name,email,password;
private Button regBtn;
public static ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_register);
//////hide status bar code//////
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//////end code for hide status bar//////
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
init();
}
private void init() {
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
regBtn = (Button) findViewById(R.id.button2);
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Registration();
}
});
}
private void Registration() {
String user_name = name.getText().toString().trim();
String user_email = email.getText().toString().trim();
String user_password = password.getText().toString().trim();
if(TextUtils.isEmpty(user_name)){
name.setError("Name is required!");
}
else if(TextUtils.isEmpty(user_email)){
email.setError("Email is required!");
}
else if(TextUtils.isEmpty(user_password)){
password.setError("Password is required!");
}
else {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Registering...");
dialog.setMessage("Please wait while we adding your credentials");
dialog.show();
dialog.setCanceledOnTouchOutside(false);
Call<Users> call = apiInterface.performEmailRegistration(user_name,user_email,user_password);
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if(response.body().getResponse().equals("ok")){
Toast.makeText(EmailRegisterActivity.this, "Your account has been created", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("failed!")){
Toast.makeText(EmailRegisterActivity.this, "Something went wrong, Please try again!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("already")){
Toast.makeText(EmailRegisterActivity.this, "This email is already exists, Please try another email", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
}
});
}
}
//////link code for text go to login page//////
public void goToLogin(View view) {
Intent intent = new Intent(EmailRegisterActivity.this, EmailLoginActivity.class);
startActivity(intent);
Animatoo.animateSlideRight(this);
finish();
}
///////end of code//////
//////link code for back button go to main page//////
public void goToMainPage(View view) {
Intent intent = new Intent(EmailRegisterActivity.this, MainActivity.class);
startActivity(intent);
Animatoo.animateSlideRight(this);
finish();
}
//////end of code//////
}
这是为了便于查看代码(只是相同的代码,但我只是剪切了顶部和底部的代码)
else {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Registering...");
dialog.setMessage("Please wait while we adding your credentials");
dialog.show();
dialog.setCanceledOnTouchOutside(false);
Call<Users> call = apiInterface.performEmailRegistration(user_name,user_email,user_password);
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if(response.body().getResponse().equals("ok")){
Toast.makeText(EmailRegisterActivity.this, "Your account has been created", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("failed!")){
Toast.makeText(EmailRegisterActivity.this, "Something went wrong, Please try again!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals("already")){
Toast.makeText(EmailRegisterActivity.this, "This email is already exists, Please try another email", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
}
});
}
}
结果只有 运行 个对话框,没有关闭对话框:
如果您有任何想法或建议,我将不胜感激。 谢谢
刚刚尝试了您的代码,它运行良好。
确保 onResponse
中有 'else' 子句,并关闭 onFailure
中的对话框
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if(response.body().getResponse().equals("ok")){}
else if(response.body().getResponse().equals("failed!")){}
else if(response.body().getResponse().equals("already")){}
else {
Toast.makeText(EmailRegisterActivity.this, "Do'h, I forgot to put and 'else' clause", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
Toast.makeText(EmailRegisterActivity.this, "Do'h, Failure :(", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}