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

首頁 > 系統 > Android > 正文

Android時間選擇器、日期選擇器實現代碼

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

本文為大家分享了兩款選擇器,一款可以針對時間進行選擇、一款可以針對日期進行選擇,供大家參考,具體內容如下

一、時間選擇器
1.1.布局

<?xml version="1.0" encoding="utf-8"?> <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.rj141.sb.kongjian.DateActivity">   <LinearLayout  android:orientation="horizontal"  android:layout_width="match_parent"  android:layout_height="wrap_content">  <TextView   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:textSize="20dp"   android:text="幾點吃飯:"   />  <TextView   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:textSize="20dp"   android:id="@+id/tv" />  </LinearLayout>  <Button  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="時間"  android:id="@+id/btndate" /> </LinearLayout> 

1.2.Java文件

public class DateActivity extends ActionBarActivity {   private Button btn;  private TextView tv;  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_date);   btn=(Button)this.findViewById(R.id.btndate);  tv= (TextView) this.findViewById(R.id.tv);  btn.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {   new TimePickerDialog(DateActivity.this, new TimePickerDialog.OnTimeSetListener() {    @Override    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {    tv.setText(String.format("%d:%d",hourOfDay,minute));    }   //0,0指的是時間,true表示是否為24小時,true為24小時制   },0,0,true).show();   }  });  } } 

效果圖:

二、日期選擇器
2.1.activity.xml

<?xml version="1.0" encoding="utf-8"?> <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.rj141.sb.kongjian.DateActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:id="@+id/tv" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日歷" android:id="@+id/btndate" /> </LinearLayout> 

2.2.DateActivity.class

public class DateActivity extends ActionBarActivity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); btn=(Button)this.findViewById(R.id.btndate); tv= (TextView) this.findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new DatePickerDialog(DateActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { tv.setText("您的出生日期是:"+String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth)); } },2000,1,2).show(); } }); } } DatePickerDialog日歷選擇器的對話框,監聽為OnDateSetListener(){..}

效果圖:

以上就是兩款Android時間選擇器、Android日期選擇器的實現代碼,希望對大家學習Android軟件編程有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贡觉县| 伊川县| 夏河县| 汝阳县| 利辛县| 高安市| 阳朔县| 博湖县| 隆昌县| 保康县| 乌兰浩特市| 麻江县| 武宁县| 永寿县| 原阳县| 会宁县| 綦江县| 武威市| 中宁县| 依兰县| 义马市| 来凤县| 宁阳县| 浏阳市| 高青县| 万全县| 肃宁县| 忻城县| 安远县| 时尚| 康马县| 当雄县| 灵璧县| 伊吾县| 涞水县| 三明市| 浙江省| 建宁县| 巴里| 资溪县| 泊头市|