Android Diglog

Android Diglog

Andorid 等待框,询问框是在开发非常普遍的,现在先放几张图,我们大概需要实现什么样的样式,我们一步一步实现,然后再做适合我们自己Dialog

loading.png
trip1.png
trip2.png

关键代码:

//DialogStyle
 

    
        
        

//等待框
public class WaitDialog {
    private Dialog dlg,dlg2;
    private Context mContext;
    private TextView tv2;

    public WaitDialog(Context context){
      mContext=context;
    }

    public void waitDlg(String text){
        if (dlg!=null) {
            if (!dlg.isShowing()) {
                dlg.show();
            }
        }else {
            dlg = new Dialog(mContext, R.style.wait_dlg);
            dlg.setCancelable(false);
            View view = LayoutInflater.from(mContext).inflate(R.layout.dlg_wait, null);
            TextView tvWait = (TextView) view.findViewById(R.id.dlg_wait);
            tvWait.setText(text);
            dlg.setContentView(view);
            dlg.show();
            init(dlg);
        }
    }

    public void dlgDimss(){
        if (dlg!=null&&dlg.isShowing()) {
            dlg.dismiss();
        }
        if (dlg2!=null&&dlg2.isShowing()){
            dlg2.dismiss();
        }
    }

    public void waitDlg2(String text){
        if (dlg2!=null) {
            tv2.setText(text);
            if (!dlg2.isShowing()) {
                dlg2.show();
            }
        }else {
            dlg2 = new Dialog(mContext, R.style.wait_dlgtext);
            dlg2.setCancelable(false);
            View view = LayoutInflater.from(mContext).inflate(R.layout.dlg_wait2, null);
            tv2 = (TextView) view.findViewById(R.id.dlg_wait);
            tv2.setText(text);
            dlg2.setContentView(view);
            dlg2.show();
            init(dlg2);

        }
    }

    private void init(Dialog dlg){
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int displayWidth = dm.widthPixels;
        android.view.WindowManager.LayoutParams p = dlg.getWindow().getAttributes();
        p.width = (int) (displayWidth * 0.35);
        p.height = (int) (displayWidth * 0.35);
        dlg.setCanceledOnTouchOutside(false);
        dlg.getWindow().setAttributes(p);
    }
}
//提示框
public class TripDialog  {
    private static Dialog dlg,dlg2;
    private Context mContext;
    public TripDialog(Context context){
        mContext=context;
    }

    private static void init(Context context,Dialog dlg){
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int displayWidth = dm.widthPixels;
        android.view.WindowManager.LayoutParams p = dlg.getWindow().getAttributes();
        p.width = (int) (displayWidth * 0.55);
        dlg.setCanceledOnTouchOutside(false);
        dlg.getWindow().setAttributes(p);
    }

    public   static  void  show(Context context,String text){
        View view = LayoutInflater.from(context).inflate(R.layout.dlg_trip, null);
        TextView tv_trip = (TextView) view.findViewById(R.id.tv_trip);
        TextView tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        tv_trip.setText(text);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlgDimss();
            }
        });
        if (dlg!=null) {
            if (!dlg.isShowing()) {
                dlg.setCancelable(false);
                dlg.setContentView(view);
                dlg.show();
            }
        }else {
            dlg = new Dialog(context, R.style.wait_dlgtext);
            dlg.setCancelable(false);
            dlg.setContentView(view);
            dlg.show();
            init(context,dlg);
        }
    }

    public static void show2(Context context,String text, final DiaListener dlgSure){
        View view = LayoutInflater.from(context).inflate(R.layout.dlg_trip2, null);
        TextView tv_trip = (TextView) view.findViewById(R.id.tv_trip);
        TextView tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_trip.setText(text);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlgSure.diaSure();
                dlgDimss();
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlgDimss();
            }
        });
        if (dlg2!=null) {
            if (!dlg2.isShowing()) {
                dlg2.setContentView(view);
                dlg2.show();
            }
        }else {
            dlg2 = new Dialog(context, R.style.wait_dlgtext);
            dlg2.setCancelable(false);
            dlg2.setContentView(view);
            dlg2.show();
            init(context,dlg2);
        }
    }

    public static void dlgDimss(){
        if (dlg!=null&&dlg.isShowing()) {
            dlg.dismiss();
        }
        if (dlg2!=null&&dlg2.isShowing()) {
            dlg2.dismiss();
        }
    }

    public interface DiaListener{
        void diaSure();
    }
}

你可能感兴趣的:(Android Diglog)