美文网首页
Android Diglog

Android Diglog

作者: 愤怒的码农1 | 来源:发表于2018-08-21 17:02 被阅读0次

Android Diglog

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

loading.png trip1.png trip2.png

关键代码:

//DialogStyle
 <style name="wait_dlg">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:maxHeight">@dimen/dlg_max_height</item>
        <item name="android:width">@dimen/showdlg_whidth</item>
        <item name="android:layout_width">@dimen/showdlg_whidth</item>
        <item name="android:minHeight">@dimen/dlg_min_height</item>
    </style>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 用于设置信息对话框的圆角 -->
        <corners android:radius="@dimen/dp12"></corners>
        <solid android:color="@color/white"></solid>
</shape>
//等待框
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

      本文链接:https://www.haomeiwen.com/subject/qsvobftx.html