qq_23842231
2015-10-02 13:24
采纳率: 66.7%
浏览 2.5k

Android模拟器与真机测试结果不一样?

初学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>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2015-10-02 22:23
    已采纳

    就你的代码来说,唯一可能引起不同的是你的模拟器和真机上时间格式的设置的不同。你对比下
    day = calendar.get(Calendar.DAY_OF_MONTH); 这个变量的值

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题