gpt4 book ai didi

java - 从 DatePicker 获取月份时出错

转载 作者:行者123 更新时间:2023-12-01 07:34:25 25 4
gpt4 key购买 nike

请看下面的代码

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

}

当“Toast”工作时,它显示月份减少 1 值。这意味着如果我选择 MARCH(第 3 个月)并单击按钮,该月份将显示为 2。这是为什么?请帮忙!

最佳答案

这是因为它是从0开始的。

作为

0 - January

1 - February

2 - March

3 - April...

检查this .

month - The month that was set (0-11) for compatibility with Calendar.

关于java - 从 DatePicker 获取月份时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com