安卓app_sl3_21日期拾取器,为时间拾取器设置监听器

安卓app_sl3_21日期拾取器,为时间拾取器设置监听器

 

 

复制代码
package com.example.sl3_21;

import java.util.Calendar;

import android.app.Activity;
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 Activity {
    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_main);
        DatePicker datepicker=(DatePicker)findViewById(R.id.datePicker1);
        TimePicker timepicker=(TimePicker)findViewById(R.id.timePicker1);
        timepicker.setIs24HourView(true);
        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_OF_DAY);
        minute=calendar.get(Calendar.MINUTE);
        //初始化日期拾取器,并在初始化时指定监听器
        datepicker.init(year, month, day, new OnDateChangedListener(){

            @Override
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                // TODO 自动生成的方法存根
                MainActivity.this.year=year;
                MainActivity.this.month=month;
                MainActivity.this.day=day;
                show(year,month,day,hour,minute);
            }
            
            
        });
        //为时间拾取器设置监听器
        timepicker.setOnTimeChangedListener(new OnTimeChangedListener(){

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                // TODO 自动生成的方法存根
                
                MainActivity.this.hour=hourOfDay;
                MainActivity.this.minute=minute;
                show(year,month,day,hourOfDay,minute);
            }
            
        });
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void show(int year,int month,int day,int hour,int minute)
    {
        String str=year+"年"+(month+1)+"月"+day+"日 "+hour+":"+minute;
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}
复制代码
复制代码
<LinearLayout 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:orientation="vertical"
    
    tools:context="com.example.sl3_21.MainActivity" >

     <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
         />
    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        />

   

</LinearLayout>
复制代码

 

posted @   txwtech  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2020-02-19 cb30a_c++_STL_算法_查找算法_(3)search_find_end
2020-02-19 cb29a_c++_STL_算法_查找算法_(2)search_n
2020-02-19 cb28a_c++_STL_算法_查找算法_(1)find_find_if
2020-02-19 cb27a_c++_STL_算法_最小值和最大值
2020-02-19 cb26a_c++_STL_算法_元素计数
点击右上角即可分享
微信分享提示