CSDN上有篇Android滚轮时间选择控件(可扩展自定义),阅读量很高,看来大家对这块的需要还是挺大的。版本迭代开发自测完,等待测试提bug。就抽空把项目需求,自己自定义的一个控件分享给需要的人。里面可以学习到Builder创建者模式,还有就是使用接口,一般新手可能不一定会使用。
先想吐槽一下,今天负能量有点多,端午想找人出去玩,都找不到,回家的回家,陪妹子的陪妹子。果然都是一伙重色轻友的人。我也得抓紧了。
好吧,进入正题。先来看看效果图。
1.Hello World是一个TextView控件,用于显示从弹窗选择的内容。里面控件的背景色,标题,文字大小颜色都可以自定义。实际项目中可以用于比如android长按的时候,弹出一些选择的操作,还有就是一些金额选择等等。
2.布局文件
3.主界面操作,简书复制代码不方便...格式排版有问题,我就直接上图片。后面的demo里面会有代码的。
4.重头戏,选择弹窗SelectDialog
packageandrew.selectdialogdemo;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.Color;
importandroid.text.TextUtils;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.LinearLayout.LayoutParams;
importandroid.widget.TextView;
importjava.util.ArrayList;
importjava.util.List;
/**
* 选择弹框
* Created by liuhongxia on 2016/05/30
*/
public classSelectDialogextendsBaseNoticeWindowimplementsView.OnClickListener {
privateListmDataList=newArrayList(0);
privateContextmContext;
privateStringmTitleText=null;
private intmTitleBackgroundColor= -1;
private intmTitleTextColor= Color.WHITE;
private intmButtonColor= -1;
private intmTitleTextSize;
private intmButtonSize;
private intmLastButtonSize;
privateArrayListbuttonList=newArrayList();
public static classBuilder {
ContextmContext;
StringmTitleText=null;
intmTitleBackgroundColor= -1;
intmTitleTextColor= Color.WHITE;
intmButtonColor= -1;
intmTitleTextSize=18;
intmButtonSize=14;
intmLastButtonSize=17;
ListmDataList;
publicBuilder(Context context) {
mContext= context;
}
publicSelectDialogbuild() {
return newSelectDialog(this);
}
publicBuildersetTitleText(String title) {
if(TextUtils.isEmpty(title)) {
return null;
}
mTitleText= title;
return this;
}
publicBuildersetTitleBackground(intcolorResId){
mTitleBackgroundColor= colorResId;
return this;
}
publicBuildersetTitleTextColor(intcolorResId){
mTitleTextColor= colorResId;
return this;
}
publicBuildersetTitleTextSize(inttextSize){
mTitleTextSize= textSize;
return this;
}
publicBuildersetButtonColor(intbuttonColor) {
mButtonColor= buttonColor;
return this;
}
publicBuildersetButtonSize(intbuttonSize) {
mButtonSize= buttonSize;
return this;
}
publicBuildersetLastButtonSize(intbuttonSize) {
mLastButtonSize= buttonSize;
return this;
}
publicBuildersetDataList(List list) {
mDataList= list;
return this;
}
}
publicSelectDialog(Builder builder) {
mContext= builder.mContext;
mTitleText= builder.mTitleText;
mTitleBackgroundColor= builder.mTitleBackgroundColor;
mTitleTextColor= builder.mTitleTextColor;
mButtonColor= builder.mButtonColor;
mDataList= builder.mDataList;
mTitleTextSize= builder.mTitleTextSize;
mButtonSize= builder.mButtonSize;
mLastButtonSize= builder.mLastButtonSize;
init();
}
private voidinit(){
if(null==mDataList) {
return;
}
//头部标题TextView
LayoutParams titleParams =newLayoutParams(
LayoutParams.MATCH_PARENT,ConvertUtils.dip2px(mContext,44));
TextView titleTextView =newTextView(mContext);
titleTextView.setLayoutParams(titleParams);
titleTextView.setBackgroundColor(mContext.getResources().getColor(R.color.topbar));
titleTextView.setTextColor(mTitleTextColor);
titleTextView.setText(mTitleText);
titleTextView.setTextSize(mTitleTextSize);
titleTextView.setGravity(Gravity.CENTER);
//用于添加button和textview的layout
LinearLayout layout =newLinearLayout(mContext);
LayoutParams layoutParams =newLayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin= ConvertUtils.dip2px(mContext,50);
layoutParams.rightMargin= ConvertUtils.dip2px(mContext,50);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
layout.addView(titleTextView);
//button的属性
LayoutParams btnParams =newLayoutParams(
LayoutParams.MATCH_PARENT,ConvertUtils.dip2px(mContext,44));
btnParams.leftMargin= ConvertUtils.dip2px(mContext,15);
btnParams.rightMargin= ConvertUtils.dip2px(mContext,15);
//主Layout
LinearLayout mainLayout =newLinearLayout(mContext);
LayoutParams mainLayoutParams =newLayoutParams(
LayoutParams.MATCH_PARENT,mContext.getResources().getDisplayMetrics().heightPixels);
mainLayout.setLayoutParams(mainLayoutParams);
mainLayout.setGravity(Gravity.CENTER);
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.setBackgroundColor(mContext.getResources().getColor(R.color.alpha50));
//textView的属性
LayoutParams textParams =newLayoutParams(
LayoutParams.MATCH_PARENT,ConvertUtils.dip2px(mContext,1));
textParams.leftMargin= ConvertUtils.dip2px(mContext,15);
textParams.rightMargin= ConvertUtils.dip2px(mContext,15);
intsize =mDataList.size();
for(inti =0;i < size +1;i++) {
Button tDiscardBtn =newButton(mContext);
buttonList.add(tDiscardBtn);
tDiscardBtn.setLayoutParams(btnParams);
tDiscardBtn.setGravity(Gravity.CENTER);
if(i == size) {
tDiscardBtn.setText("取消");
tDiscardBtn.setTextSize(mLastButtonSize);
tDiscardBtn.setBackgroundColor(mContext.getResources().getColor(R.color.white));
tDiscardBtn.setTextColor(mContext.getResources().getColor(R.color.topbar));
}else{
tDiscardBtn.setText(mDataList.get(i));
tDiscardBtn.setTextSize(mButtonSize);
tDiscardBtn.setBackgroundColor(mContext.getResources().getColor(R.color.white));
tDiscardBtn.setTextColor(mButtonColor);
tDiscardBtn.setTag(i);
}
tDiscardBtn.setOnClickListener(this);
tDiscardBtn.setGravity(Gravity.CENTER);
TextView textView =newTextView(mContext);
textView.setBackgroundColor(mContext.getResources().getColor(R.color.line));
textView.setLayoutParams(textParams);
layout.addView(tDiscardBtn);
//去掉最后一行的线
if(i != size) {
layout.addView(textView);
}
}
mainLayout.addView(layout);
//设置按钮的属性
// setAllButtonStyle();
this.setContentView(mainLayout);
this.setWidth(mContext.getResources().getDisplayMetrics().widthPixels);
this.setHeight(mContext.getResources().getDisplayMetrics().heightPixels);
// this.setAnimationStyle(R.style.menu_dialog_animation);
this.setOutsideTouchable(true);
mainLayout.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View view) {
Dismiss(view,BaseNoticeWindow.TYPE_DISMISS_NORMAL);
}
});
}
public voidshow(View parent) {
if(!((Activity)mContext).isFinishing()) {
this.showAtLocation(parent,Gravity.CENTER,0,0);
}
}
public voidsetAllButtonTextColor(intcolorId) {
for(Button button :buttonList) {
button.setTextColor(colorId);
}
}
/**
* 设置button的样式
*/
public voidsetAllButtonStyle() {
for(Button button :buttonList) {
button.setTextSize(14);
button.setBackgroundColor(mContext.getResources().getColor(R.color.white));
button.setTextColor(mContext.getResources().getColor(R.color.text_10));
}
}
@Override
public voidonClick(View v) {
if(mListener==null) {
dismiss();
return;
}
if(v.getTag() !=null) {
mListener.onSureListener(v);
}else{
mListener.onDiscardListener(v);
}
Dismiss(v,BaseNoticeWindow.TYPE_DISMISS_BUTTON);
}
private voidDismiss(View v, intnType) {
if(null!=mListener) {
mListener.onDismissListener(v,nType);
}
dismiss();
}
}
5.总结
对于在代码里面写view,可能有些新手会觉得看上去有点吃力,最简单直接的方法就是,把代码里的view弄成xml布局,这样就会更加方便直接。
转载请注明转自:http://www.jianshu.com/p/874a8736cbd0
csdn博客:http://blog.csdn.net/u011176685/article/details/51546404