Android 날짜 선택 기 년 월 일 3 단계 연동 실현

최근 프로젝트 에 날짜 선택 기 를 사용 하여 현재 주류 인 WheelView 슬라이딩 선택 을 실현 하고 정리 한 다음 에 Demo 를 만 들 었 습 니 다.쓸데없는 말 을 하지 않 고 코드 를 직접 올 렸 습 니 다.
주 레이아웃:activitymain.xml

<?xml version="1.0" encoding="utf-8"?> 
<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="com.example.user.mydate.MainActivity"> 
 
 <TextView 
  android:id="@+id/selectDate" 
  android:layout_width="match_parent" 
  android:layout_height="30dp" 
  android:gravity="center" 
  android:onClick="selectDate" 
  android:clickable="true" 
  android:text="    " /> 
</RelativeLayout> 
MainActivity:

package com.example.user.mydate; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.Window; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import com.example.user.mydate.view.ChangeDatePopwindow; 
 
public class MainActivity extends Activity { 
 
 private TextView selectDate; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  setContentView(R.layout.activity_main); 
  selectDate= (TextView) findViewById(R.id.selectDate); 
 } 
 
 public void selectDate(View v){ 
  if (v.getId()==R.id.selectDate){ 
   selectDate(); 
  } 
 
 } 
 
 
 private String[] selectDate() { 
  final String[] str = new String[10]; 
  ChangeDatePopwindow mChangeBirthDialog = new ChangeDatePopwindow( 
    this); 
  mChangeBirthDialog.setDate("2016", "1", "1"); 
  mChangeBirthDialog.showAtLocation(selectDate, Gravity.BOTTOM, 0, 0); 
  mChangeBirthDialog.setBirthdayListener(new ChangeDatePopwindow.OnBirthListener() { 
 
   @Override 
   public void onClick(String year, String month, String day) { 
    // TODO Auto-generated method stub 
    Toast.makeText(MainActivity.this,year + "-" + month + "-" + day,Toast.LENGTH_LONG).show(); 
    StringBuilder sb = new StringBuilder(); 
    sb.append(year.substring(0, year.length() - 1)).append("-").append(month.substring(0, day.length() - 1)).append("-").append(day); 
    str[0] = year + "-" + month + "-" + day; 
    str[1] = sb.toString(); 
    selectDate.setText(year + "-" + month + "-" + day); 
 
   } 
  }); 
  return str; 
 } 
 
} 
주요 클래스:

package com.example.user.mydate.view; 
 
import android.content.Context; 
import android.graphics.drawable.ColorDrawable; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.PopupWindow; 
import android.widget.TextView; 
 
import com.example.user.mydate.R; 
import com.example.user.mydate.view.wheelview.OnWheelChangedListener; 
import com.example.user.mydate.view.wheelview.OnWheelScrollListener; 
import com.example.user.mydate.view.wheelview.WheelView; 
import com.example.user.mydate.view.wheelview.adapter.AbstractWheelTextAdapter1; 
 
import java.util.ArrayList; 
import java.util.Calendar; 
 
/** 
 * Author: Chen.yuan 
 * Email: [email protected] 
 * Date: 2016/7/28 17:37 
 * Description:    window 
 */ 
public class ChangeDatePopwindow extends PopupWindow implements View.OnClickListener { 
 
 private Context context; 
 private WheelView wvYear; 
 private WheelView wvMonth; 
 private WheelView wvDay; 
 
 private TextView btnSure; 
 private TextView btnCancel; 
 
 private ArrayList<String> arry_years = new ArrayList<String>(); 
 private ArrayList<String> arry_months = new ArrayList<String>(); 
 private ArrayList<String> arry_days = new ArrayList<String>(); 
 private CalendarTextAdapter mYearAdapter; 
 private CalendarTextAdapter mMonthAdapter; 
 private CalendarTextAdapter mDaydapter; 
 
 private String month; 
 private String day; 
 
 private String currentYear = getYear(); 
 private String currentMonth = getMonth(); 
 private String currentDay = getDay(); 
 
 private int maxTextSize = 24; 
 private int minTextSize = 14; 
 
 private boolean issetdata = false; 
 
 private String selectYear; 
 private String selectMonth; 
 private String selectDay; 
 
 private OnBirthListener onBirthListener; 
 
 public ChangeDatePopwindow(final Context context) { 
  super(context); 
  this.context = context; 
  View view=View.inflate(context, R.layout.dialog_myinfo_changebirth,null); 
  wvYear = (WheelView) view.findViewById(R.id.wv_birth_year); 
  wvMonth = (WheelView) view.findViewById(R.id.wv_birth_month); 
  wvDay = (WheelView) view.findViewById(R.id.wv_birth_day); 
  btnSure = (TextView) view.findViewById(R.id.btn_myinfo_sure); 
  btnCancel = (TextView) view.findViewById(R.id.btn_myinfo_cancel); 
 
  //  SelectPicPopupWindow View 
  this.setContentView(view); 
  //  SelectPicPopupWindow       
  this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); 
  //  SelectPicPopupWindow       
  this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT); 
  //  SelectPicPopupWindow        
  this.setFocusable(true); 
  //  SelectPicPopupWindow         
//  this.setAnimationStyle(R.style.AnimBottom); 
  //     ColorDrawable       
  ColorDrawable dw = new ColorDrawable(0xb0000000); 
  //  SelectPicPopupWindow        
  this.setBackgroundDrawable(dw); 
 
  btnSure.setOnClickListener(this); 
  btnCancel.setOnClickListener(this); 
 
  if (!issetdata) { 
   initData(); 
  } 
  initYears(); 
  mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize); 
  wvYear.setVisibleItems(5); 
  wvYear.setViewAdapter(mYearAdapter); 
  wvYear.setCurrentItem(setYear(currentYear)); 
 
  initMonths(Integer.parseInt(month)); 
  mMonthAdapter = new CalendarTextAdapter(context, arry_months, setMonth(currentMonth), maxTextSize, minTextSize); 
  wvMonth.setVisibleItems(5); 
  wvMonth.setViewAdapter(mMonthAdapter); 
  wvMonth.setCurrentItem(setMonth(currentMonth)); 
 
  initDays(Integer.parseInt(day)); 
  mDaydapter = new CalendarTextAdapter(context, arry_days, Integer.parseInt(currentDay) - 1, maxTextSize, minTextSize); 
  wvDay.setVisibleItems(5); 
  wvDay.setViewAdapter(mDaydapter); 
  wvDay.setCurrentItem(Integer.parseInt(currentDay) - 1); 
 
  wvYear.addChangingListener(new OnWheelChangedListener() { 
 
   @Override 
   public void onChanged(WheelView wheel, int oldValue, int newValue) { 
    // TODO Auto-generated method stub 
    String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem()); 
    selectYear = currentText; 
    setTextviewSize(currentText, mYearAdapter); 
    currentYear = currentText.substring(0, currentText.length()-1).toString(); 
    Log.d("currentYear==",currentYear); 
    setYear(currentYear); 
    initMonths(Integer.parseInt(month)); 
    mMonthAdapter = new CalendarTextAdapter(context, arry_months, 0, maxTextSize, minTextSize); 
    wvMonth.setVisibleItems(5); 
    wvMonth.setViewAdapter(mMonthAdapter); 
    wvMonth.setCurrentItem(0); 
 
    calDays(currentYear, month); 
   } 
  }); 
 
  wvYear.addScrollingListener(new OnWheelScrollListener() { 
 
   @Override 
   public void onScrollingStarted(WheelView wheel) { 
    // TODO Auto-generated method stub 
 
   } 
 
   @Override 
   public void onScrollingFinished(WheelView wheel) { 
    // TODO Auto-generated method stub 
    String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem()); 
    setTextviewSize(currentText, mYearAdapter); 
   } 
  }); 
 
  wvMonth.addChangingListener(new OnWheelChangedListener() { 
 
   @Override 
   public void onChanged(WheelView wheel, int oldValue, int newValue) { 
    // TODO Auto-generated method stub 
    String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem()); 
    selectMonth = currentText; 
    setTextviewSize(currentText, mMonthAdapter); 
    setMonth(currentText.substring(0, 1)); 
    initDays(Integer.parseInt(day)); 
    mDaydapter = new CalendarTextAdapter(context, arry_days, 0, maxTextSize, minTextSize); 
    wvDay.setVisibleItems(5); 
    wvDay.setViewAdapter(mDaydapter); 
    wvDay.setCurrentItem(0); 
 
    calDays(currentYear, month); 
   } 
  }); 
 
  wvMonth.addScrollingListener(new OnWheelScrollListener() { 
 
   @Override 
   public void onScrollingStarted(WheelView wheel) { 
    // TODO Auto-generated method stub 
 
   } 
 
   @Override 
   public void onScrollingFinished(WheelView wheel) { 
    // TODO Auto-generated method stub 
    String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem()); 
    setTextviewSize(currentText, mMonthAdapter); 
   } 
  }); 
 
  wvDay.addChangingListener(new OnWheelChangedListener() { 
 
   @Override 
   public void onChanged(WheelView wheel, int oldValue, int newValue) { 
    // TODO Auto-generated method stub 
    String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem()); 
    setTextviewSize(currentText, mDaydapter); 
    selectDay = currentText; 
   } 
  }); 
 
  wvDay.addScrollingListener(new OnWheelScrollListener() { 
 
   @Override 
   public void onScrollingStarted(WheelView wheel) { 
    // TODO Auto-generated method stub 
 
   } 
 
   @Override 
   public void onScrollingFinished(WheelView wheel) { 
    // TODO Auto-generated method stub 
    String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem()); 
    setTextviewSize(currentText, mDaydapter); 
   } 
  }); 
 } 
 
 
 public void initYears() { 
  for (int i = Integer.parseInt(getYear()); i > 1950; i--) { 
   arry_years.add(i + " "); 
  } 
 } 
 
 public void initMonths(int months) { 
  arry_months.clear(); 
  for (int i = 1; i <= months; i++) { 
   arry_months.add(i + " "); 
  } 
 } 
 
 public void initDays(int days) { 
  arry_days.clear(); 
  for (int i = 1; i <= days; i++) { 
   arry_days.add(i + " "); 
  } 
 } 
 
 private class CalendarTextAdapter extends AbstractWheelTextAdapter1 { 
  ArrayList<String> list; 
 
  protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) { 
   super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize); 
   this.list = list; 
   setItemTextResource(R.id.tempValue); 
  } 
 
  @Override 
  public View getItem(int index, View cachedView, ViewGroup parent) { 
   View view = super.getItem(index, cachedView, parent); 
   return view; 
  } 
 
  @Override 
  public int getItemsCount() { 
   return list.size(); 
  } 
 
  @Override 
  protected CharSequence getItemText(int index) { 
   return list.get(index) + ""; 
  } 
 } 
 
 public void setBirthdayListener(OnBirthListener onBirthListener) { 
  this.onBirthListener = onBirthListener; 
 } 
 
 @Override 
 public void onClick(View v) { 
 
  if (v == btnSure) { 
   if (onBirthListener != null) { 
    onBirthListener.onClick(selectYear, selectMonth, selectDay); 
    Log.d("cy",""+selectYear+""+selectMonth+""+selectDay); 
   } 
  } else if (v == btnSure) { 
 
  } else { 
   dismiss(); 
  } 
  dismiss(); 
 
 } 
 
 public interface OnBirthListener { 
  public void onClick(String year, String month, String day); 
 } 
 
 /** 
  *        
  * 
  * @param curriteItemText 
  * @param adapter 
  */ 
 public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) { 
  ArrayList<View> arrayList = adapter.getTestViews(); 
  int size = arrayList.size(); 
  String currentText; 
  for (int i = 0; i < size; i++) { 
   TextView textvew = (TextView) arrayList.get(i); 
   currentText = textvew.getText().toString(); 
   if (curriteItemText.equals(currentText)) { 
    textvew.setTextSize(maxTextSize); 
   } else { 
    textvew.setTextSize(minTextSize); 
   } 
  } 
 } 
 
 public String getYear() { 
  Calendar c = Calendar.getInstance(); 
  return c.get(Calendar.YEAR)+""; 
 } 
 
 public String getMonth() { 
  Calendar c = Calendar.getInstance(); 
  return c.get(Calendar.MONTH) + 1+""; 
 } 
 
 public String getDay() { 
  Calendar c = Calendar.getInstance(); 
  return c.get(Calendar.DATE)+""; 
 } 
 
 public void initData() { 
  setDate(getYear(), getMonth(), getDay()); 
  this.currentDay = 1+""; 
  this.currentMonth = 1+""; 
 } 
 
 /** 
  *       
  * 
  * @param year 
  * @param month 
  * @param day 
  */ 
 public void setDate(String year, String month, String day) { 
  selectYear = year + " "; 
  selectMonth = month + " "; 
  selectDay = day + " "; 
  issetdata = true; 
  this.currentYear = year; 
  this.currentMonth = month; 
  this.currentDay = day; 
  if (year == getYear()) { 
   this.month = getMonth(); 
  } else { 
   this.month = 12+""; 
  } 
  calDays(year, month); 
 } 
 
 /** 
  *      
  * 
  * @param year 
  */ 
 public int setYear(String year) { 
  int yearIndex = 0; 
  if (!year.equals(getYear())) { 
   this.month = 12+""; 
  } else { 
   this.month = getMonth(); 
  } 
  for (int i = Integer.parseInt(getYear()); i > 1950; i--) { 
   if (i == Integer.parseInt(year)) { 
    return yearIndex; 
   } 
   yearIndex++; 
  } 
  return yearIndex; 
 } 
 
 /** 
  *      
  * 
  * @param month 
  * @param month 
  * @return 
  */ 
 public int setMonth(String month) { 
  int monthIndex = 0; 
  calDays(currentYear, month); 
  for (int i = 1; i < Integer.parseInt(this.month); i++) { 
   if (Integer.parseInt(month) == i) { 
    return monthIndex; 
   } else { 
    monthIndex++; 
   } 
  } 
  return monthIndex; 
 } 
 
 /** 
  *         
  * 
  * @param month 
  * @param year 
  */ 
 public void calDays(String year, String month) { 
  boolean leayyear = false; 
  if (Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0) { 
   leayyear = true; 
  } else { 
   leayyear = false; 
  } 
  for (int i = 1; i <= 12; i++) { 
   switch (Integer.parseInt(month)) { 
   case 1: 
   case 3: 
   case 5: 
   case 7: 
   case 8: 
   case 10: 
   case 12: 
    this.day = 31+""; 
    break; 
   case 2: 
    if (leayyear) { 
     this.day = 29+""; 
    } else { 
     this.day = 28+""; 
    } 
    break; 
   case 4: 
   case 6: 
   case 9: 
   case 11: 
    this.day = 30+""; 
    break; 
   } 
  } 
  if (year.equals( getYear()) && month .equals( getMonth())) { 
   this.day = getDay(); 
  } 
 } 
}
아래쪽 팝 업 상자 레이아웃:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ly_myinfo_changeaddress" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/color_transparent" 
android:gravity="bottom" 
android:orientation="vertical" > 
 
<LinearLayout 
 android:id="@+id/ly_myinfo_changeaddress_child" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_alignParentBottom="true" 
 android:background="@color/white" 
 android:orientation="vertical" > 
 
 
 <RelativeLayout 
  android:layout_width="match_parent" 
  android:layout_height="44dp" 
  > 
 
  <TextView 
   android:id="@+id/btn_myinfo_cancel" 
   android:layout_width="wrap_content" 
   android:layout_height="match_parent" 
   android:paddingLeft="12dp" 
   android:text="  " 
   android:gravity="center" 
   android:layout_alignParentLeft="true" 
   android:layout_marginRight="15dip" 
   android:textColor="@color/color_e84515" 
   android:textSize="13sp" /> 
 
 
  <TextView 
   android:id="@+id/btn_myinfo_sure" 
   android:layout_width="wrap_content" 
   android:layout_height="match_parent" 
   android:layout_alignParentRight="true" 
   android:gravity="center" 
   android:text="  " 
   android:textColor="@color/color_e84515" 
   android:paddingRight="12dp" 
   android:textSize="12sp" /> 
 
 </RelativeLayout> 
 <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="0.5dp" 
  android:background="@color/color_d8d8d8" 
  /> 
 <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="160dip" 
  android:orientation="horizontal" 
  android:gravity="center_vertical"> 
  <com.example.user.mydate.view.wheelview.WheelView 
   android:id="@+id/wv_birth_year" 
   android:layout_width="0dip" 
   android:layout_weight="1" 
   android:layout_height="match_parent" 
   android:layout_gravity="center_vertical" 
   /> 
  <com.example.user.mydate.view.wheelview.WheelView 
   android:id="@+id/wv_birth_month" 
   android:layout_width="0dip" 
   android:layout_weight="1" 
   android:layout_height="match_parent" 
   android:layout_gravity="center_vertical" 
   /> 
 
  <com.example.user.mydate.view.wheelview.WheelView 
   android:id="@+id/wv_birth_day" 
   android:layout_width="0dip" 
   android:layout_weight="1" 
   android:layout_height="match_parent" 
   android:layout_gravity="center_vertical" 
   /> 
 </LinearLayout> 
 
</LinearLayout> 
 
</LinearLayout> 
실행 효과 그림:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기