国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中TimePicker與DatePicker時間日期選擇組件的使用實例

2019-12-12 06:31:12
字體:
來源:轉載
供稿:網友

效果和代碼都非常直觀:

實例1:TimePicker

2016423120510814.jpg (391×590)

<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" >         <TimePicker      android:id="@+id/timePic1"     android:layout_height="wrap_content"     android:layout_width="match_parent"/>   <Button      android:id="@+id/buttone1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_below="@id/timePic1"     android:text="獲取TimePick時間"/>    </RelativeLayout> 
package com.android.xiong.times;  import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener;  public class MainActivity extends Activity {         private TimePicker timePick1;   private Button buttone1;    @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     timePick1=(TimePicker)findViewById(R.id.timePic1);          buttone1=(Button)findViewById(R.id.buttone1);     OnChangeListener buc=new OnChangeListener();     buttone1.setOnClickListener(buc);     //是否使用24小時制     timePick1.setIs24HourView(true);     TimeListener times=new TimeListener();     timePick1.setOnTimeChangedListener(times);   }      class OnChangeListener implements OnClickListener{     @Override     public void onClick(View v) {       // TODO Auto-generated method stub       int h=timePick1.getCurrentHour();       int m=timePick1.getCurrentMinute();       System.out.println("h:"+h+"  m:"+m);     }   }   class TimeListener implements OnTimeChangedListener{          /**      * view 當前選中TimePicker控件      * hourOfDay 當前控件選中TimePicker 的小時      * minute 當前選中控件TimePicker 的分鐘      */     @Override     public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {       // TODO Auto-generated method stub       System.out.println("h:"+ hourOfDay +" m:"+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;   }  } 

實例2:DatePicker

2016423120641619.jpg (466×524)

<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" >    <DatePicker      android:id="@+id/datePick1"     android:layout_height="wrap_content"     android:layout_width="match_parent"     />   <Button      android:id="@+id/button1"     android:layout_below="@id/datePick1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="獲取DatePicker的值"/>  </RelativeLayout> 
package com.android.xiong.datepicker;  import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker;  public class MainActivity extends Activity {    private DatePicker datePicker1;   private Button button1;   @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     datePicker1=(DatePicker)findViewById(R.id.datePick1);     //設置默認的時間 比如2055年 9月9日     datePicker1.updateDate(2012, 8, 9);     button1=(Button)findViewById(R.id.button1);     OnClicLisers cl=new OnClicLisers();     button1.setOnClickListener(cl);   }    class OnClicLisers implements OnClickListener{     @Override     public void onClick(View v) {       // TODO Auto-generated method stub       int y=datePicker1.getYear();       int m=datePicker1.getMonth()+1;       int d=datePicker1.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;   }  } 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富平县| 南宫市| 清苑县| 禄劝| 浑源县| 洛扎县| 张家口市| 乾安县| 公安县| 阳春市| 平顺县| 定边县| 曲周县| 交口县| 南投市| 革吉县| 成武县| 珲春市| 龙里县| 郑州市| 育儿| 石阡县| 霍山县| 肃南| 怀集县| 海兴县| 郓城县| 浦北县| 九龙城区| 峡江县| 会同县| 曲松县| 越西县| 德昌县| 武陟县| 洪洞县| 曲阜市| 洛南县| 阳高县| 兰西县| 南溪县|