首页 > 编程知识 正文

自定义弹出框,设置自动弹出

时间:2023-05-05 20:07:09 阅读:206794 作者:2203

wndqjDiglog,然后自定义上面的内smdxgxml布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="120dp" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/tv_isSuccess" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="领取成功" android:textColor="@color/ico" android:textSize="25sp" /> <TextView android:id="@+id/tv_errorMessage" android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="优惠劵已发放到您的账户" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#999" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_coupon_close" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="关闭" android:textSize="18sp" /> <TextView android:id="@+id/tv_coupon_look" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="查看" android:textSize="18sp" /> </LinearLayout></LinearLayout>

实现类:

public class DialogCoupon implements View.OnClickListener { Activity activity; Dialog dialog; LayoutInflater inflater; private TextView tv_coupon_close,tv_coupon_look,tv_isSuccess,tv_errorMessage; private View v; private WindowManager windowManager; private int screenWidth; public DialogCoupon(Activity activity) { this.activity = activity; //把dialog实例化 dialog = new Dialog(activity); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); init(); } public DialogCoupon(Activity activity, int styleid) { this.activity = activity; //把dialog实例化 dialog = new Dialog(activity, styleid); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); init(); } public void init() { inflater = LayoutInflater.from(activity); windowManager = activity.getWindowManager(); Display display = windowManager.getDefaultDisplay(); screenWidth = display.getWidth(); //把dialog布局填充进来 v = inflater.inflate(R.layout.dialog_coupon, null, false); tv_errorMessage=(TextView)v.findViewById(R.id.tv_errorMessage); tv_isSuccess=(TextView)v.findViewById(R.id.tv_isSuccess); tv_coupon_close = (TextView) v.findViewById(R.id.tv_coupon_close); tv_coupon_look = (TextView) v.findViewById(R.id.tv_coupon_look); tv_coupon_look.setOnClickListener(this); tv_coupon_close.setOnClickListener(this); dialog.setContentView(v); //在这给dialog设置宽高 Window window = dialog.getWindow(); WindowManager.LayoutParams layoutParams = window.getAttributes(); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(layoutParams); } public void setText(String isSuccess,String errorMessage){//调用此方法可以对Diglog的文字进行改变 if (!TextUtils.isEmpty(isSuccess)||!TextUtils.isEmpty(errorMessage)) { tv_isSuccess.setText(isSuccess); tv_errorMessage.setText(errorMessage); } } public void show() { if (dialog != null && !dialog.isShowing()) { dialog.show(); } } public void hide() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } public void setCancelable(boolean value) { dialog.setCancelable(value); } @Override public void onClick(View view) {//在这里对dialog布局的btn进行操作 switch (view.getId()) { case R.id.tv_coupon_look: Intent intent = new Intent(activity, CouponShopActivity.class); activity.startActivity(intent); dialog.dismiss();//这里做了跳转的功能 break; case R.id.tv_coupon_close: if (dialog.isShowing()) { dialog.dismiss(); } break; default: break; } }}

MainActivity上面的应用:

public class MainActivity extends Activity{private DialogCoupon dialogCoupon;//领取优惠券wndqj的diglog @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gold);showCoupon(); } //wndqjdiglog private void showCoupon() { dialogCoupon = new DialogCoupon(this); dialogCoupon.show(); }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。