📜  如何在 Android 中制作自定义退出对话框?

📅  最后修改于: 2022-05-13 01:55:09.924000             🧑  作者: Mango

如何在 Android 中制作自定义退出对话框?

在本教程中,我们将在 Android 中创建一个自定义退出对话框。默认情况下,android 不提供任何退出对话框,但我们可以使用Java的对话框类创建它。但是大多数开发人员和用户不喜欢默认对话框,我们也无法根据需要对对话框进行任何修改,因此在本文中,我们将创建一个简单的自定义退出对话框。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

在 Android 示例 GIF 中制作自定义退出对话框

方法

步骤 1:创建新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,尽管我们将使用Java语言实现此项目,但选择Java作为语言。

第 2 步:在进入编码部分之前先做一些预任务



转到app -> res -> values -> colors.xml文件并设置应用程序的颜色。

XML


  
   #0F9D58
   #0F9D58
   #05af9b
     


XML


    
    


XML


  
    
  


XML


      
    
          
        
        
          
        
        
  
        
  
            
            
  
            
            
              
        
          
    
  


Java
public  void customExitDialog()
    {
        // creating custom dialog
        final Dialog dialog = new Dialog(MainActivity.this);
  
        // setting content view to dialog
        dialog.setContentView(R.layout.custom_exit_dialog);
  
        // getting reference of TextView
        TextView dialogButtonYes = (TextView) dialog.findViewById(R.id.textViewYes);
        TextView dialogButtonNo = (TextView) dialog.findViewById(R.id.textViewNo);
  
        // click listener for No
        dialogButtonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dismiss the dialog
                dialog.dismiss();
  
            }
        });
        // click listener for Yes
        dialogButtonYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dismiss the dialog and exit the exit
                dialog.dismiss();
                finish();
  
            }
        });
  
        // show the exit dialog
        dialog.show();
}


Java
@Override
public void onBackPressed() {
        // calling the function
        customExitDialog();
}


Java
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
    public void customExitDialog() {
        // creating custom dialog
        final Dialog dialog = new Dialog(MainActivity.this);
  
        // setting content view to dialog
        dialog.setContentView(R.layout.custom_exit_dialog);
  
        // getting reference of TextView
        TextView dialogButtonYes = (TextView) dialog.findViewById(R.id.textViewYes);
        TextView dialogButtonNo = (TextView) dialog.findViewById(R.id.textViewNo);
  
        // click listener for No
        dialogButtonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //dismiss the dialog
                dialog.dismiss();
  
            }
        });
          
        // click listener for Yes
        dialogButtonYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dismiss the dialog
                // and exit the exit
                dialog.dismiss();
                finish();
  
            }
        });
  
        // show the exit dialog
        dialog.show();
    }
  
    @Override
    public void onBackPressed() {
        // calling the function
        customExitDialog();
    }
}


我们还创建了一个新的 drawable 文件( card_round.xml ),并参考 elasq、flaticon 作为警报图标,并将其粘贴到 drawable 文件夹中。 card_round.xml代码如下所示

XML



    
    

第 3 步:设计 UI

activity_main.xml包含一个默认文本,我们将文本更改为“ Press back to exit ”,如下所示

XML



  
    
  

现在我们在其中创建一个新的布局资源文件( custom_exit_dialog.xml ),我们添加一个 ImageView、TextView 和 LinearLayout,如下所示



XML



      
    
          
        
        
          
        
        
  
        
  
            
            
  
            
            
              
        
          
    
  

第 4 步:编码部分

现在打开MainActivity。类中有Java文件,首先创建函数public void customExitDialog()如下图

Java

public  void customExitDialog()
    {
        // creating custom dialog
        final Dialog dialog = new Dialog(MainActivity.this);
  
        // setting content view to dialog
        dialog.setContentView(R.layout.custom_exit_dialog);
  
        // getting reference of TextView
        TextView dialogButtonYes = (TextView) dialog.findViewById(R.id.textViewYes);
        TextView dialogButtonNo = (TextView) dialog.findViewById(R.id.textViewNo);
  
        // click listener for No
        dialogButtonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dismiss the dialog
                dialog.dismiss();
  
            }
        });
        // click listener for Yes
        dialogButtonYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dismiss the dialog and exit the exit
                dialog.dismiss();
                finish();
  
            }
        });
  
        // show the exit dialog
        dialog.show();
}

现在我们所说的onBackPressed()customExitDialog()方法,如下图所示

Java

@Override
public void onBackPressed() {
        // calling the function
        customExitDialog();
}

下面是MainActivity的完整代码。 Java文件。

Java

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
    public void customExitDialog() {
        // creating custom dialog
        final Dialog dialog = new Dialog(MainActivity.this);
  
        // setting content view to dialog
        dialog.setContentView(R.layout.custom_exit_dialog);
  
        // getting reference of TextView
        TextView dialogButtonYes = (TextView) dialog.findViewById(R.id.textViewYes);
        TextView dialogButtonNo = (TextView) dialog.findViewById(R.id.textViewNo);
  
        // click listener for No
        dialogButtonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //dismiss the dialog
                dialog.dismiss();
  
            }
        });
          
        // click listener for Yes
        dialogButtonYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dismiss the dialog
                // and exit the exit
                dialog.dismiss();
                finish();
  
            }
        });
  
        // show the exit dialog
        dialog.show();
    }
  
    @Override
    public void onBackPressed() {
        // calling the function
        customExitDialog();
    }
}

输出: