先声明,whell控件是看别人的,最后自己改的,不知道是那位大哥写的了,忘了
package com.sxyrd.yrdapp.ui;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sxyrd.yrdapp.R;
import com.sxyrd.yrdapp.utils.ActivityManager;
import com.sxyrd.yrdapp.utils.Utils;
import com.sxyrd.yrdapp.widget.CleanableEditText;
import com.sxyrd.yrdapp.widget.PickerView;
import com.sxyrd.yrdapp.widget.TimeButton;
import com.sxyrd.yrdapp.widget.wheelview.OnWheelScrollListener;
import com.sxyrd.yrdapp.widget.wheelview.WheelView;
import com.sxyrd.yrdapp.widget.wheelview.adapter.NumericWheelAdapter;
import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by fly0116 2016/03/07.
*/
public class ReserveActivity extends KJActivity {
@BindView(id = R.id.titlebar_img_back, click = true)
private View backView;
@BindView(id = R.id.reserve_time, click = true)
private TextView reserve_time;
@BindView(id = R.id.get_code_bt, click = true)
private TimeButton getCodeBt;
@BindView(id = R.id.phone_et)
private CleanableEditText phoneet;
@BindView(id = R.id.sms_code_et)
private EditText smsCodeEt;
@BindView(id = R.id.reserve_number_people)
private EditText reserve_number_people;
@BindView(id = R.id.reserve_promptly_btn, click = true)
private View reserve_promptly_btn;
@BindView(id = R.id.ll)
private LinearLayout linearLayout;
private LayoutInflater inflater = null;
// private WheelView year;
private WheelView month;
private WheelView day;
// private WheelView time;
private WheelView hour;
private WheelView min;
private TextView weekTv;
// private int mYear=1996;
// private int mMonth=0;
// private int mDay=1;
LinearLayout ll;
View view = null;
PickerView minute_pv;
PickerView second_pv;
// boolean isMonthSetted=false,isDaySetted=false;
private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)";
public ReserveActivity() {
}
@Override
public void setRootView() {
setContentView(R.layout.yrd_reserve_view);
ActivityManager.getActivityManager().addActivity(this);
}
@Override
public void widgetClick(View v) {
super.widgetClick(v);
redirectTo(v.getId());
}
@Override
public void initData() {
super.initData();
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
ll = (LinearLayout) findViewById(R.id.ll);
ll.addView(getDataPick());
}
@Override
public void initWidget() {
super.initWidget();
getCodeBt.setEnabled(false);
phoneet.setOnClickListener(this);
phoneet.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
phoneError();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
phoneError();
}
@Override
public void afterTextChanged(Editable s) {
}
});
reserve_time.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
checkReserveBtn();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkReserveBtn();
}
@Override
public void afterTextChanged(Editable s) {
}
});
smsCodeEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
checkReserveBtn();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkReserveBtn();
}
@Override
public void afterTextChanged(Editable s) {
}
});
reserve_number_people.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
checkReserveBtn();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkReserveBtn();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void checkReserveBtn(){
if (isPhoneEmpty()&&isCodeEmpty()&&isPeopleEmpty()&&phoneError()){
reserve_promptly_btn.setEnabled(true);
reserve_promptly_btn.setBackgroundResource(R.drawable.yuding2);
}else{
reserve_promptly_btn.setEnabled(false);
reserve_promptly_btn.setBackgroundResource(R.drawable.yuding1);
}
}
private void redirectTo(int vid) {
switch (vid) {
case R.id.reserve_time:
// DateTimePickerDialog dateTimePickerDialog=new DateTimePickerDialog(this);
// dateTimePickerDialog.dateTimePicKDialog(reserve_time,0);
break;
case R.id.titlebar_img_back:
finish();
break;
case R.id.get_code_bt:
break;
case R.id.reserve_promptly_btn:
doReserve();
break;
}
}
private boolean isPhoneEmpty(){
if (StringUtils.isEmpty(phoneet.getText().toString())) {
ViewInject.toast(getString(R.string.phone_error));
return false;
}
return true;
}
private boolean isCodeEmpty(){
if (StringUtils.isEmpty(smsCodeEt.getText().toString())) {
ViewInject.toast(getString(R.string.sms_code_no_empty));
return false;
}
return true;
}
private boolean isPeopleEmpty(){
if (StringUtils.isEmpty(reserve_number_people.getText().toString())) {
ViewInject.toast(getString(R.string.people_number));
return false;
}
return true;
}
private boolean phoneInputCheck() {
phoneError();
if (StringUtils.isEmpty(smsCodeEt.getText().toString())) {
ViewInject.toast(getString(R.string.sms_code_no_empty));
return false;
}
return true;
}
private boolean phoneError() {
if (StringUtils.isEmpty(phoneet.getText().toString())) {
phoneet.setFocusable(true);
phoneet.setFocusableInTouchMode(true);
phoneet.requestFocus();
phoneet.requestFocusFromTouch();
getCodeBt.setEnabled(false);
getCodeBt.setBackgroundResource(R.drawable.code1);
return false;
}
if (!StringUtils.isEmpty(phoneet.getText().toString())) {
if (phoneet.getText().toString().trim().length() != 11) {
phoneet.setFocusable(true);
phoneet.setFocusableInTouchMode(true);
phoneet.requestFocus();
phoneet.requestFocusFromTouch();
getCodeBt.setEnabled(false);
getCodeBt.setBackgroundResource(R.drawable.code1);
return false;
} else {
if (!Utils.isMobileNO(phoneet.getText().toString().trim())) {
ViewInject.toast("手机号格式错误");
getCodeBt.setEnabled(false);
getCodeBt.setBackgroundResource(R.drawable.code1);
return false;
}
}
}
getCodeBt.setEnabled(true);
getCodeBt.setBackgroundResource(R.drawable.code2);
return true;
}
private void doReserve() {
if (!phoneInputCheck()) {
return;
}
}
/**
* 必须返回 return super.dispatchKeyEvent(event);,
* 则如返回return true时,绑定的删除键则无效
*
* @param event
* @return
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return super.dispatchKeyEvent(event);
}
/**
* 匹配短信中间的6个数字(验证码等)
*
* @param patternContent
* @return
*/
private String patternCode(String patternContent) {
if (TextUtils.isEmpty(patternContent)) {
return null;
}
Pattern p = Pattern.compile(patternCoder);
Matcher matcher = p.matcher(patternContent);
if (matcher.find()) {
return matcher.group();
}
return null;
}
private int today = 0;
private int toMonth = 0;
private int toYear = 0;
private View getDataPick() {
Calendar c = Calendar.getInstance();
int norYear = c.get(Calendar.YEAR);
toYear = c.get(Calendar.YEAR);
int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
int curDate = c.get(Calendar.DATE);
today = c.get(Calendar.DATE);
toMonth = c.get(Calendar.MONTH) + 1;
int curTime = c.get(Calendar.HOUR_OF_DAY);
int curMinute = c.get(Calendar.MINUTE);
view = inflater.inflate(R.layout.wheel_date_picker, null);
month = (WheelView) view.findViewById(R.id.month);
NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(this, 1, 12, "%02d");
numericWheelAdapter2.setLabel("月");
month.setViewAdapter(numericWheelAdapter2);
month.setCyclic(true);
month.addScrollingListener(scrollListener);
day = (WheelView) view.findViewById(R.id.day);
initDay(norYear, curMonth);
day.setCyclic(true);
day.addScrollingListener(scrollListener);
weekTv = (TextView) view.findViewById(R.id.week);
weekTv.setText(Utils.getWeek());
hour = (WheelView) view.findViewById(R.id.hour);
NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(this, 0, 23, "%02d");
numericWheelAdapter3.setLabel("时");
hour.setViewAdapter(numericWheelAdapter3);
hour.setCyclic(true);
hour.addScrollingListener(scrollListener);
min = (WheelView) view.findViewById(R.id.min);
NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(this, 0, 59, "%02d");
numericWheelAdapter4.setLabel("分");
min.setViewAdapter(numericWheelAdapter4);
min.setCyclic(true);
min.addScrollingListener(scrollListener);
month.setVisibleItems(3);
day.setVisibleItems(3);
hour.setVisibleItems(3);
min.setVisibleItems(3);
month.setCurrentItem(curMonth - 1);
day.setCurrentItem(curDate - 1);
hour.setCurrentItem(curTime);
min.setCurrentItem(curMinute);
return view;
}
OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
@Override
public void onScrollingStarted(WheelView wheel) {
}
@Override
public void onScrollingFinished(WheelView wheel) {
if (month.getCurrentItem() != (toMonth - 1)) {
int days = getDay(toYear, toMonth);
if (month.getCurrentItem() == (toMonth)) {
if ((today + 7) > days) {
if (day.getCurrentItem() > today + 7 - days - 1) {
day.setCurrentItem(today + 7 - days - 1);
}
//if (today+7-days-1)
} else {
month.setCurrentItem(toMonth - 1);
}
}
if (month.getCurrentItem() > toMonth || month.getCurrentItem() < (toMonth - 1)) {
month.setCurrentItem(toMonth - 1);
day.setCurrentItem(today - 1);
}
} else {
if (day.getCurrentItem() > (today + 6)) {
day.setCurrentItem(today + 6);
}
if (day.getCurrentItem() < (today - 1)) {
day.setCurrentItem(today - 1);
}
}
if (day.getCurrentItem() == (today - 1)) {
if (hour.getCurrentItem() < getHour()) {
hour.setCurrentItem(getHour());
}
if (hour.getCurrentItem() == getHour()) {
if (min.getCurrentItem() < getMinute()) {
min.setCurrentItem(getMinute());
}
}
}
String birthday = new StringBuilder().append(Utils.getYear()).append("-").
append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).
append("-").append(((day.getCurrentItem() + 1) < 10) ? "0" + (day.getCurrentItem() + 1) : (day.getCurrentItem() + 1)).
append(" ").append((hour.getCurrentItem() < 10 ? "0" + hour.getCurrentItem() : hour.getCurrentItem())).
append(":").append((min.getCurrentItem() < 10 ? "0" + min.getCurrentItem() : min.getCurrentItem())).toString();
reserve_time.setText(birthday);
String mWeek = getWeek(birthday);
weekTv.setText(mWeek);
}
};
private int getHour() {
Calendar c = Calendar.getInstance();
int curTime = c.get(Calendar.HOUR_OF_DAY);
return curTime;
}
private int getMinute() {
Calendar c = Calendar.getInstance();
int curMinute = c.get(Calendar.MINUTE);
return curMinute;
}
private String getWeek(String pTime) {
String Week = "周";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
e.printStackTrace();
}
switch (c.get(Calendar.DAY_OF_WEEK)) {
case 1:
Week += "日";
break;
case 2:
Week += "一";
break;
case 3:
Week += "二";
break;
case 4:
Week += "三";
break;
case 5:
Week += "四";
break;
case 6:
Week += "五";
break;
case 7:
Week += "六";
break;
default:
break;
}
return Week;
}
/**
* @param year
* @param month
* @return
*/
private int getDay(int year, int month) {
int day = 30;
boolean flag = false;
switch (year % 4) {
case 0:
flag = true;
break;
default:
flag = false;
break;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
day = flag ? 29 : 28;
break;
default:
day = 30;
break;
}
return day;
}
/**
*/
private void initDay(int arg1, int arg2) {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this, 1, getDay(arg1, arg2), "%02d");
numericWheelAdapter.setLabel("日");
day.setViewAdapter(numericWheelAdapter);
}
@Override
protected void onDestroy() {
getCodeBt.onDestroy();
ActivityManager.getActivityManager().closeActivity();
super.onDestroy();
}
}
下面是:
yrd_reserve_view
<?xml version="1.0" encoding="utf-8"?><!--<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"--><!--android:layout_width="match_parent"--><!--android:layout_height="match_parent"--><!--android:focusable="true"--><!--android:focusableInTouchMode="true"--><!--android:orientation="vertical">-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:id="@+id/title_content_layout"
android:layout_width="match_parent"
android:layout_height="47.5dp"
android:background="@color/yrd_title_bg">
<ImageView
android:id="@+id/titlebar_img_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:src="@drawable/back" />
<TextView
android:id="@+id/titlebar_text_title"
style="@style/titlebar_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:gravity="center"
android:singleLine="true"
android:text="@string/reserve_detail"
android:textColor="#696969"
android:visibility="visible" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/title_content_layout"
android:background="@color/bg_Gray" />
<LinearLayout
android:id="@+id/phone_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reserve_phone"
android:textSize="16sp" />
<com.sxyrd.yrdapp.widget.CleanableEditText
android:id="@+id/phone_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawablePadding="15dp"
android:drawableRight="@drawable/clear"
android:hint="@string/hint_reserve_phone"
android:maxEms="11"
android:padding="15dp"
android:phoneNumber="true"
android:singleLine="true"
android:textSize="14sp" />
<com.sxyrd.yrdapp.widget.TimeButton
android:id="@+id/get_code_bt"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/code1"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="获取验证码"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/product_devier" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reserve_auth_code"
android:textSize="16sp" />
<EditText
android:id="@+id/sms_code_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/sms_code"
android:inputType="number"
android:padding="15dp"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/product_devier" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reserve_number_people"
android:textSize="16sp" />
<EditText
android:id="@+id/reserve_number_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/hint_reserve_people"
android:inputType="number"
android:padding="15dp"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/product_devier" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reserve_time"
android:textSize="16sp" />
<TextView
android:id="@+id/reserve_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/hint_reserve_time"
android:padding="15dp"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/product_devier" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/yrd_title_bg">
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:visibility="visible"></LinearLayout>
<TextView
android:id="@+id/reserve_promptly_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/yuding1"
android:gravity="center"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:text="@string/reserve_promptly"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout><!--</ScrollView>-->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.sxyrd.yrdapp.widget.wheelview.WheelView
android:id="@+id/month"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<com.sxyrd.yrdapp.widget.wheelview.WheelView
android:id="@+id/day"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<TextView
android:id="@+id/week"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="周六"
android:textColor="@color/big_red"
android:textSize="20sp" />
<com.sxyrd.yrdapp.widget.wheelview.WheelView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:visibility="gone" />
<com.sxyrd.yrdapp.widget.wheelview.WheelView
android:id="@+id/hour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
<com.sxyrd.yrdapp.widget.wheelview.WheelView
android:id="@+id/min"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/whell_bg"
android:orientation="vertical">
</LinearLayout>
<!--<View-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="0.5dp"-->
<!--android:layout_marginBottom="1dp"-->
<!--android:background="#999999" />-->
</FrameLayout>
上面的是我做的预订功能代码,
下面whell代码太多大家可以直接下载文件看看,有什么需要修改的地方,共同研究
下载地址:http://download.youkuaiyun.com/detail/mingzhnglei/9483491