Dialog
Android Studio 环境下
一、自定义Dialog基类HintDialog
自定义Dialog类,在之后的使用过程中,直接在使用的Activity新建该基类的实例。
public class HintDialog extends Dialog { public HintDialog(Context context) { super(context); } public HintDialog(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private OnClickListener positiveButtonClickListener; private OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } /** * Set the Dialog message from resource * */ public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } public Builder setContentView(View v) { this.contentView = v; return this; } /** * Set the positive button resource and it's listener * */ public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public HintDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final HintDialog dialog = new HintDialog(context,R.style.IOSDialog); View layout = inflater.inflate(R.layout.hint_dialog, null); dialog.addContentView(layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // set the confirm button if (positiveButtonText != null) { ((TextView) layout.findViewById(R.id.positiveButton)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((TextView) layout.findViewById(R.id.positiveButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.positiveButton).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((TextView) layout.findViewById(R.id.negativeButton)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((TextView) layout.findViewById(R.id.negativeButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.negativeButton).setVisibility( View.GONE); } // set the content message if (message != null) { ((TextView) layout.findViewById(R.id.message)).setText(message); } else if (contentView != null) { // if no message set // add the contentView to the dialog body ((LinearLayout) layout.findViewById(R.id.content)) .removeAllViews(); ((LinearLayout) layout.findViewById(R.id.content)) .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } dialog.setContentView(layout); return dialog; } } }
二、HintDialog的使用在使用HintDialog的Activity中:
初始化:
HintDialog.Builder builder;在需要展示Dialog的地方:builder = new HintDialog.Builder(WaitingActivity.this); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //设置你的操作事项,另外一个按钮同样使用这种方法,这里我只使用了一个按钮 dialog.dismiss(); } }); builder.setMessage("你今天去玩了么?"); builder.create().show();
三、相应的layouthint_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/content"> <TextView android:id="@+id/message" android:gravity="center" android:layout_width="240dp" android:layout_height="wrap_content" android:text="Medium Text" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:textColor="@color/important_word" android:layout_marginTop="50dp" android:layout_marginBottom="30dp" android:textSize="18sp" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="@color/assistant_word"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <TextView android:id="@+id/positiveButton" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="0.5" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:gravity="center" android:text="前往设置" android:textColor="@color/mainColor" android:textSize="20sp" /> <View android:layout_width="1dp" android:layout_height="fill_parent" android:background="@color/assistant_word" /> <TextView android:id="@+id/negativeButton" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="0.5" android:gravity="center" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:text="忽略" android:textColor="@color/important_word" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
效果图:
个人感觉还是比较好用的!!!强烈推荐
本文介绍如何在Android Studio环境下自定义Dialog基类HintDialog,详细讲解了创建和初始化过程,并提供了布局文件hint_dialog.xml的示例,强调其易用性和实用性。

8755

被折叠的 条评论
为什么被折叠?



