android showDialog用法

protected Dialog onCreateDialog(int id) {  
        // TODO Auto-generated method stub  
        switch(id){  
        case 10:  
            return new AlertDialog.Builder(Activity13.this)  
            .setTitle(getString(R.string.title)).setMessage(  
                    getString(R.string.timeout)).setPositiveButton(  
                    getString(R.string.ok),  
                    new DialogInterface.OnClickListener() {  
                        public void onClick(DialogInterface dialog,  
                                int which) {  
                        }  
                    }).create();  
      
        }  
        return super.onCreateDialog(id);  
    }  

 调用:

showDialog(10);

 

自定义的view:

 View  dialogView=(LinearLayout) getLayoutInflater().inflate(R.layout.dialog_addip,null);
        final AlertDialog.Builder builder =new AlertDialog.Builder(Demo12Activity.this);
        builder.setView(dialogView);
        showBtn = (Button)findViewById(R.id.show);
        showBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                                               //这
                builder.show();
            }
        });
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@drawable/radius_5dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:padding="12dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="20dp"
            android:textColor="#f75830"
            android:textSize="18dp"
            android:text="添加ip"/>

      
    </LinearLayout>

</LinearLayout>
原文地址:https://www.cnblogs.com/hualuoshuijia/p/6956594.html