zoukankan      html  css  js  c++  java
  • A10_DatePicker的对话框设置

    1.这次的学习主要是,弥补上一篇文章:A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的设置 

    2.java代码中设置DatePicker,使用OnDateSetListener监听器接口监听对话框的动作。

    代码比较简单,主要是初期的熟悉和学习。

    3.注意,在DatePicker中,月份是从0开始编号的,但是日是从1开始编号的。

    效果图:


    java代码:

    package com.example.a10_datepicker;
    
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	private Button button;
    	private static final int DATE_PICKER_ID = 1;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		button = (Button)findViewById(R.id.buttonId);
    		button.setOnClickListener(new ButtonListener());
    	}
    
    	// 设置DatePicker对话框的监听器
    	DatePickerDialog.OnDateSetListener datePickerListener = new OnDateSetListener() {
    
    		public void onDateSet(DatePicker view, int year, int monthOfYear,
    				int dayOfMonth) {
    			Toast.makeText(MainActivity.this,
    					year + "年" + monthOfYear + "月" + dayOfMonth + "日",
    					Toast.LENGTH_SHORT).show();
    		}
    	};
    	//实现按钮监听器
    	class ButtonListener implements OnClickListener{
    
    		public void onClick(View v) {
    			//该方法用于显示对话框,是继承Activity中的方法,执行该方法系统就会调用onCreateDialog()方法
    			showDialog(DATE_PICKER_ID);
    		}
    
    	}
    	//复写该方法显示对话框
    	@Override
    	protected Dialog onCreateDialog(int id) {
    
    		if(id == DATE_PICKER_ID){
    			return new DatePickerDialog(this,datePickerListener,2012,6,1);
    		}
    		return null;
    	}
    	
    	
    
    }
    


  • 相关阅读:
    字符乱码问题
    一个利用go反向代理解决api转发的例子(go反向代理)
    udp绑定信息
    python3编码转换(字符串/字节码)——Python
    网络UDP——Python
    常用服务器ftp、ssh——Python
    Linux寻找国内镜像源——Python
    常用 Linux 命令的基本使用(二)——Python
    Linux 主要目录速查表——Python
    飞机大战——Python
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3134662.html
Copyright © 2011-2022 走看看