本例使用到的类比较多,所使用的自定义的控件都是老外写好的,我知识根据他给的滑动的空间的基础上美化下和添加图片罢了,不多说直接看图:
第一步:设计弹出框xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff424542"
android:orientation="horizontal"
android:layout_above="@+id/bithday_layout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:layout_centerVertical="true"
android:textColor="@android:color/white"
android:text="日期" />
<Button
android:id="@+id/cancel"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/mm_title_btn_right"
android:text="取消"
android:textColor="@android:color/white"
/>
<Button
android:id="@+id/submit"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/mm_title_act_btn"
android:text="完成"
android:textColor="@android:color/white"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/bithday_layout"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="220dip"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_height="220dip"
android:gravity="center"
android:orientation="horizontal" >
<com.example.widget.WheelView
android:id="@+id/year"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<com.example.widget.WheelView
android:id="@+id/month"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<com.example.widget.WheelView
android:id="@+id/day"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="220.0dip"
android:layout_gravity="center"
android:background="@drawable/com_ttshrk_view_scroll_picker_background" >
</FrameLayout>
</RelativeLayout>
</LinearLayout>
第二步:编写弹出框PopupWindow类:
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.example.widget.NumericWheelAdapter;
import com.example.widget.OnWheelChangedListener;
import com.example.widget.WheelView;
public class SelectBirthday extends PopupWindow implements OnClickListener {
private Activity mContext;
private View mMenuView;
private ViewFlipper viewfipper;
private Button btn_submit, btn_cancel;
private String age;
private DateNumericAdapter monthAdapter, dayAdapter, yearAdapter;
private WheelView year, month, day;
private int mCurYear = 80, mCurMonth = 5, mCurDay = 14;
private String[] dateType;
public SelectBirthday(Activity context) {
super(context);
mContext = context;
this.age = "2012-9-25";
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.birthday, null);
viewfipper = new ViewFlipper(context);
viewfipper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
year = (WheelView) mMenuView.findViewById(R.id.year);
month = (WheelView) mMenuView.findViewById(R.id.month);
day = (WheelView) mMenuView.findViewById(R.id.day);
btn_submit = (Button) mMenuView.findViewById(R.id.submit);
btn_cancel = (Button) mMenuView.findViewById(R.id.cancel);
btn_submit.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
Calendar calendar = Calendar.getInstance();
OnWheelChangedListener listener = new OnWheelChangedListener() {
public void onChanged(WheelView wheel, int oldValue, int newValue) {
updateDays(year, month, day);
}
};
int curYear = calendar.get(Calendar.YEAR);
if (age != null && age.contains("-")) {
String str[] = age.split("-");
mCurYear = 100 - (curYear - Integer.parseInt(str[0]));
mCurMonth = Integer.parseInt(str[1]) - 1;
mCurDay = Integer.parseInt(str[2]) - 1;
;
}
dateType = mContext.getResources().getStringArray(R.array.date);
monthAdapter = new DateNumericAdapter(context, 1, 12, 5);
monthAdapter.setTextType(dateType[1]);
month.setViewAdapter(monthAdapter);
month.setCurrentItem(mCurMonth);
month.addChangingListener(listener);
// year
yearAdapter = new DateNumericAdapter(context, curYear - 100, curYear+100,
100 - 20);
yearAdapter.setTextType(dateType[0]);
year.setViewAdapter(yearAdapter);
year.setCurrentItem(mCurYear);
year.addChangingListener(listener);
// day
updateDays(year, month, day);
day.setCurrentItem(mCurDay);
updateDays(year, month, day);
day.addChangingListener(listener);
viewfipper.addView(mMenuView);
viewfipper.setFlipInterval(6000000);
this.setContentView(viewfipper);
this.setWidth(LayoutParams.FILL_PARENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x00000000);
this.setBackgroundDrawable(dw);
this.update();
}
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
super.showAtLocation(parent, gravity, x, y);
viewfipper.startFlipping();
}
private void updateDays(WheelView year, WheelView month, WheelView day) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,
calendar.get(Calendar.YEAR) + year.getCurrentItem());
calendar.set(Calendar.MONTH, month.getCurrentItem());
int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
dayAdapter = new DateNumericAdapter(mContext, 1, maxDays,
calendar.get(Calendar.DAY_OF_MONTH) - 1);
dayAdapter.setTextType(dateType[2]);
day.setViewAdapter(dayAdapter);
int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
day.setCurrentItem(curDay - 1, true);
int years = calendar.get(Calendar.YEAR) - 100;
age = years + "-" + (month.getCurrentItem() + 1) + "-"
+ (day.getCurrentItem() + 1);
}
/**
* Adapter for numeric wheels. Highlights the current value.
*/
private class DateNumericAdapter extends NumericWheelAdapter {
// Index of current item
int currentItem;
// Index of item to be highlighted
int currentValue;
/**
* Constructor
*/
public DateNumericAdapter(Context context, int minValue, int maxValue,
int current) {
super(context, minValue, maxValue);
this.currentValue = current;
setTextSize(24);
}
protected void configureTextView(TextView view) {
super.configureTextView(view);
view.setTypeface(Typeface.SANS_SERIF);
}
public CharSequence getItemText(int index) {
currentItem = index;
return super.getItemText(index);
}
}
public void onClick(View v) {
this.dismiss();
}
}
代码主要就是这两个,其他的控件类不是本人编写就不上传了,直接上源码,有需要的自己下载研究
- 大小: 51.6 KB
分享到:
相关推荐
此demo通过自定义dialog和自定义View实现了滑动选择框,也可以实现二级联动三级联动,不过需要自己修改联动关系。
div弹出提示框,滑动提示框div弹出提示框,滑动提示框div弹出提示框,滑动提示框div弹出提示框,滑动提示框
android实现的可左右滑动的选择控件
Android案例页面底部弹框PopupWindow+竖直滑动选择器WheelView的实现
Android滑动菜单框架 SliderMenu 一分钟集成
android 滚动数字选择器
android 自定义时间选择器 滑动选取 支持年月日时分
该Android水平数字滚动器,完全自定义,继承view,在界面中可以左右滚动一行很长的数据,可以并且选中屏幕中间的数字,中间的数字最大最大,两边数字变淡变小,可以根据个人需要调整字体和颜色,在实际项目开发中可以...
Android垂直滚动弹幕,垂直滚动新闻,可自定义item布局
移动端滑动选择器效果(含自定义选择内容/日期选择/地址选择)
MyPickerView Watch the GIF please
android仿QQ相册滑动多选
Android从屏幕底部滑动弹出PopupWindow,有动画效果,类似于sharesdk的分享页面滑动弹出效果。
android水平滚动选择器spinner_wheel,类似时间滚动选择一样,不仅可以水平选择,还能自定义漂亮的时间控件
1、android原生仿头条滑动 2、封装自定义 3、可执行demo
Android 界面禁止左右滑动切换
部分区域滑动的弹出框
Android自定义简易日期选择器,可自定义背景格式,可滑动选择日期,该项目是用Android studio写的,如果是eclipse用户请改成eclipse格式再使用。
android 仿京东顶部搜索框滑动伸缩动画效果,实现下滑搜索框变化效果
android gridview分页和滑动效