标签:android
对话框 在android中是一种很常见的交互提示用户的方式,但是很多产品狗都叫我们这些做android的仿ios,搞的我们android程序员很苦逼,凭什么效果老是仿ios,有没有一点情怀,不过ios在界面封装确实比android好很多,吐槽完毕,比如一种很常见的场景就是在没网的情况下 提示用户,看效果图:
在很多界面都要有提示,那么就自然而然想到了封装,而不至于在每个页面都重写一篇,话不多说直接上代码
CommonDialog.java
public class CommonDialog extends Dialog{ private Context context; private String title; private ClickListenerInterface clickListenerInterface; public CommonDialog(Context context) { super(context, R.style.MyDialogStyle); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } private void init() { LayoutInflater inflater =LayoutInflater.from(context); View view =inflater.inflate(R.layout.dialog_common, null); setContentView(view); TextView tv_ok= (TextView) view.findViewById(R.id.tv_ok); tv_ok.setOnClickListener(new clickListener()); Window dialogWindow = getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用 lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6 dialogWindow.setAttributes(lp); } public interface ClickListenerInterface { public void doConfirm(); } public void setClicklistener(ClickListenerInterface clickListenerInterface) { this.clickListenerInterface = clickListenerInterface; } private class clickListener implements View.OnClickListener { @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.tv_ok: dismiss(); break; } } }; }
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/login_bg" android:gravity="center_horizontal" android:orientation="vertical" android:layout_marginBottom="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:gravity="center_horizontal" android:padding="10dp" android:text="温馨提示" android:textColor="@color/c282828" android:textSize="18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请求超时,请确定网络是否正常" android:textSize="15sp" android:textColor="#383838" /> <View android:layout_width="match_parent" android:layout_height="0.4dp" android:background="@color/line_color01" android:layout_marginTop="10dp" /> <TextView android:id="@+id/tv_ok" android:layout_width="fill_parent" android:layout_height="30dp" android:text="确定" android:textSize="16sp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:focusable="true" android:clickable="true" android:gravity="center" android:textColor="#00B2EE" /> </LinearLayout>
<style name="MyDialogStyle"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>使用:
final CommonDialog confirmDialog = new CommonDialog(getActivity());
confirmDialog.show();
confirmDialog.setClicklistener(new CommonDialog.ClickListenerInterface() {
@Override
public void doConfirm() {}
});
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android
原文地址:http://blog.csdn.net/coderinchina/article/details/46998943