How to prevent a dialog from closing when a button is clicked

后端 未结 18 1865
无人及你
无人及你 2020-11-21 23:59

I have a dialog with EditText for input. When I click the \"yes\" button on dialog, it will validate the input and then close the dialog. However, if the input

18条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 00:45

    To prevent Dialog box from closing when clicked and it should only close when the internet is available

    I am trying to do the same thing, as I don't want the dialog box to be closed until and unless the internet is connected.

    Here is my code:

    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("Internet Not Connected");
        if(ifConnected()){
    
            Toast.makeText(this, "Connected or not", Toast.LENGTH_LONG).show();
        }
        else{
            builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                   if(!ifConnected())
                   {
                       builder.show();
                   }
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
            builder.show();
    
        }
    

    And here is my Connectivity manager code:

     private boolean ifConnected()
    {
        ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
       return networkInfo!=null && networkInfo.isConnected();
    }
    

提交回复
热议问题