为了账号安全,请及时绑定邮箱和手机立即绑定

修改时间后,Title就是不变,大神搭救哦!

package com.example.dateortime;


import java.util.Calendar;


import javax.security.auth.PrivateCredentialPermission;


import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.DatePicker;

import android.widget.DatePicker.OnDateChangedListener;

import android.widget.TimePicker;

import android.widget.TimePicker.OnTimeChangedListener;

import android.widget.Toast;



public class MainActivity extends ActionBarActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        TimePicker timePicker;

        final DatePicker datePicker;

        Calendar cal;

        int year;

        int month;

        int day;

        int hour;

        int minute;

        

        //获取日历的对象

        cal = Calendar.getInstance();

        //获取年月日时分秒的信息

        year = cal.get(Calendar.YEAR);

        month = cal.get(Calendar.MONTH)+1;

        day = cal.get(Calendar.DAY_OF_MONTH);

        hour = cal.get(Calendar.HOUR_OF_DAY);

        minute = cal.get(Calendar.MINUTE);

        setTitle(year+"-"+month+"-"+day+" "+hour+":"+minute);

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

        timePicker = (TimePicker) findViewById(R.id.timePicker);

        //

        datePicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() {

@Override

public void onDateChanged(DatePicker view, int year1, int monthOfYear, int dayOfMonth) {

// TODO Auto-generated method stub

       setTitle(year1+"-"+monthOfYear+"-"+dayOfMonth);

       //这里测试toast都可以显示修改后日期的,但是setTitle就是没用,咋破?

       String text = year1+"-"+(monthOfYear+1)+"-"+dayOfMonth;

       Toast.makeText(datePicker.getContext(), text, 5).show();

}

});

        

        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

@Override

public void onTimeChanged(TimePicker arg0, int hourOfDay, int minute1) {

// TODO Auto-generated method stub

setTitle(hourOfDay+":"+minute1);

}

});

    }

}


正在回答

1 回答


String text = year1+"-"+(monthOfYear+1)+"-"+dayOfMonth;

setTitle(text );

Toast.makeText(datePicker.getContext(), text, 5).show();

你可以吧事件写成这样试试

1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第二门课(第1季)
  • 参与学习       111161    人
  • 解答问题       1457    个

本课程由浅入深地带您学会Android的常用控件的开发和使用

进入课程

修改时间后,Title就是不变,大神搭救哦!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信