Custom DatePicker
I ve been stuck on a DatePicker problem for several and i can't seem to find any solution.
I currently have a DatePickerDialog which fits quit开发者_运维问答e well to my needs YET I also need to be able to hide / disable Both Day and Month field (in order to select a Month or a Year), I can't find any solution except implementing my own MonthPicker/yearPicker . the fact is I don't really know where to begin with to write a picker which has the same style as the default one.
I would be glad if any of you have sample code of custom date picjer or any simpler Idea to get to cope with my problem.
Thanks a lot
hush
My idea is using a custom Date Picker for android. In Google Code, I found an open source project called Android Wheel
After checkout source code from Google Code, you'll find several example so that you can easily customize a year-picker as you want.
There is some change in honeycomb.
ViewGroup group = (ViewGroup) findViewById(R.id.picker);
group = (ViewGroup) group.getChildAt(0);
group = (ViewGroup) group.getChildAt(0);
// 0:datepicker , 1:calendar
Toast.makeText(this, String.format("%s children", group.getChildCount()), Toast.LENGTH_SHORT).show();
try{
group.getChildAt(0).setVisibility(View.GONE);
// 0 for year, 1 for month, 2 for day
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
Its kinda hacky, but if you look at the layout for the DatePicker you can see its a FrameLayout containing 3 sub-views. So I was wanting the hide the year and did this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup group = (ViewGroup) findViewById(R.id.picker);
group = (ViewGroup) group.getChildAt(0);
Toast.makeText(this, String.format("%s children", group.getChildCount()), Toast.LENGTH_SHORT).show();
try{
group.getChildAt(2).setVisibility(View.GONE);
// 2 for year, 1 for day, 0 for month
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
This gets the framelayout of the picker and then hides its 3rd child which is the year. Hope this helps!
精彩评论