2010-08-12 78 views
10

我一直坚持DatePicker的几个问题,我似乎无法找到任何解决方案。自定义DatePicker

我目前有一个DatePickerDialog,它非常适合我的需要我还需要能够隐藏/禁用日期和月份字段(为了选择一个月或一年),我找不到任何解决方案除了实施我自己的MonthPicker/yearPicker。事实是,我不知道从哪里开始写一个与默认风格相同风格的选择器。

如果您有任何自定义日期picjer示例代码或任何更简单的想法来解决我的问题,我会很高兴。

非常感谢

+0

试试这个,你可以从这里开始,它有正常的微调选择和复杂的视图寻呼机刷卡http://viswanathl.in/2014/02/custom-datepicker-in-android/ – 2015-01-08 18:45:27

回答

7

我的想法是使用的Android定制的日期选择器。在谷歌代码,我发现了一个开源项目叫做Android Wheel

Android wheel for date time picker

从谷歌代码校验的源代码后,你会发现几个例子,这样,只要你想,你可以轻松地定制为期一年的选择器。

+0

喜你有没有这个轮子的源代码? – Dimitri 2013-05-10 11:14:34

+0

@Nikita你可以在我的回答中找到他的谷歌代码回购的链接。 – anticafe 2013-05-10 12:04:48

+0

你能给我链接吗? – Dimitri 2013-05-10 12:07:47

1

它有点不好意思,但如果你看看DatePicker的布局,你可以看到它的FrameLayout包含3个子视图。所以我想隐藏一年,做到这一点。

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(); 
    } 
} 

这将获取选取器的framelayout,然后隐藏它的第三个孩子,这是一年。希望这可以帮助!

+0

感谢您的想法。尽管一般只有一年可以成功隐藏,但月份和日期的位置取决于地区(美国的月/日/年和英国的日/月/年)。 – ernazm 2011-09-22 12:19:03

3

蜂巢有一些变化。

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(); 
}