自定义dialog
来源:互联网 发布:真实的王菲 知乎 编辑:程序博客网 时间:2024/06/03 15:42
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">效果1:</span>
效果2:
下面贴代码
自定义的dialog类
public class CustomDialog extends Dialog{ /** * dialog style 正在加载 */ public static final int DIALOG_PROGRESS = 0; /** * 试玩 */ public static final int DIALOG_TRIAL = 1; /** * 包月订购 */ public static final int DIALOG_TWO_BUTTON = 2; /** * 提示正在扣费 */ public static final int DIALOG_TITLE_PROGRESS = 3; /** * 提示订购成功 */ public static final int DIALOG_ONE_BUTTON = 4; /** * 提示获取金币 */ public static final int DIALOG_GET_GOLD = 5; private int dialogStyle = -1; // 是否可以外部取消 private boolean isCancelable = false; // 是否同时关闭activity private boolean isShutActivity; /** * dialog 属性 */ private String titleTxt; private int aheadImage; private SpannableStringBuilder firstLineContentTxt; private int behindImage; private SpannableStringBuilder secondLineContentTxt; private int coinImage; private String positiveButTxt; private String neutralButTxt; private String negativeButTxt; // 左侧按钮点击监听 private View.OnClickListener positiveClickListener; // 中间按钮点监听 private View.OnClickListener neutralClickListener; // 右侧按钮点击监听 private View.OnClickListener negativeClickListener; // 左侧按钮变焦监听 private View.OnFocusChangeListener positiveFocusChangeListener; // 中间按钮变焦监听 private View.OnFocusChangeListener neutralFocusChangeListener; // 右侧按钮变焦监听 private View.OnFocusChangeListener negativeFocusChangeListener; /** * /dialog 部件 */ private LinearLayout titleLayout; // 提示标题 private TextView mTvTitle; private LinearLayout conentFirstPart; // top内容前图标,如购买成功前的√ private ImageView aheadIV; //TODO 你这个定义的和下面的太像了 // top内容 private TextView mTvTopMessage; // top内容后图片 private ImageView mIVTopBehindIV; private LinearLayout contentSecondPart; private LinearLayout contentSecontTxt; // 中心内容 private TextView contentTV; // 金币图片(购买道具后金币余额) private ImageView coinIV; private ImageView circlProgressBar; private LinearLayout buttonLayout; // 左侧按钮(一般为确认) private Button positiveBut; // 中间按钮(只有一个按的场合) private Button neutralBut; // 右侧按钮(一般为取消) private Button negativeBut; private Context mContext; /** * 构造器 * * @param context c * @param style s * */ public CustomDialog(Context context, int style) { super(context, R.style.games_custom_dialog); setOwnerActivity((Activity) context); dialogStyle = style; mContext = context; } /** * 构造器是否可以返回 * * @param context Context * @param style int * @param cancelable boolean */ public CustomDialog(Context context, int style, boolean cancelable) { super(context, R.style.games_custom_dialog); setOwnerActivity((Activity) context); dialogStyle = style; mContext = context; isCancelable = cancelable; } /** * 设置一个可以是否返回的dialog * * @param context Context * @param style int * @param cancelable boolean * @return CustomDialog */ public static CustomDialog getCustomDialog(Context context, int style, boolean cancelable) { CustomDialog mCustomDialog = new CustomDialog(context, style); mCustomDialog.setCancelable(cancelable); return mCustomDialog; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.games_dialog_style_layout); init(); refreshUI(); } /** * 初始化控件 */ public void init() { titleLayout = (LinearLayout) findViewById(R.id.title_layout); mTvTitle = (TextView) findViewById(R.id.tv_title); conentFirstPart = (LinearLayout) findViewById(R.id.first_part_layout); aheadIV = (ImageView) findViewById(R.id.iv_ahead); mTvTopMessage = (TextView) findViewById(R.id.tv_center); mIVTopBehindIV = (ImageView) findViewById(R.id.iv_behind); contentSecondPart = (LinearLayout) findViewById(R.id.second_part_layout); contentSecontTxt = (LinearLayout) findViewById(R.id.second_contend_text_layout); contentTV = (TextView) findViewById(R.id.tv_content); coinIV = (ImageView) findViewById(R.id.iv_coin); circlProgressBar = (ImageView) findViewById(R.id.circle_progress_bar); buttonLayout = (LinearLayout) findViewById(R.id.button_layout); positiveBut = (Button) findViewById(R.id.positive_but); neutralBut = (Button) findViewById(R.id.neutral_but); negativeBut = (Button) findViewById(R.id.negative_but); } @Override public void show() { super.show(); setAnimation(); } @Override public void cancel() { super.cancel(); } @Override public void dismiss() { super.dismiss(); } private void setAnimation() { if (dialogStyle == DIALOG_PROGRESS) { // 设置循环旋转动画 Animation operatingAnim = AnimationUtils.loadAnimation(mContext, R.anim.games_dialog_progress_anim); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); circlProgressBar.startAnimation(operatingAnim); } } /** * 更新界面数据 */ public void refreshUI() { switch (dialogStyle) { case DIALOG_PROGRESS: titleLayout.setVisibility(View.GONE); conentFirstPart.setVisibility(View.GONE); contentTV.setText(secondLineContentTxt); circlProgressBar.setVisibility(View.VISIBLE); buttonLayout.setVisibility(View.GONE); setCancelable(isCancelable); break; case DIALOG_TRIAL: titleLayout.setVisibility(View.INVISIBLE); conentFirstPart.setVisibility(View.GONE); positiveBut.setVisibility(View.INVISIBLE); neutralBut.setVisibility(View.VISIBLE); negativeBut.setVisibility(View.INVISIBLE); contentTV.setText(secondLineContentTxt); neutralBut.setText(neutralButTxt); neutralBut.setOnClickListener(neutralClickListener); setCancelable(isCancelable); break; case DIALOG_TWO_BUTTON: mTvTitle.setText(titleTxt); conentFirstPart.setVisibility(View.GONE); contentTV.setText(secondLineContentTxt); positiveBut.setText(positiveButTxt); negativeBut.setText(negativeButTxt); positiveBut.setOnClickListener(positiveClickListener); negativeBut.setOnClickListener(negativeClickListener); setCancelable(isCancelable); break; case DIALOG_TITLE_PROGRESS: mTvTitle.setText(titleTxt); conentFirstPart.setVisibility(View.GONE); circlProgressBar.setVisibility(View.VISIBLE); contentTV.setText(secondLineContentTxt); setCancelable(isCancelable); break; case DIALOG_ONE_BUTTON: mTvTitle.setText(titleTxt); aheadIV.setVisibility(View.VISIBLE); aheadIV.setImageResource(aheadImage); mTvTopMessage.setVisibility(View.VISIBLE); mTvTopMessage.setText(firstLineContentTxt); contentSecondPart.setVisibility(View.GONE); positiveBut.setVisibility(View.INVISIBLE); neutralBut.setVisibility(View.VISIBLE); negativeBut.setVisibility(View.INVISIBLE); neutralBut.setText(neutralButTxt); neutralBut.setOnClickListener(neutralClickListener); setCancelable(isCancelable); break; case DIALOG_GET_GOLD: titleLayout.setVisibility(View.INVISIBLE); conentFirstPart.setVisibility(View.VISIBLE); mIVTopBehindIV.setVisibility(View.VISIBLE); mIVTopBehindIV.setImageResource(behindImage); mTvTopMessage.setText(firstLineContentTxt); positiveBut.setVisibility(View.INVISIBLE); neutralBut.setVisibility(View.VISIBLE); negativeBut.setVisibility(View.INVISIBLE); contentTV.setText(secondLineContentTxt); neutralBut.setText(neutralButTxt); neutralBut.setOnClickListener(neutralClickListener); setCancelable(isCancelable); break; } } public String getTitleTxt() { return titleTxt; } public void setTitleTxt(String titleTxt) { this.titleTxt = titleTxt; } public int getAheadImage() { return aheadImage; } public void setAheadImage(int aheadImage) { this.aheadImage = aheadImage; } public SpannableStringBuilder getFirstLineContentTxt() { return firstLineContentTxt; } public void setFirstLineContentTxt(SpannableStringBuilder firstLineContentTxt) { this.firstLineContentTxt = firstLineContentTxt; } public int getBehindImage() { return behindImage; } public void setBehindImage(int behindImage) { this.behindImage = behindImage; } public SpannableStringBuilder getSecondLineContentTxt() { return secondLineContentTxt; } public void setSecondLineContentTxt(SpannableStringBuilder secondLineContentTxt) { this.secondLineContentTxt = secondLineContentTxt; } public int getCoinImage() { return coinImage; } public void setCoinImage(int coinImage) { this.coinImage = coinImage; } public String getPositiveButTxt() { return positiveButTxt; } public void setPositiveButTxt(String positiveButTxt) { this.positiveButTxt = positiveButTxt; } public String getNeutralButTxt() { return neutralButTxt; } public void setNeutralButTxt(String neutralButTxt) { this.neutralButTxt = neutralButTxt; } public String getNegativeButTxt() { return negativeButTxt; } public void setNegativeButTxt(String negativeButTxt) { this.negativeButTxt = negativeButTxt; } public View.OnClickListener getPositiveClickListener() { return positiveClickListener; } public void setPositiveClickListener( View.OnClickListener positiveClickListener) { this.positiveClickListener = positiveClickListener; } public View.OnClickListener getNeutralClickListener() { return neutralClickListener; } public void setNeutralClickListener( View.OnClickListener neutralClickListener) { this.neutralClickListener = neutralClickListener; } public View.OnClickListener getNegativeClickListener() { return negativeClickListener; } public void setNegativeClickListener( View.OnClickListener negativeClickListener) { this.negativeClickListener = negativeClickListener; } public View.OnFocusChangeListener getPositiveFocusChangeListener() { return positiveFocusChangeListener; } public void setPositiveFocusChangeListener( View.OnFocusChangeListener positiveFocusChangeListener) { this.positiveFocusChangeListener = positiveFocusChangeListener; } public View.OnFocusChangeListener getNeutralFocusChangeListener() { return neutralFocusChangeListener; } public void setNeutralFocusChangeListener( View.OnFocusChangeListener neutralFocusChangeListener) { this.neutralFocusChangeListener = neutralFocusChangeListener; } public View.OnFocusChangeListener getNegativeFocusChangeListener() { return negativeFocusChangeListener; } public void setNegativeFocusChangeListener( View.OnFocusChangeListener negativeFocusChangeListener) { this.negativeFocusChangeListener = negativeFocusChangeListener; } public boolean isShutActivity() { return isShutActivity; } public void setShutActivity(boolean shutActivity) { isShutActivity = shutActivity; }}
xml文件
1 games_dialog_style_layout
<?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="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.5" android:visibility="invisible"> </LinearLayout> <LinearLayout android:id="@+id/title_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="" android:textColor="@color/games_dialog_title_bg" android:textSize="@dimen/games_text_big" /> <View android:layout_width="match_parent" android:layout_height="2px" android:layout_gravity="bottom" android:layout_marginTop="15dp" android:background="@color/games_divide_line_bg" /> </LinearLayout> <LinearLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:id="@+id/first_part_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/iv_ahead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <TextView android:id="@+id/tv_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|top" android:gravity="center" android:text="" android:textColor="@color/white" android:textSize="@dimen/games_text_nor" /> <ImageView android:id="@+id/iv_behind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/games_normal_margin_left" android:visibility="gone" /> </LinearLayout> <LinearLayout android:id="@+id/second_part_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/circle_progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@mipmap/games_circle_progress_bg" android:visibility="gone" /> <LinearLayout android:id="@+id/second_contend_text_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|center" android:layout_marginTop="@dimen/games_bigger_margin_top" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|top" android:gravity="center" android:lineSpacingExtra="42px" android:text="" android:textColor="@color/white" android:textSize="@dimen/games_text_nor" /> <ImageView android:id="@+id/iv_coin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/button_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginTop="30dp" android:gravity="center" android:orientation="horizontal" android:padding="5dp" android:visibility="visible"> <Button android:id="@+id/positive_but" android:layout_width="@dimen/games_dialog_button_height" android:layout_height="@dimen/games_dialog_button_width" android:background="@drawable/games_dialog_but_selector" android:gravity="center" android:padding="7dp" android:text="确定" android:textColor="@color/games_normal_text_color" android:textSize="@dimen/games_text_nor" /> <Button android:id="@+id/neutral_but" android:layout_width="@dimen/games_dialog_button_height" android:layout_height="@dimen/games_dialog_button_width" android:layout_marginLeft="@dimen/games_normal_padding" android:background="@drawable/games_dialog_but_selector" android:gravity="center" android:padding="7dp" android:textColor="@color/games_normal_text_color" android:textSize="@dimen/games_text_nor" android:visibility="gone" /> <Button android:id="@+id/negative_but" android:layout_width="@dimen/games_dialog_button_height" android:layout_height="@dimen/games_dialog_button_width" android:layout_marginLeft="@dimen/games_normal_padding" android:background="@drawable/games_dialog_but_selector" android:gravity="center" android:padding="7dp" android:text="取消" android:textColor="@color/games_normal_text_color" android:textSize="@dimen/games_text_nor" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </LinearLayout></LinearLayout>
还有一些动画和颜色等代码就不贴了,可以根据自己需要手动添加
0 0
- 自定义Dialog---继承Dialog
- 自定义Dialog android Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义dialog
- 自定义 Dialog
- 自定义dialog
- 自定义 dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 自定义dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义Dialog
- LeetCode 113. Path Sum II
- PI中的WSDL文件的导出
- Android Studio更新升级方法
- 基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器
- Android studio 导出不包含asset资源文件的jar包
- 自定义dialog
- EventBus使用详解(一)——初步使用EventBus
- elasticsearch2.3安装以及集群部署
- leetcode 29. Divide Two Integers
- 论一个真正的软件测试工程师
- 格子类型游戏实践源项目
- boost::enable_shared_from_this的用法
- 分析(Android下的任务和Activity栈)
- windows写的txt mac打开是乱码, 浏览器也打不开