如何设置日期选择器的值中的EditText? [英] How to set the value of the datePicker in to EditText?

查看:374
本文介绍了如何设置日期选择器的值中的EditText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML布局是象下面这样:

My XMl Layout is as like below:

<RelativeLayout  android:id="@+id/dateSelectionLayout"  android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="vertical" android:visibility="visible">

            <EditText android:layout_height="wrap_content" android:layout_width="fill_parent" 
                android:singleLine="true" android:id="@+id/dateSelectionEditText" android:gravity="center"
                android:textColor="#000000" android:textSize="14sp" android:cursorVisible="false"
                android:focusable="false"
                android:hint="tax code" android:layout_weight="1"/>

            <DatePicker android:id="@+id/datePicker"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1" android:layout_below="@+id/dateSelectionEditText"/>

    </RelativeLayout>

现在我想改变的是基于日期选择日期EDITTEXT的价值。如果用户更改日期那么就应该在当时的EDITTEXT得到体现。它是如何可能的?

Now i want to change the value of the editText that is based on the datePicker date. If User change the date then it should be reflected on the editText at that time. how it is Possible ?

编辑:
我曾经做过这样的:
已设置resourcec这样的:

Edited: I have done like this: Have set the resourcec like:

datePicker = (DatePicker) findViewById(R.id.datePicker);
        dateSelectionEditText = (EditText)findViewById(R.id.dateSelectionEditText);

和设置的覆盖方​​法是这样的:

And have set the override method like this:

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    dateSelectionEditText.setText(dayOfMonth+"/"+monthOfYear+"/"+year);

}

但仍然没有得到关于日期选取器值的变化任意值。

But still not getting any value on the changing of date picker value.

编辑:

在卡斯帕Moerch的回答我得到了解决。但有一点问题。
我用这code给init日期选择器。

After Kasper Moerch's answer i got the solution. But there is little problem. I am using this code to init the datepicker.

final Calendar c = Calendar.getInstance();  
        //dateSelectionEditText.setText( "" + dayOfMonth + "-" + monthOfYear + "-" + year );
        datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new MyOnDateChangedListener());
        Toast.makeText(getApplicationContext(), ""+position+"", Toast.LENGTH_SHORT).show();

现在有了这个,我可以看到日期选择器更改值。但它需要0为第一个月(一月),而不是1。那么,为什么它happend这样?

Now with this, I am able to see the changed value from datePicker. But it takes 0 as a First month (from January) instead of the "1". So why it is happend like this ?

感谢。

推荐答案

您需要做的是得到一个参考的EditText DatePicker的

What you need to do is get a reference to the EditText and the DatePicker:

EditText editText = (EditText) findViewById( R.id.dateSelectionEditText );
DatePicker datePicker = (DatePicker) findViewById( R.id.datePicker );

然后你需要创建一个 OnDateChangedListener

private class MyOnDateChangedListener implements OnDateChangedListener {
  @Override
  public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     editText.setText( "" + dayOfMonth + "-" + (monthOfYear + 1) + "-" + year );
  }
};

这就是所有剩下要做的仅仅是初始化的DatePicker:

All thats left to do is just initialize the DatePicker:

datePicker.init( year, monthOfYear, dayOfMonth, new MyOnDateChangedListener() ).

这篇关于如何设置日期选择器的值中的EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆