AlertDialog 的自定义

本文介绍了一个使用Android自定义AlertDialog的示例应用。通过创建一个包含按钮的简单布局,并为这些按钮添加点击事件来演示如何展示自定义对话框。文章详细展示了如何设置对话框样式并响应用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFFFF"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;

public class CustomAlertDialogActivity extends Activity {
    /** Called when the activity is first created. */
    private Button button;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //点击弹出自定义对话框
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showCustomAlertDialog();
            }
        });
    }
    
    private void showCustomAlertDialog(){
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.show();
        Window win = alertDialog.getWindow();
        //设置自定义的对话框布局
        win.setContentView(R.layout.custom_alertdialog);
        
        //关闭对话框按钮事件
        ImageButton enterBtn = (ImageButton)win.findViewById(R.id.enter_btn);
        enterBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alertDialog.cancel();
            }
        });
        
        //退出程序
        ImageButton exitBtn = (ImageButton)win.findViewById(R.id.exit_btn);
        exitBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CustomAlertDialogActivity.this.finish();
            }
        });
    }
}


<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_margin="15dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:background="@drawable/dialog_bg">
    
    <!-- enter button -->
    <ImageButton 
        android:id="@+id/enter_btn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="15dp"
        android:layout_gravity="bottom"
        android:src="@drawable/enter_btn"/>
    
    <!-- quit button -->
    <ImageButton 
        android:id="@+id/exit_btn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="15dp"
        android:layout_gravity="bottom"
        android:src="@drawable/exit_btn"/>
    
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值