版权声明:本文为 zhangxiao原创文章,可以随意转载,但必须在明确位置注明出处!!!
转载请标明出处:http://www.jianshu.com/p/2cbf5bf3842e
概述
为了应对产品的变化多端的需求,于是封装了一个自定义dialog,话不多说上Demo先:
https://github.com/zh-xiao/CustomDialog
需求分析
自定义dialog至少应该提供的接口
- 设置自定义布局(这里我放在构造方法里)
- 设置标题(这里我用一个set方法)
- 设置内容(这里我用一个set方法)
- 设置确定和取消的点击事件(先通过定义接口的方式,定义一个确定事件的监听器和一个取消事件的监听器,再通过构造方法传入自定义类)
CustomDialog:
public class CustomDialog extends Dialog {
private TextView mTitle;
private TextView mContent;
private Button mCancel;
private Button mConfirm;
private int mLayoutId;
private OnConfirmListener mConfirmListener;
private OnCancleListener mCancelListener;
private String mTitleString;
private String mContentString;
/**
* 构造方法
* @param context
* @param layoutId 布局文件id
* @param confirmListener 点击确定对应的监听器
* @param cancelListener 点击取消对应的监听器
*/
public CustomDialog(Context context,int layoutId,OnConfirmListener confirmListener,OnCancleListener cancelListener) {
super(context, R.style.MyDialog);
mConfirmListener=confirmListener;
mCancelListener=cancelListener;
mLayoutId=layoutId;
}
//点击确定对应的监听器
public interface OnConfirmListener {
void onConfirm();
}
//点击取消对应的监听器
public interface OnCancleListener {
void onCancle();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mLayoutId);
mTitle= (TextView) findViewById(R.id.title);
mContent= (TextView) findViewById(R.id.content);
mCancel = (Button) findViewById(R.id.cancle);
mConfirm= (Button) findViewById(R.id.confirm);
mTitle.setText(mTitleString);
mContent.setText(mContentString);
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCancelListener.onCancle();
}
});
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mConfirmListener.onConfirm();
}
});
}
/**
* 设置对话框标题
* @param title
* @return
*/
public CustomDialog setTitle(String title){
mTitleString=title;
return this;
}
/**
* 设置对话框内容
* @param content
* @return
*/
public CustomDialog setContent(String content){
mContentString=content;
return this;
}
}
dialog_custom:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/dialog_bg"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="title"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="content"
android:textSize="20sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:id="@+id/cancle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/selector_left_button"
android:gravity="center"
android:text="cancle"
android:textColor="#3980D1"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#000000"/>
<Button
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/selector_right_button"
android:gravity="center"
android:text="confirm"
android:textColor="#3980D1"/>
</LinearLayout>
</LinearLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CustomDialog(MainActivity.this, R.layout.dialog_custom, new CustomDialog.OnConfirmListener() {
@Override
public void onConfirm() {
Toast.makeText(MainActivity.this, "confirm", Toast.LENGTH_SHORT).show();
}
}, new CustomDialog.OnCancleListener() {
@Override
public void onCancle() {
Toast.makeText(MainActivity.this, "cancel", Toast.LENGTH_SHORT).show();
}
})
.setTitle("this is Title")
.setContent("this is Content! this is Content!")
.show();
}
});
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.xiao.customdialog.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
总结:
可以看到CustomDialog使用起来非常简洁,直接new出来,传入context,layoutId,OnConfirmListener,OncancleListener,接着流式set/set/show.是不是超简单.O(∩_∩)O.