- public classCommomDialogextendsDialogimplements View.OnClickListener{
- private TextView contentTxt;
- private TextView titleTxt;
- private TextView submitTxt;
- private TextView cancelTxt;
- private Context mContext;
- private String content;
- private OnCloseListener listener;
- private String positiveName;
- private String negativeName;
- private String title;
- public CommomDialog(Context context) {
- super(context);
- this.mContext = context;
- }
- publicCommomDialog(Context context,int themeResId, String content) {
- super(context, themeResId);
- this.mContext = context;
- this.content = content;
- }
- publicCommomDialog(Context context,int themeResId, String content, OnCloseListener listener) {
- super(context, themeResId);
- this.mContext = context;
- this.content = content;
- this.listener = listener;
- }
- protectedCommomDialog(Context context,boolean cancelable, OnCancelListener cancelListener) {
- super(context, cancelable, cancelListener);
- this.mContext = context;
- }
- public CommomDialog setTitle(String title){
- this.title = title;
- return this;
- }
- public CommomDialog setPositiveButton(String name){
- this.positiveName = name;
- return this;
- }
- public CommomDialog setNegativeButton(String name){
- this.negativeName = name;
- return this;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dialog_commom);
- setCanceledOnTouchOutside(false);
- initView();
- }
- private void initView(){
- contentTxt = (TextView)findViewById(R.id.content);
- titleTxt = (TextView)findViewById(R.id.title);
- submitTxt = (TextView)findViewById(R.id.submit);
- submitTxt.setOnClickListener(this);
- cancelTxt = (TextView)findViewById(R.id.cancel);
- cancelTxt.setOnClickListener(this);
- contentTxt.setText(content);
- if(!TextUtils.isEmpty(positiveName)){
- submitTxt.setText(positiveName);
- }
- if(!TextUtils.isEmpty(negativeName)){
- cancelTxt.setText(negativeName);
- }
- if(!TextUtils.isEmpty(title)){
- titleTxt.setText(title);
- }
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.cancel:
- if(listener !=null){
- listener.onClick(this,false);
- }
- this.dismiss();
- break;
- case R.id.submit:
- if(listener !=null){
- listener.onClick(this,true);
- }
- break;
- }
- }
- public interface OnCloseListener{
- voidonClick(Dialog dialog,boolean confirm);
- }
- }
再看看 R.layout.dialog_commom xml 文件
自定义了监听事件,设置了消息后,返回该句柄, return this;
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bg_round_white"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:padding="12dp"
- android:layout_marginTop="12dp"
- android:text="提示"
- android:textSize="16sp"
- android:textColor="@color/black"/>
- <TextView
- android:id="@+id/content"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_gravity="center_horizontal"
- android:lineSpacingExtra="3dp"
- android:layout_marginLeft="40dp"
- android:layout_marginTop="20dp"
- android:layout_marginRight="40dp"
- android:layout_marginBottom="30dp"
- android:text="签到成功,获得200积分"
- android:textSize="12sp"
- android:textColor="@color/font_common_1"/>
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="@color/commom_background"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/cancel"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bg_dialog_left_white"
- android:layout_weight="1.0"
- android:gravity="center"
- android:text="@string/cancel"
- android:textSize="12sp"
- android:textColor="@color/font_common_2"/>
- <View
- android:layout_width="1dp"
- android:layout_height="match_parent"
- android:background="@color/commom_background"/>
- <TextView
- android:id="@+id/submit"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bg_dialog_right_white"
- android:gravity="center"
- android:layout_weight="1.0"
- android:text="@string/submit"
- android:textSize="12sp"
- android:textColor="@color/font_blue"/>
整个背景我使用了圆角,这样不显得特别生硬 android:background="@drawable/bg_round_white"
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
- <solid android:color="@color/white" />
- <corners android:radius="8dp" />
- </shape>
左下按钮:android:background="@drawable/bg_dialog_left_white"
当然底部两个按钮也是要做相应的圆角处理:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
- <solid android:color="@color/white" />
- <corners android:bottomLeftRadius="8dp" />
- </shape>
右下按钮:android:background="@drawable/bg_dialog_right_white"
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
- <solid android:color="@color/white" />
- <corners android:bottomRightRadius="8dp" />
- </shape>
展示的 style 也要设置一下:
- null
- true
- false
- true@android:color/transparent
- true
这样基本大功告成,通过设置消息头,信息体,按钮名称,还有点击事件,就可以随意控制你的提示框了。
源码链接:后面我会把自己的代码库都放上来,与大家一起学习。
https://github.com/xiaoxiaoqingyi/mine-android-repository
来源: http://www.cnblogs.com/xiaoxiaoqingyi/p/6738108.html