无法在安卓中关闭对话框。

huangapple go评论69阅读模式
英文:

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(&quot;Name is required!&quot;);
}
else if(TextUtils.isEmpty(user_email)){
email.setError(&quot;Email is required!&quot;);
}
else if(TextUtils.isEmpty(user_password)){
password.setError(&quot;Password is required!&quot;);
}
else {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle(&quot;Registering...&quot;);
dialog.setMessage(&quot;Please wait while we adding your credentials&quot;);
dialog.show();
dialog.setCanceledOnTouchOutside(false);
Call&lt;Users&gt; call = apiInterface.performEmailRegistration(user_name,user_email,user_password);
call.enqueue(new Callback&lt;Users&gt;() {
@Override
public void onResponse(Call&lt;Users&gt; call, Response&lt;Users&gt; response) {
if(response.body().getResponse().equals(&quot;ok&quot;)){
Toast.makeText(EmailRegisterActivity.this, &quot;Your account has been created&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals(&quot;failed!&quot;)){
Toast.makeText(EmailRegisterActivity.this, &quot;Something went wrong, Please try again!&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals(&quot;already&quot;)){
Toast.makeText(EmailRegisterActivity.this, &quot;This email is already exists, Please try another email&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call&lt;Users&gt; 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(&quot;Registering...&quot;);
dialog.setMessage(&quot;Please wait while we adding your credentials&quot;);
dialog.show();
dialog.setCanceledOnTouchOutside(false);
Call&lt;Users&gt; call = apiInterface.performEmailRegistration(user_name,user_email,user_password);
call.enqueue(new Callback&lt;Users&gt;() {
@Override
public void onResponse(Call&lt;Users&gt; call, Response&lt;Users&gt; response) {
if(response.body().getResponse().equals(&quot;ok&quot;)){
Toast.makeText(EmailRegisterActivity.this, &quot;Your account has been created&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals(&quot;failed!&quot;)){
Toast.makeText(EmailRegisterActivity.this, &quot;Something went wrong, Please try again!&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
else if(response.body().getResponse().equals(&quot;already&quot;)){
Toast.makeText(EmailRegisterActivity.this, &quot;This email is already exists, Please try another email&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call&lt;Users&gt; 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&lt;Users&gt; call, Response&lt;Users&gt; response) {
if(response.body().getResponse().equals(&quot;ok&quot;)){}
else if(response.body().getResponse().equals(&quot;failed!&quot;)){}
else if(response.body().getResponse().equals(&quot;already&quot;)){}
else {
Toast.makeText(EmailRegisterActivity.this, &quot;Do&#39;h, I forgot to put and &#39;else&#39; clause&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
@Override
public void onFailure(Call&lt;Users&gt; call, Throwable t) {
Toast.makeText(EmailRegisterActivity.this, &quot;Do&#39;h, Failure :(&quot;, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}

huangapple
  • 本文由 发表于 2020年9月14日 16:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63880869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定