You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android自定义AlertDialog中自定义按钮失效,求点击关闭实现方案

Fixing Custom Dialog Button Click to Close the Dialog

Let's get your custom dialog button working properly. There are a few small gaps in your current code that are preventing the button from dismissing the dialog—let's fix them step by step:

Key Issues in Your Current Code

  • You aren't storing the created AlertDialog instance, so you can't call dismiss() on it later
  • You haven't referenced the custom button from your activity_dialog layout to set a click listener
  • You forgot to update the firstStart preference to false, so the dialog will keep popping up every time the app launches

Modified Working Code

public class MainActivity extends AppCompatActivity { 
    private TextView regis; 
    public Button bt_ok; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        bt_ok=findViewById(R.id.bt_ok); 
        getSupportActionBar().hide(); 

        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE); 
        boolean firstStart = prefs.getBoolean("firstStart", true); 

        if (firstStart) { 
            showStartDialog(); 
        } 
    } 

    public void showStartDialog() { 
        // Inflate your custom dialog layout
        View customLayout = getLayoutInflater().inflate(R.layout.activity_dialog, null); 

        // Build the dialog and store the instance
        AlertDialog dialog = new AlertDialog.Builder(MainActivity.this, R.style.DialogCustomTheme)
                .setView(customLayout)
                .setCancelable(false)
                .create(); 

        // Find your custom button from the dialog layout (replace R.id.your_custom_button with actual ID)
        Button customDialogButton = customLayout.findViewById(R.id.your_custom_button); 

        // Set click listener to dismiss the dialog
        customDialogButton.setOnClickListener(v -> {
            dialog.dismiss(); 
            // Update the preference so the dialog doesn't show again
            SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE); 
            SharedPreferences.Editor editor = prefs.edit(); 
            editor.putBoolean("firstStart", false); 
            editor.apply(); 
        }); 

        // Show the dialog
        dialog.show(); 
    } 
}

Important Notes

  1. Replace R.id.your_custom_button: Make sure to use the actual ID of the button in your activity_dialog.xml layout file.
  2. Saving firstStart: We now set firstStart to false when the button is clicked, so the dialog only appears on the first app launch.
  3. Storing the Dialog Instance: By storing the AlertDialog object when we create it, we can call dismiss() directly from the button's click listener to close the dialog.

That's it—your custom button should now close the dialog properly when tapped!

内容的提问来源于stack exchange,提问作者Teja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:12:56