Android中Dialog的使用

直接看代码:

public class ThirdActivity extends Activity{
    private Button but , but1 ;
    private AlertDialog dialog ;
    private AlertDialog.Builder builder;
    private ProgressDialog progressDialog ;
    private SexDialog dialogBottom ;
    private PopupWindow popupWindow ;
    private View popupWindowView ;
    private WindowManager.LayoutParams lp ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.thirdlayout);
        but = (Button) findViewById( R.id.but3);
        but1 = (Button) findViewById( R.id.but33);
        initDialog(); //普通对话框
        initItems();  //列表对话框
        initSingleChoice(); //单选项对话框
        initSMultiChoice();  //复选对话框
        initProgressDialog(); //进度条对话框
        showSexDialogBottom(); //自定义对话框(继承Dialog 自写样式)
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
                dialogBottom.show();
                progressDialog.show();
                dialogBottom.show();
            }
        });
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else{
                    backgroundAlpha(ThirdActivity.this , 0.5f);
                    popupWindow.showAsDropDown(v);
                }
            }
        });
    }

    /**
     * 普通对话框
     */
    private void initDialog(){
        dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("系统提示");
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.setMessage("最基本的对话框学习");
        dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(ThirdActivity.this , "取消" , Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(ThirdActivity.this , "中立" , Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(ThirdActivity.this , "确认" , Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 列表对话框
     */
    private void initItems(){
        String[] strings =  new String[]{"选项一" , "选项二" ,"选项三","选项四" };
        builder = new AlertDialog.Builder(this);
        builder.setTitle("系统提示");
        builder.setIcon(R.mipmap.ic_launcher);
        //builder.setMessage("最基本的对话框学习");//这时候这个就不能设置了,否侧显示的这个,列表不能显示了
        builder.setItems(strings, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0 :
                        Toast.makeText(ThirdActivity.this , "选项一" , Toast.LENGTH_SHORT).show();
                        break;
                    case 1 :
                        Toast.makeText(ThirdActivity.this , "选项二" , Toast.LENGTH_SHORT).show();
                        break;
                    case 2 :
                        Toast.makeText(ThirdActivity.this , "选项三" , Toast.LENGTH_SHORT).show();
                        break;
                    case 3 :
                        Toast.makeText(ThirdActivity.this , "选项四" , Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        dialog = builder.create();
    }

    /**
     * 单选对话框
     */
    private void initSingleChoice(){
        String[] strings =  new String[]{"选项一" , "选项二" ,"选项三","选项四" };
        builder = new AlertDialog.Builder(this);
        builder.setTitle("系统提示");
        builder.setIcon(R.mipmap.ic_launcher);
        //builder.setMessage("最基本的对话框学习");//这时候这个就不能设置了,否侧显示的这个,列表不能显示了
        builder.setSingleChoiceItems(strings, 1, new DialogInterface.OnClickListener() { //第二个int参数 代表一开始选中的第几项
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0 :
                        Toast.makeText(ThirdActivity.this , "选项一" , Toast.LENGTH_SHORT).show();
                        break;
                    case 1 :
                        Toast.makeText(ThirdActivity.this , "选项二" , Toast.LENGTH_SHORT).show();
                        break;
                    case 2 :
                        Toast.makeText(ThirdActivity.this , "选项三" , Toast.LENGTH_SHORT).show();
                        break;
                    case 3 :
                        Toast.makeText(ThirdActivity.this , "选项四" , Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
    }
    /**
     * 多选对话框
     */
    private void initSMultiChoice(){
        String[] strings =  new String[]{"选项一" , "选项二" ,"选项三","选项四" };
        boolean[] booleen = new boolean[]{false ,false ,false, false};
        builder = new AlertDialog.Builder(this);
        builder.setTitle("系统提示");
        builder.setIcon(R.mipmap.ic_launcher);
        //builder.setMessage("最基本的对话框学习");//这时候这个就不能设置了,否侧显示的这个,列表不能显示了
        builder.setMultiChoiceItems(strings, booleen, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                switch (which){
                    case 0 :
                        Toast.makeText(ThirdActivity.this , "选项一" , Toast.LENGTH_SHORT).show();
                        break;
                    case 1 :
                        Toast.makeText(ThirdActivity.this , "选项二" , Toast.LENGTH_SHORT).show();
                        break;
                    case 2 :
                        Toast.makeText(ThirdActivity.this , "选项三" , Toast.LENGTH_SHORT).show();
                        break;
                    case 3 :
                        Toast.makeText(ThirdActivity.this , "选项四" , Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
    }

    /**
     * 进度条对话框
     */
    private void initProgressDialog(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("正在加载,请稍等。。。");
        progressDialog.setIcon( R.mipmap.ic_launcher);
        progressDialog.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                progressDialog.dismiss();
            }
        });
    }

    /**
     * 自定义对话框样式
     * @param
     */
    private void showSexDialogBottom() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_sex_dialogfragment, null);
        dialogBottom = new SexDialog(this, contentView, R.style.sex_dialog);
    }
}

继承Dialog类:(自定义对话框)

public class SexDialog extends Dialog {

    private View content ;
    private Context context ;

    public SexDialog(@NonNull Context context) {
        super(context);
    }

    public SexDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected SexDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public SexDialog(Context context , View view , int themeResId){
        super(context, themeResId);
        this.context = context ;
        this.content = view ;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        this.setContentView(content);
        Window window = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        WindowManager.LayoutParams lp = window.getAttributes();  // 获取对话框当前的参数值
        lp.gravity = Gravity.BOTTOM;                             //位于底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        lp.width = (int) (d.getWidth() * 0.8);   // 宽度设置为屏幕的0.8,根据实际情况调整
//        lp.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.5,根据实际情况调整
        window.setAttributes(lp);
    }
}

style:

<style name="sex_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFrame">@null</item>  
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowBackground">@color/transparent</item>
</style>

<style name="line_style">
    <item name="android:layout_width">match_parent</item>
    <item name="android:background">@color/new_line_bg</item>
    <item name="android:layout_height">0.1dp</item>
</style>

就不上结果了。常见的对话框使用就这个么几种。











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值