从DatePicker拾取月份时出错 [英] Error in picking up Month from DatePicker
本文介绍了从DatePicker拾取月份时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请看下面的代码
< RelativeLayout xmlns:android =http://schemas.android .com / apk / res / android
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height = match_parent
android:descendantFocusability =afterDescendants
android:background =@ drawable / background1
tools:context =。Form>
< TextView
android:id =@ + id / txt1
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_centerHorizontal =true
android:layout_centerVertical =true
android:text =@ string / dateTxt
android:textSize =32sp
android :textColor =#FFFFFF
android:textStyle =bold
style =@ style / Text.Strong.Blurry
/>
< DatePicker
android:id =@ + id / datePick
android:layout_below =@ + id / txt1
android:layout_width = $$$$$$$ />
< Button
android:id =@ + id / goBtn
android:layout_below =@ + id / datePick
android:layout_width = wrap_content
android:layout_height =wrap_content
android:layout_centerHorizontal =true
android:layout_centerVertical =true
android:text =@ string / goBtnTxt
android:onClick =goNext
/>
< / RelativeLayout>
包xxxx.xxxx;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
public class Form extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
}
@Override
public boolean onCreateOptionsMenu(菜单菜单){
//充满菜单;如果存在,则会将项目添加到操作栏。
getMenuInflater()。inflate(R.menu.activity_form,menu);
返回true;
}
public void goNext(查看视图)
{
DatePicker datePicker =(DatePicker)findViewById(R.id.datePick);
int year = datePicker.getYear();
int month = datePicker.getMonth();
int date = datePicker.getDayOfMonth();
Toast.makeText(this,String.valueOf(year)+:+ String.valueOf(month)+:+ String.valueOf(date),Toast.LENGTH_LONG).show );
// Intent intent = new Intent(Form.this,DisplayResult.class);
// startActivity(intent);
}
}
当Toast工作,它显示月份减少1值。这意味着如果我选择MARCH(第3个月),并点击按钮,它显示的月份为2.为什么是这样?请帮助!
解决方案
这是因为它从0开始。
< >作为
0 - 一月
1 - 二月
2 - 三月
3 - 四月...
检查这个。
月 - 设置的月份(0-11)兼容性日历。
Please have a look at the following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="afterDescendants"
android:background="@drawable/background1"
tools:context=".Form" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/dateTxt"
android:textSize="32sp"
android:textColor="#FFFFFF"
android:textStyle="bold"
style="@style/Text.Strong.Blurry"
/>
<DatePicker
android:id="@+id/datePick"
android:layout_below="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
/>
<Button
android:id = "@+id/goBtn"
android:layout_below="@+id/datePick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/goBtnTxt"
android:onClick="goNext"
/>
</RelativeLayout>
package xxxx.xxxx;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
public class Form extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form, menu);
return true;
}
public void goNext(View view)
{
DatePicker datePicker = (DatePicker)findViewById(R.id.datePick);
int year = datePicker.getYear();
int month = datePicker.getMonth();
int date = datePicker.getDayOfMonth();
Toast.makeText(this, String.valueOf(year)+":"+String.valueOf(month)+":"+String.valueOf(date), Toast.LENGTH_LONG).show();
//Intent intent = new Intent(Form.this,DisplayResult.class);
//startActivity(intent);
}
}
When the 'Toast' is working, it showing the month reducing 1 value. Which means if I select MARCH (3rd month) and click the button it is showing the month as 2. Why is this? Please help!
解决方案
This is because it starts from 0.
As
0 - January
1 - February
2 - March
3 - April...
Check this.
month - The month that was set (0-11) for compatibility with Calendar.
这篇关于从DatePicker拾取月份时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文