如何使当点击AlertDialog中的按钮时,AlertDialog不被dismiss掉

//通过反射来实现 

@Override 

    protected Dialog onCreateDialog(int id) { 

        final AlertDialog.Builder builder=new AlertDialog.Builder(this); 

        if(id==1){ 

            builder.setTitle("标题"); 

            builder.setMessage("消息"); 

            final EditText et = new EditText(this); 

            et.setWidth(LayoutParams.FILL_PARENT); 

            et.setPadding(5, 5 ,5, 5); 

            et.setTextSize(18); 

            et.setHeight(160); 

            et.setGravity(Gravity.TOP); 

            builder.setView(et); 

            final AlertDialog ad = builder.create(); 

            ad.setButton("确定", new DialogInterface.OnClickListener(){ 

                @Override 

                public void onClick(DialogInterface dialog, int which) { 

                    if(et.getText().toString().trim().length()==0){ 

                        try { 

                            Field field = ad.getClass().getSuperclass() 

                                    .getDeclaredField("mShowing"); 

                            field.setAccessible(true); 

                            field.set(ad, false); 

                        } catch (Exception e) { 

                            e.printStackTrace(); 

                        } 

                    } 

                }}); 

            ad.setButton2("取消",new DialogInterface.OnClickListener(){ 

                @Override 

                public void onClick(DialogInterface dialog, int which) { 

                    try { 

                        Field field = ad.getClass().getSuperclass() 

                                .getDeclaredField("mShowing"); 

                        field.setAccessible(true); 

                        field.set(ad, true); 

                    } catch (Exception e) { 

                        e.printStackTrace(); 

                    } 

                }}); 



            return ad; 

        } 

        return super.onCreateDialog(id); 

    }

你可能感兴趣的:(AlertDialog)