初學Android:下面一段程序在android模擬器中TextView的時間可以隨自己的選擇改變,但是放到真機中無論怎麼改變時間TextView中的內容都不改變問題出在哪裡?
Java源碼:
package com.example.harleyjiang.widgetdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.sql.Time;
import java.util.Calendar;
public class DatePickerActivity extends AppCompatActivity {
private TextView textView;
private DatePicker datePicker;
private TimePicker timePicker;
/*聲明日期和時間變量*/
private int year;
private int month;
private int day;
private int hour;
private int minute;
/*聲明日期和時間變量*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
/*獲取當前日期及時間*/
Calendar calendar = Calendar.getInstance();
year =calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
/*獲取當前日期及時間*/
datePicker = (DatePicker) findViewById(R.id.datepicker);
timePicker = (TimePicker) findViewById(R.id.timepicker);
/*設置TextView對象,顯示初始日期時間*/
textView = (TextView) findViewById(R.id.timeview);
textView.setText(new StringBuilder().append(year).append("/")
.append(format(month + 1)).append("/").append(format(day))
.append(" ").append(format(hour)).append(":").append(format(minute)));
/*設置TextView對象,顯示初始日期時間*/
/*設置OnDateChangedListener()*/
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
DatePickerActivity.this.year = year;
month = monthOfYear;
day = dayOfMonth;
textView.setText(new StringBuilder().append(year)
.append("/").append(format(month + 1))
.append("/").append(format(day)).append(" ")
.append(format(hour)).append(":").append(format(minute)));
}
});
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
DatePickerActivity.this.minute=minute;
textView.setText(new StringBuilder().append(year).append("/")
.append(format(month+1)).append("/").append(format(day))
.append(" ").append(format(hour)).append(":").append(format(minute)));
}
});
}
private String format(int time) {
String str =""+time;
if (str.length()==1)
str="0" +str;
return str;
}
}
布局文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.harleyjiang.widgetdemo.DatePickerActivity">
<TextView
android:id="@+id/timeview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DatePicker和TimePicker演示"/>
<TimePicker
android:id="@+id/timepicker"
android:layout_below="@+id/timeview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TimePicker>
<DatePicker
android:id="@+id/datepicker"
android:layout_below="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</DatePicker>
就你的代碼來說,唯一可能引起不同的是你的模擬器和真機上時間格式的設置的不同。你對比下
day = calendar.get(Calendar.DAY_OF_MONTH); 這個變量的值