英文:
Can't dismiss dialog in Android
问题
我不知道为什么我的对话框无法被关闭。实际上,我在数据库中看到,我们成功保存了获取的数据。只是对话框的关闭代码没有起作用。
这是我的代码:
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) {
}
});
}
只运行对话框而不关闭对话框的结果:
如果您有任何想法或建议,我真的很感激。谢谢。
英文:
I don't know why my dialog can not be dismissed. Actually, I see in my database, the data we take successfully gets saved in that database. Just the dialog dismiss code is not working.
This is my code:
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//////
}
This for easy code to see( just same code but i just cut the top and bottom 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) {
}
});
}
}
Result just run dialog without dismiss dialog:
If you have any ideas or advice, I realy apreciate it.
Thanks
答案1
得分: 0
刚刚尝试了你的代码,它完美地运行了。
确保在 `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, "噢,我忘记加上 'else' 子句了", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
Toast.makeText(EmailRegisterActivity.this, "噢,失败了 :(", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
英文:
Just tried your code, and it works perfectly.
Make sure you have an 'else' clause in your onResponse
, and dismiss the dialog in onFailure
as well
@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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论