Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android入門之時間日期控件

Android入門之時間日期控件

編輯:關於Android編程

package com.jkxqj.helloandroid;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.DatePicker;  
  
public class MainActivity extends Activity {  
  
    private DatePicker datePicker;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        datePicker = (DatePicker)findViewById(R.id.firstDatePicker);  
        datePicker.updateDate(2013, 8, 23);  
    }  
  
    class ButtonListener implements OnClickListener{  
        @Override  
        public void onClick(View v) {  
            int y = datePicker.getYear();  
            int m = datePicker.getMonth();  
            int d = datePicker.getDayOfMonth();  
            System.out.println("y:" + y + ",m" + m + ",d:" + d);  
        }  
          
    }  
    @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;  
    }  
  
}  

package com.jkxqj.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;

public class MainActivity extends Activity {

	private DatePicker datePicker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		datePicker = (DatePicker)findViewById(R.id.firstDatePicker);
		datePicker.updateDate(2013, 8, 23);
	}

	class ButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			int y = datePicker.getYear();
			int m = datePicker.getMonth();
			int d = datePicker.getDayOfMonth();
			System.out.println("y:" + y + ",m" + m + ",d:" + d);
		}
		
	}
	@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;
	}

}

activity_main.xml

 

<RelativeLayout 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: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=".MainActivity" > 
 
    <AnalogClock 
        android:id="@+id/clock" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:layout_marginLeft="64dp" /> 
 
    <DatePicker 
        android:id="@+id/firstDatePicker" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/clock" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="17dp" /> 
     

</RelativeLayout> 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved