Android 自定义 Dialog方法总结

本文介绍两种在Android应用中创建自定义对话框的方法。一种是通过AlertDialog.Builder创建并设置内容视图,另一种是通过继承Dialog类来实现更灵活的自定义选项。这两种方法都详细展示了如何设置对话框的布局、按钮响应及基本交互。

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

方法一:

在activity中

AlertDialog myDialog = new AlertDialog.Builder(this).create();

myDialog.show();

//这里注意一定要先show dialog 再去加载 contentView。否则会出现异常。

myDialog.getWindow().setContentView(R.layout.main);

方法二:

继承Android 系统提供的Dialog类。 

1. 创建一个 Dialog . 

public class CustomDialog extends Dialog {  
  
    public CustomDialog(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  
      
     protected void onCreate(Bundle savedInstanceState){  
         super.onCreate(savedInstanceState);  
           
         setContentView(R.layout.custom_dialog);  
         setTitle("Custom Dialog");  
  
         TextView text = (TextView)findViewById(R.id.text);  
         text.setText("Hello, this is a custom dialog!");  
         ImageView image = (ImageView)findViewById(R.id.image);  
         image.setImageResource(R.drawable.sepurple);  
           
         Button buttonYes = (Button) findViewById(R.id.button_yes);  
         buttonYes.setHeight(5);  
         buttonYes.setOnClickListener(new Button.OnClickListener(){  
  
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
                    dismiss();  
                      
                }  
            });  
         Button buttonNo = (Button) findViewById(R.id.button_no);  
         buttonNo.setSingleLine(true);  
         buttonNo.setOnClickListener(new Button.OnClickListener(){  
  
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
                    dismiss();  
                      
                }  
            });  
     }  
       
     //called when this dialog is dismissed  
     protected void onStop() {  
         Log.d("TAG","+++++++++++++++++++++++++++");  
     }  
}

2. Dialog 的使用: 

public class CustomDialogUsage extends Activity {  
    CustomDialog cd;  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        cd = new CustomDialog(this);  
          
          
        Button buttonYes = (Button) findViewById(R.id.main_button);  
        buttonYes.setOnClickListener(new OnClickListener(){  
  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                cd.show();  
                  
            }  
        });    
    }       
}    

3. Dialog 的界面定制. 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:id="@+id/layout_root"  
              android:orientation="horizontal"  
              android:layout_width="fill_parent"  
              android:layout_height="fill_parent"  
              android:padding="10dp"  
              >  
    <ImageView android:id="@+id/image"  
               android:layout_width="wrap_content"  
               android:layout_height="fill_parent"  
               android:layout_marginRight="10dp"  
               />  
    <LinearLayout   
        android:orientation="vertical"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"  
        android:padding="5px"   
        >  
    <TextView android:id="@+id/text"  
              android:layout_width="wrap_content"  
              android:layout_height="fill_parent"  
              android:textColor="#FFF"  
              />  
    <LinearLayout   
        android:orientation="horizontal"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"  
        android:padding="5px"   
        >  
    <Button android:id="@+id/button_yes"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content"  
              android:text=" Yes "  
              android:gravity="center"  
              />  
    <Button android:id="@+id/button_no"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content"  
              android:text=" No "  
              android:gravity="center"  
              />  
    </LinearLayout>  
    </LinearLayout>  
</LinearLayout>  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值