对话框后退键已禁用

时间:2019-03-01 18:14:36

标签: java android dialog android-alertdialog

我正在使用该应用程序,并且卡在一个位置上。我正在使用自定义对话框来显示消息。当我的对话框弹出以显示消息时,我将活动上显示的所有视图设置为“不可见”,因此当我的对话框打开时它看起来很好,没有背景视图。取消对话框的一种方法是单击“确定”按钮,该按钮会将活动中显示的视图设置回“可见”状态。 问题,我遇到的问题是,如果用户按下后退按钮而不是“确定”按钮,那将导致对话框尿布,但视图仍处于隐藏状态。我试图禁用“后退”按钮,但是它不起作用。如果用户按下后退按钮两次而不是一次,则可以正常工作。

Image before the dialog box appears

Image when the dialog box appears

Image when user click back button instead of OK

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alexeducationchina.alexeducation.App;
import com.alexeducationchina.alexeducation.R;
import com.alexeducationchina.alexeducation.UserAgreement.UserAgreement;
import com.backendless.Backendless;
import com.backendless.BackendlessUser;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;

public class Register extends AppCompatActivity implements View.OnClickListener {

    private TextView memberRegister ;
    private EditText firstName , lastName , email , password ;
    private Button register ;
    SharedPreferences sharedPreferences ;
    ValidateInformation validateInformation ;
    private CardView cardFirstName , cardLastName , cardEmail , cardPassword , cardRegister ;
    private ImageView userImage ;




    private void linkWithXml () {

        memberRegister = (TextView) findViewById(R.id.memberRegister);
        firstName = (EditText) findViewById(R.id.firstName);
        lastName = (EditText) findViewById(R.id.lastName);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);
        register = (Button) findViewById(R.id.register);


        cardFirstName =(CardView) findViewById(R.id.FNC);
        cardLastName =(CardView) findViewById(R.id.LNC);
        cardEmail =(CardView) findViewById(R.id.EC);
        cardPassword =(CardView) findViewById(R.id.PC);
        cardRegister =(CardView) findViewById(R.id.RC);

        userImage = (ImageView) findViewById(R.id.userImage);


    }

    private void settingFonts() {

        firstName.setTypeface(App.getAppInstance().getRegularFont());
        lastName.setTypeface(App.getAppInstance().getRegularFont());
        email.setTypeface(App.getAppInstance().getRegularFont());
        password.setTypeface(App.getAppInstance().getRegularFont());
        memberRegister.setTypeface(App.getAppInstance().getBoldFont());
        register.setTypeface(App.getAppInstance().getBoldFont());

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        linkWithXml();
        settingFonts();
        register.setOnClickListener(this);
        App.getAppInstance().initApp();

    }







       /*



*/



       private void hideViews () {

           userImage.setVisibility(View.INVISIBLE);
           memberRegister.setVisibility(View.INVISIBLE);
           cardFirstName.setVisibility(View.INVISIBLE);
           cardLastName.setVisibility(View.INVISIBLE);
           cardEmail.setVisibility(View.INVISIBLE);
           cardPassword.setVisibility(View.INVISIBLE);
           cardRegister.setVisibility(View.INVISIBLE);


       }

    @Override
    public void onClick(View v) {

        if (v.getId()==R.id.register){


            hideViews();
            validateInformation = new ValidateInformation(Register.this , "REGISTER");
            validateInformation.register(firstName , lastName , email , password);



       //     startActivity(new Intent(Register.this , UserAgreement.class));

        }


    }

    private void showViews () {

        userImage.setVisibility(View.VISIBLE);
        memberRegister.setVisibility(View.VISIBLE);
        cardFirstName.setVisibility(View.VISIBLE);
        cardLastName.setVisibility(View.VISIBLE);
        cardEmail.setVisibility(View.VISIBLE);
        cardPassword.setVisibility(View.VISIBLE);
        cardRegister.setVisibility(View.VISIBLE);


    }


}


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.Image;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alexeducationchina.alexeducation.App;
import com.alexeducationchina.alexeducation.R;
import com.alexeducationchina.alexeducation.UserAgreement.UserAgreement;
import com.backendless.Backendless;
import com.backendless.BackendlessUser;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;

public class ValidateInformation {


    Context context ;
    ImageView ui ;
    TextView rt ;
    CardView fn ;
    CardView ln;
    CardView e ;
    CardView p ;
    CardView r ;
    ProgressBar progressBar ;
    TextView notice ;
    LinearLayout linearLayout ;

    private static String userOk = "NOT" ;
    ValidateInformation(Context context , String activityName){

           this.context = context ;
        progressBar = new ProgressBar(context , null , R.style.ProgressBarTheme);
           if (activityName.equals("REGISTER"))
           {
         ui = (ImageView) ((Activity)context).findViewById(R.id.userImage);
         rt = (TextView) ((Activity)context).findViewById(R.id.memberRegister);
         fn = (CardView) ((Activity)context).findViewById(R.id.FNC);
          ln = (CardView) ((Activity)context).findViewById(R.id.LNC);
         e = (CardView) ((Activity)context).findViewById(R.id.EC);
         p = (CardView) ((Activity)context).findViewById(R.id.PC);
        r = (CardView) ((Activity)context).findViewById(R.id.RC);
        progressBar = (ProgressBar) ((Activity)context).findViewById(R.id.progressBar);
           }
    }


    public void register(EditText firstName , EditText lastName, EditText email , EditText password){


        String FIRST_NAME = firstName.getText().toString().trim();
        String LAST_NAME = lastName.getText().toString().trim() ;
        String EMAIL = email.getText().toString().trim() ;
        String PASSWORD = password.getText().toString().trim() ;

        String emptyFirstName = "Enter Your First Name";
        String emptyLastName = "Enter Your Last Name";
        String emptyEmail = "Enter Your Email Address" ;
        String emptyPassword = "Enter Your Password";
        String allEmpty = "All The Fields Are Empty";
        if (TextUtils.isEmpty(FIRST_NAME) && TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {

           showMessage(allEmpty);

        }



        else if (!TextUtils.isEmpty(FIRST_NAME)&& TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {
            showMessage(emptyLastName+"\n"+emptyEmail +"\n"+emptyPassword);

        }
        else if (TextUtils.isEmpty(FIRST_NAME)&& !TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {
            showMessage(emptyFirstName+"\n"+emptyEmail +"\n"+emptyPassword);


        }
        else if (TextUtils.isEmpty(FIRST_NAME)&& TextUtils.isEmpty(LAST_NAME) && !TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {
            showMessage(emptyFirstName+"\n"+emptyLastName +"\n"+emptyPassword);


        }
        else if (TextUtils.isEmpty(FIRST_NAME)&& TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && !TextUtils.isEmpty(PASSWORD))
        {
            showMessage(emptyFirstName+"\n"+emptyLastName +"\n"+emptyEmail);


        }




        else if (!TextUtils.isEmpty(FIRST_NAME)&& !TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {

            showMessage(emptyEmail +"\n"+emptyPassword  );

        }
        else if (TextUtils.isEmpty(FIRST_NAME)&& TextUtils.isEmpty(LAST_NAME) && !TextUtils.isEmpty(EMAIL) && !TextUtils.isEmpty(PASSWORD))
        {

            showMessage(emptyFirstName+"\n"+emptyLastName  );
        }



        else if (!TextUtils.isEmpty(FIRST_NAME)&& TextUtils.isEmpty(LAST_NAME) && !TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {
            showMessage(emptyLastName+"\n"+emptyPassword  );



        }
        else if (TextUtils.isEmpty(FIRST_NAME)&& !TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && !TextUtils.isEmpty(PASSWORD))
        {

            showMessage(emptyFirstName+"\n"+emptyEmail  );
        }


        else if (!TextUtils.isEmpty(FIRST_NAME)&& TextUtils.isEmpty(LAST_NAME) && TextUtils.isEmpty(EMAIL) && !TextUtils.isEmpty(PASSWORD))
        {

            showMessage(emptyLastName+"\n"+emptyEmail  );

        }
        else if (TextUtils.isEmpty(FIRST_NAME)&& !TextUtils.isEmpty(LAST_NAME) && !TextUtils.isEmpty(EMAIL) && TextUtils.isEmpty(PASSWORD))
        {

            showMessage(emptyFirstName+"\n"+emptyPassword  );
        }


        else if (TextUtils.isEmpty(FIRST_NAME)){


            showMessage(emptyFirstName);
        }
        else if (TextUtils.isEmpty(LAST_NAME)){

            showMessage(emptyLastName);
        }
        else if (TextUtils.isEmpty(EMAIL)){
            showMessage(emptyEmail);

        }
        else if (TextUtils.isEmpty(PASSWORD)){
            showMessage(emptyPassword);

        }



        else {

            showRegisterViews();
            showProgressBar();
            hideRegisterButton();

            BackendlessUser user = new BackendlessUser();
            user.setProperty( "email", email.getText().toString().trim() );
            user.setPassword(  password.getText().toString().trim() );

            Backendless.UserService.register( user, new AsyncCallback<BackendlessUser>()
            {
                public void handleResponse( BackendlessUser registeredUser )
                {
                    hideRegisterViews();
                    hideProgressBar();
                    userOk = "OK" ;
                    showMessage("You Are Registered");
                    notice.setText("CONGRATULATION");
                    linearLayout.setBackground(ContextCompat.getDrawable(context , R.drawable.upper_rounded_congratulation));



                }

                public void handleFault( BackendlessFault fault )
                {
                    hideRegisterViews();
                    hideProgressBar();


                    switch (fault.getCode()){




                        case "2002" :

                            userOk = "NOT" ;
                            showMessage("Invalid Application Info (Application Id Or Api Key)");

                            break ;


                        case "3009" :

                            userOk = "NOT" ;
                            showMessage("User Registration Is Disabled For The Application");

                            break ;


                        case "3010" :

                            userOk = "NOT" ;
                            showMessage("User Registration Has An Unknown Property And Dynamic Properties Are Disabled For This Version Of The Application");

                            break ;


                        case "3011" :

                            userOk = "NOT" ;
                            showMessage("Missing \"password\" Property");

                            break ;


                        case "3012" :

                            userOk = "NOT" ;
                            showMessage("Required Property Is Missing");

                            break ;


                        case "3013" :

                            userOk = "NOT" ;
                            showMessage("Missing Value For The Identity Property");

                            break ;


                        case "3014" :

                            userOk = "NOT" ;
                            showMessage("External Registration Failed With An Error");

                            break ;


                        case "3021" :

                            userOk = "NOT" ;
                            showMessage("General User Registration Error. Details Included With The Error Message");

                            break ;


                        case "3033" :

                            userOk = "NOT" ;
                            showMessage("User With The Same Identity Already Exists");

                            break ;


                        case "3038" :

                            userOk = "NOT" ;
                            showMessage("Missing Application-Id Or Collection Of Properties For The Registering User");

                            break ;


                        case "3039" :

                            userOk = "NOT" ;
                            showMessage("Property \"id\" Cannot Ce Used In The Registration Call");

                            break ;


                        case "3040" :

                            userOk = "NOT" ;
                            showMessage("Email Address Is In The Wrong Format");

                            break ;


                        case "3041" :

                            userOk = "NOT" ;
                            showMessage("A Value For A Required Property Is Missing");

                            break ;




                        case "3043" :

                            userOk = "NOT" ;
                            showMessage("Duplicate Properties In The Registration Request");

                            break ;
                        case "8000" :

                            userOk = "NOT" ;
                            showMessage("Property Value Exceeds The Length Limit");

                            break ;





                    }



                }
            } );




        }
        }




        private void hideRegisterButton () {

        r.setVisibility(View.GONE);


        }
        private void hideProgressBar (){

        progressBar.setVisibility(View.INVISIBLE);
        }
        private void showProgressBar () {


        progressBar.setVisibility(View.VISIBLE);



        }

    public void showMessage(String message){



        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.validation_purpose);

        notice = (TextView) dialog.findViewById(R.id.toolbarTitle);
        TextView information = (TextView) dialog.findViewById(R.id.information);
        linearLayout = (LinearLayout) dialog.findViewById(R.id.linearlayout);


        Button ok = (Button) dialog.findViewById(R.id.ok);

        information.setTypeface(App.getAppInstance().getRegularFont());
        information.setText(message);


        notice.setTypeface(App.getAppInstance().getBoldFont());

        ok.setTypeface(App.getAppInstance().getBoldFont());

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (userOk.equals("NOT")){

                }

                dialog.dismiss();
                showRegisterViews();

                if (userOk.equals("OK")){

                    context.startActivity(new Intent(context , UserAgreement.class));
                }


            }
        });


        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT);

        dialog.show();

    }

    private void showRegisterViews(){


        ui.setVisibility(View.VISIBLE);
        rt.setVisibility(View.VISIBLE);
        fn.setVisibility(View.VISIBLE);
        ln.setVisibility(View.VISIBLE);
        e.setVisibility(View.VISIBLE);
        p.setVisibility(View.VISIBLE);
        r.setVisibility(View.VISIBLE);



    }
    private  void hideRegisterViews (){


        ui.setVisibility(View.INVISIBLE);
        rt.setVisibility(View.INVISIBLE);
        fn.setVisibility(View.INVISIBLE);
        ln.setVisibility(View.INVISIBLE);
        e.setVisibility(View.INVISIBLE);
        p.setVisibility(View.INVISIBLE);
        r.setVisibility(View.INVISIBLE);

    }




}

我想要的是,当用户单击后退按钮时,什么也不会发生,或者阻止我已经使用过的后退按钮

onBackPressed()

方法,但仅当用户轻按两次后退键时它才有效。 做了什么 当单击两次后,我将视图可见性声明为VISIBLE,然后两次按下后视图又回来了,问题是我无法返回到我以前的活动,我做过的另一种方法是声明通过声明使视图返回的方法 onRestart() 但失败了 如果有人帮助我解决此问题,我将非常感谢

1 个答案:

答案 0 :(得分:0)

解决了这个问题。这将禁用后按按钮,其作用类似于超级按钮。

dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            return keyCode == KeyEvent.KEYCODE_BACK;
        }
    });