亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android日期選擇控件使用詳解

 更新時間:2020年08月26日 15:24:25   作者:抱著回憶旅行  
這篇文章主要為大家詳細介紹了Android日期選擇控件的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android日期選擇控件的使用方法,供大家參考,具體內容如下

1.創(chuàng)建dialog 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center">
 
  <DatePicker
    android:datePickerMode="spinner"
    android:calendarViewShown="false"
    android:id="@+id/date_picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  </DatePicker>
 
</LinearLayout>

2.MainActivity

public class MainActivity extends AppCompatActivity {
 
  private TextView date;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    date = findViewById(R.id.date);
 
    //點擊"日期"按鈕布局 設置日期
    date.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //通過自定義控件AlertDialog實現(xiàn)
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        View view = (LinearLayout) getLayoutInflater().inflate(R.layout.date_dialog, null);
        final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
        //設置日期簡略顯示 否則詳細顯示 包括:星期\周
        datePicker.setCalendarViewShown(false);
        //設置date布局
        builder.setView(view);
        builder.setTitle("選擇出生日期");
        builder.setPositiveButton("確 定", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            //日期格式
            int year = datePicker.getYear();
            int month = datePicker.getMonth()+1;
            int dayOfMonth = datePicker.getDayOfMonth();
            date.setText(year+"年"+month+"月"+dayOfMonth+"日");
            dialog.cancel();
          }
        });
        builder.setNegativeButton("取 消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
          }
        });
        builder.create().show();
      }
    });
  }
}

3.activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
 
 
  <TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2020年06月01日"></TextView>

</LinearLayout> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論