1、今天的任务
依旧是UI方面的制作,直接上代码:
SelectBarberActivity.java
1 package com.example.lileme.ThreeFragment.AppointmentButton; 2 3 import android.app.AlertDialog; 4 import android.content.DialogInterface; 5 import android.graphics.Rect; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.support.annotation.NonNull; 10 import android.support.v7.app.AppCompatActivity; 11 import android.support.v7.widget.LinearLayoutManager; 12 import android.support.v7.widget.RecyclerView; 13 import android.view.Gravity; 14 import android.view.View; 15 import android.widget.Button; 16 import android.widget.ImageView; 17 import android.widget.RadioButton; 18 import android.widget.RadioGroup; 19 import android.widget.TextView; 20 import android.widget.Toast; 21 22 import com.example.lileme.R; 23 import com.example.lileme.Service.GetAllTypeNumService; 24 import com.example.lileme.Service.SetPeopleNumberService; 25 import com.example.lileme.Util.Toastutil; 26 27 import java.text.SimpleDateFormat; 28 import java.util.ArrayList; 29 import java.util.Calendar; 30 import java.util.Date; 31 32 public class SelectBarberActivity extends AppCompatActivity { 33 34 // @Override 35 // protected void onCreate(Bundle savedInstanceState) { 36 // super.onCreate(savedInstanceState); 37 // setContentView(R.layout.activity_select_barber); 38 // } 39 private ImageView mIvBack; 40 private RecyclerView mRvDay; 41 private RecyclerView mRvHour; 42 private RadioGroup mRgHairType; 43 private RadioButton mRbHairtype; 44 private Button mBtnAppointment; 45 private TextView mTvAppointmentInfo; //获取当前时间段: 46 private TextView mTvLifaNum; //获取理发预约人数 47 private TextView mTvTangfaNum; //获取理发预约人数 48 private TextView mTvRanfaNum; //获取理发预约人数 49 private ArrayList<RecyclerData> datas = new ArrayList<>(); 50 private ArrayList<RecyclerData> hours = new ArrayList<>(); 51 private String selectDay = getWeek(0)+getmonth(0);//选中的星期 52 private String selectHour = "07:00~10:00";//选中的时间段 53 private String appointmentdate = selectDay+" "+selectHour; //星期加时间段 54 private String user = ""; //接收HairAdapter页面的user 55 private String barber_user = ""; //接收HairAdapter页面的barber_user 56 private String barber_name = ""; //接收HairAdapter页面的barber_name 57 private Boolean flag_day = true; //判断日期的点击状态 58 private Boolean flag_hour = true; //判断时间段的点击状态 59 private ArrayList<String> arrayList = new ArrayList<>(); //存放各种理发类型人数 60 private Thread thread = null; 61 private final int what1 = 1; 62 private final int what2 = 2; 63 private final int what3 = 3; 64 private final int what4 = 4; 65 @Override 66 protected void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 setContentView(R.layout.activity_select_barber); 69 //获取HairAdapter页面的user与barber_user 70 getUser(); 71 //设置星期 72 datas = initData(); 73 //设置时间段 74 hours = initHour(); 75 //初始化 76 thread = new Thread(new GetAllTypeNumThread()); 77 thread.start(); 78 try { 79 thread.join(); 80 } catch (InterruptedException e) { 81 e.printStackTrace(); 82 } 83 84 mRvDay = findViewById(R.id.rv_day); //获取星期列表布局 85 mRvHour = findViewById(R.id.rv_hour); //获取时辰列表布局 86 mRgHairType = findViewById(R.id.rg_type); //获取理发类型单选框 87 //获取一开始被选中的理发类型 88 mRbHairtype = findViewById(mRgHairType.getCheckedRadioButtonId()); 89 mTvAppointmentInfo = findViewById(R.id.tv_appointment_information); //获取当前时间段: 90 mTvLifaNum = findViewById(R.id.tv_lifa_number); //获取理发人数 91 mTvTangfaNum = findViewById(R.id.tv_tangfa_number); //获取烫发人数 92 mTvRanfaNum = findViewById(R.id.tv_ranfa_number); //获取染发人数 93 mBtnAppointment = findViewById(R.id.btn_appointment); //预约按钮 94 //布局日期 95 SetDayLayout(); 96 //布局时间段 97 SetHourLayout(); 98 //设置理发类型监听事件 99 mRgHairType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 100 @Override 101 public void onCheckedChanged(RadioGroup group, int checkedId) { 102 mRbHairtype = findViewById(checkedId); 103 } 104 }); 105 //返回键 106 mIvBack = findViewById(R.id.iv_back); 107 mIvBack.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View v) { 110 finish(); 111 } 112 }); 113 //点击预约按钮 114 mBtnAppointment.setOnClickListener(new View.OnClickListener() { 115 @Override 116 public void onClick(View v) { 117 if(user == null){ 118 Toast toast = Toast.makeText(SelectBarberActivity.this,"请先登录",Toast.LENGTH_SHORT); 119 toast.setGravity(Gravity.CENTER,0,150); 120 toast.show(); 121 }else{ 122 if(!flag_day||!flag_hour){ 123 Toastutil.showmsg(SelectBarberActivity.this,"请选择时间段"); 124 } 125 else { 126 AlertDialog.Builder builder = new AlertDialog.Builder(SelectBarberActivity.this); 127 builder.setMessage("确定预约?").setPositiveButton("确定", new DialogInterface.OnClickListener() { 128 @Override 129 public void onClick(DialogInterface dialog, int which) { 130 new Thread(new Runnable() { 131 @Override 132 public void run() { 133 String flag = SetPeopleNumberService.executeHttpPost(barber_user,barber_name,user,mRbHairtype.getText().toString(),gettime(),appointmentdate,"SetPeopleNumber"); 134 //根据时间判断是否能预约 135 if(flag.equals("false")){ 136 Message message = new Message(); 137 message.what = what3; 138 mhandler.sendMessage(message); 139 }else if(flag.equals("contain")){ 140 Message message = new Message(); 141 message.what = what4; 142 mhandler.sendMessage(message); 143 } else{ 144 //弹框 145 Message message = new Message(); 146 message.what = what2; 147 mhandler.sendMessage(message); 148 } 149 } 150 }).start(); 151 // Toastutil.showmsg(SelectBarberActivity.this,selectDay+selectHour+mRbHairtype.getText().toString()+user+barber_user); 152 } 153 }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 154 @Override 155 public void onClick(DialogInterface dialog, int which) { 156 Toastutil.showmsg(SelectBarberActivity.this,"预约失败"); 157 } 158 }).show(); 159 } 160 } 161 } 162 }); 163 } 164 //布局星期 165 public void SetDayLayout(){ 166 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SelectBarberActivity.this); 167 linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 168 mRvDay.setLayoutManager(linearLayoutManager); 169 final SelectBarberDayAdapter selectBarberDayAdapter = new SelectBarberDayAdapter(SelectBarberActivity.this,datas); 170 mRvDay.setAdapter(selectBarberDayAdapter); 171 mRvDay.addItemDecoration(new MyDecoration());//设置分割线 172 ////设置点击事件返回点击内容到selectDay 173 selectBarberDayAdapter.setOnItemListener(new SelectBarberDayAdapter.OnItemListener() { 174 @Override 175 public void onClick(SelectBarberDayAdapter.MyViewHolder holder, int position) { 176 selectBarberDayAdapter.setDefSelect(position); 177 if(datas.get(position).isSelect) { 178 // 选中状态点击之后改为未选中 179 datas.get(position).isSelect=false; 180 selectBarberDayAdapter.notifyDataSetChanged(); 181 selectDay = "请选择日期"; 182 flag_day = false; //设置为未选中状态 183 }else { 184 // 未选中状态点击之后给为选中 185 datas.get(position).isSelect=true; 186 selectDay = datas.get(position).content; 187 appointmentdate = selectDay+" "+selectHour; 188 selectBarberDayAdapter.notifyDataSetChanged(); 189 flag_day = true; 190 if(flag_hour){ //时间段被选中 191 thread = new Thread(new GetAllTypeNumThread()); 192 thread.start(); 193 try { 194 thread.join(); 195 } catch (InterruptedException e) { 196 e.printStackTrace(); 197 } 198 } 199 } 200 } 201 }); 202 } 203 //布局时间段 204 public void SetHourLayout(){ 205 LinearLayoutManager linearLayoutManagerHour = new LinearLayoutManager(SelectBarberActivity.this); 206 linearLayoutManagerHour.setOrientation(LinearLayoutManager.HORIZONTAL); 207 mRvHour.setLayoutManager(linearLayoutManagerHour); 208 final SelectBarberHourAdapter selectBarberHourAdapter = new SelectBarberHourAdapter(SelectBarberActivity.this,hours); 209 mRvHour.setAdapter(selectBarberHourAdapter); 210 mRvHour.addItemDecoration(new MyDecoration()); 211 //设置点击事件返回点击内容到selectHour 212 selectBarberHourAdapter.setOnItemListener(new SelectBarberHourAdapter.OnItemListener() { 213 @Override 214 public void onClick(SelectBarberHourAdapter.MyViewHolder holder, int position) { 215 selectBarberHourAdapter.setDefSelect(position); 216 if(hours.get(position).isSelect) { 217 // 选中状态点击之后改为未选中 218 hours.get(position).isSelect=false; 219 selectBarberHourAdapter.notifyDataSetChanged(); 220 selectHour = "请选择时间段"; 221 flag_hour = false; //设置为未选中状态 222 }else { 223 // 未选中状态点击之后给为选中 224 hours.get(position).isSelect=true; 225 selectHour = hours.get(position).content; 226 appointmentdate = selectDay+" "+selectHour; 227 selectBarberHourAdapter.notifyDataSetChanged(); 228 flag_hour = true; 229 if(flag_day){ //日期被选中 230 thread = new Thread(new GetAllTypeNumThread()); 231 thread.start(); 232 try { 233 thread.join(); 234 } catch (InterruptedException e) { 235 e.printStackTrace(); 236 } 237 } 238 } 239 } 240 }); 241 } 242 //创建获取所有理发类型人数的子线程 243 public class GetAllTypeNumThread implements Runnable{ 244 @Override 245 public void run() { 246 arrayList = GetAllTypeNumService.executeHttpPost(barber_user,appointmentdate,"GetAllTypeNum"); 247 Message message = new Message(); 248 message.what = what1; 249 mhandler.sendMessage(message); 250 } 251 } 252 253 // 254 public Handler mhandler = new Handler(){ 255 @Override 256 public void handleMessage(Message msg) { 257 switch (msg.what){ 258 case what1: 259 mTvLifaNum.setText("理发预约人数:"+arrayList.get(0)+"人"); 260 mTvTangfaNum.setText("烫发预约人数:"+arrayList.get(1)+"人"); 261 mTvRanfaNum.setText("染发预约人数:"+arrayList.get(2)+"人"); 262 break; 263 case what2: 264 Toastutil.showmsg(SelectBarberActivity.this,"预约成功"); 265 break; 266 case what3: 267 Toastutil.showmsg(SelectBarberActivity.this,"因该时间段预约人数爆满,请小可爱预约别的时间段哦!"); 268 break; 269 case what4: 270 Toastutil.showmsg(SelectBarberActivity.this,"您已预约此店铺"); 271 break; 272 } 273 } 274 }; 275 276 //绘制分割线(颜色是背景颜色) 277 class MyDecoration extends RecyclerView.ItemDecoration{ 278 @Override 279 public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { 280 super.getItemOffsets(outRect, view, parent, state); 281 outRect.set(0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight3),0); 282 } 283 } 284 //获取当前时间 285 public String gettime(){ 286 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");// HH:mm:ss 287 //获取当前时间 288 Date date = new Date(System.currentTimeMillis()); 289 String time=simpleDateFormat.format(date); 290 return time; 291 } 292 293 294 /** 295 * 设置星期 296 */ 297 protected ArrayList<RecyclerData> initData() { 298 ArrayList<RecyclerData> mDatas = new ArrayList<>(); 299 RecyclerData recyclerData1=new RecyclerData(); 300 recyclerData1.content=getWeek(0)+getmonth(0); 301 mDatas.add(recyclerData1); 302 RecyclerData recyclerData2=new RecyclerData(); 303 recyclerData2.content=getWeek(1)+getmonth(1); 304 mDatas.add(recyclerData2); 305 RecyclerData recyclerData3=new RecyclerData(); 306 recyclerData3.content=getWeek(2)+getmonth(2); 307 mDatas.add(recyclerData3); 308 RecyclerData recyclerData4=new RecyclerData(); 309 recyclerData4.content=getWeek(3)+getmonth(3); 310 mDatas.add(recyclerData4); 311 RecyclerData recyclerData5=new RecyclerData(); 312 recyclerData5.content=getWeek(4)+getmonth(4); 313 mDatas.add(recyclerData5); 314 RecyclerData recyclerData6=new RecyclerData(); 315 recyclerData6.content=getWeek(5)+getmonth(5); 316 mDatas.add(recyclerData6); 317 RecyclerData recyclerData7=new RecyclerData(); 318 recyclerData7.content=getWeek(6)+getmonth(6); 319 mDatas.add(recyclerData7); 320 return mDatas; 321 } 322 /** 323 * 设置时间段 324 */ 325 protected ArrayList<RecyclerData> initHour() { 326 ArrayList<RecyclerData> mDatas = new ArrayList<>(); 327 RecyclerData recyclerData1=new RecyclerData(); 328 recyclerData1.content="07:00~10:00"; 329 mDatas.add(recyclerData1); 330 RecyclerData recyclerData2=new RecyclerData(); 331 recyclerData2.content="10:00~13:00"; 332 mDatas.add(recyclerData2); 333 RecyclerData recyclerData3=new RecyclerData(); 334 recyclerData3.content="13:00~16:00"; 335 mDatas.add(recyclerData3); 336 RecyclerData recyclerData4=new RecyclerData(); 337 recyclerData4.content="16:00~19:00"; 338 mDatas.add(recyclerData4); 339 return mDatas; 340 } 341 public String getmonth(int i){ 342 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");// HH:mm:ss 343 Calendar c = Calendar.getInstance(); 344 //加一天后的时间 345 c.add(Calendar.DAY_OF_MONTH, i); 346 String date = simpleDateFormat.format(c.getTime()); 347 return date; 348 } 349 350 /*获取星期几*/ 351 public static String getWeek(int j) { 352 Calendar cal = Calendar.getInstance(); 353 int i = cal.get(Calendar.DAY_OF_WEEK); 354 //之后第j天星期几 355 int k = (i+j)%7; 356 switch (k) { 357 case 1: 358 return "周日"; 359 case 2: 360 return "周一"; 361 case 3: 362 return "周二"; 363 case 4: 364 return "周三"; 365 case 5: 366 return "周四"; 367 case 6: 368 return "周五"; 369 case 0: 370 return "周六"; 371 default: 372 return ""; 373 } 374 } 375 //接收从HairAdapter中传递过来的user与barber_user 376 public void getUser(){ 377 Bundle bundle = getIntent().getExtras(); 378 if(bundle == null){ 379 user = null; 380 }else{ 381 user = bundle.getString("user"); 382 barber_user = bundle.getString("barber_user"); 383 barber_name = bundle.getString("barber_name"); 384 } 385 } 386 387 }
activity_select_barber.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 <LinearLayout 7 android:id="@+id/ll_1" 8 android:layout_width="match_parent" 9 android:layout_height="60dp" 10 android:orientation="horizontal" 11 android:background="#E6E6E6"> 12 <ImageView 13 android:id="@+id/iv_back" 14 android:layout_width="25dp" 15 android:layout_height="30dp" 16 android:background="@drawable/back" 17 android:scaleY="0.8" 18 android:layout_gravity="center_vertical" 19 android:layout_marginTop="5dp" 20 android:layout_marginBottom="5dp" 21 android:layout_marginLeft="5dp"/> 22 <LinearLayout 23 android:layout_width="match_parent" 24 android:layout_height="match_parent" 25 android:orientation="vertical"> 26 <TextView 27 android:id="@+id/tv_title" 28 android:layout_width="match_parent" 29 android:layout_height="match_parent" 30 android:text="预约详情" 31 android:textSize="20sp" 32 android:gravity="center" 33 android:layout_gravity="center_horizontal" 34 android:layout_marginTop="5dp" 35 android:layout_marginBottom="5dp" 36 android:layout_marginRight="15dp"/> 37 </LinearLayout> 38 </LinearLayout> 39 <RelativeLayout 40 android:id="@+id/tv_bottom" 41 android:layout_width="match_parent" 42 android:layout_height="match_parent" 43 android:orientation="horizontal" 44 android:layout_below="@id/ll_1"> 45 <TextView 46 android:id="@+id/divider_top" 47 android:layout_width="match_parent" 48 android:layout_height="0.5dp" 49 android:background="#2BB3D5" 50 /> 51 <android.support.v7.widget.RecyclerView 52 android:id="@+id/rv_day" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:scrollbars="vertical" 56 android:background="@color/unselect" 57 android:layout_below="@id/divider_top"> 58 </android.support.v7.widget.RecyclerView> 59 <TextView 60 android:id="@+id/divider" 61 android:layout_width="match_parent" 62 android:layout_height="0.5dp" 63 android:background="#2BB3D5" 64 android:layout_below="@+id/rv_day"/> 65 <android.support.v7.widget.RecyclerView 66 android:id="@+id/rv_hour" 67 android:layout_width="match_parent" 68 android:layout_height="wrap_content" 69 android:scrollbars="vertical" 70 android:background="@color/unselect" 71 android:layout_below="@+id/divider"> 72 </android.support.v7.widget.RecyclerView> 73 <TextView 74 android:id="@+id/divider_bottom" 75 android:layout_width="match_parent" 76 android:layout_height="0.5dp" 77 android:background="#2BB3D5" 78 android:layout_below="@+id/rv_hour"/> 79 <TextView 80 android:id="@+id/tv_type" 81 android:layout_below="@+id/divider_bottom" 82 android:layout_width="match_parent" 83 android:layout_height="wrap_content" 84 android:text="服务类型:" 85 android:textSize="25sp" 86 android:layout_marginLeft="10dp" 87 android:layout_marginRight="10dp" 88 android:layout_marginTop="10dp" 89 android:layout_marginBottom="10dp" 90 android:gravity="center_horizontal"/> 91 92 <LinearLayout 93 android:id="@+id/ll_2" 94 android:layout_below="@+id/tv_type" 95 android:layout_width="match_parent" 96 android:layout_height="wrap_content" 97 android:layout_marginTop="10dp" 98 android:layout_marginBottom="10dp" 99 android:gravity="center" 100 android:orientation="vertical"> 101 <RadioGroup 102 android:id="@+id/rg_type" 103 android:layout_width="wrap_content" 104 android:layout_height="wrap_content" 105 android:orientation="horizontal"> 106 <RadioButton 107 android:id="@+id/rb_lifa" 108 android:layout_width="80dp" 109 android:layout_height="40dp" 110 android:button="@null" 111 android:text="理发" 112 android:checked="true" 113 android:textColor="#000" 114 android:textSize="20sp" 115 android:gravity="center" 116 android:background="@drawable/select_boy_girl"/> 117 <RadioButton 118 android:id="@+id/rb_tangfa" 119 android:layout_width="80dp" 120 android:layout_height="40dp" 121 android:button="@null" 122 android:text="染发" 123 android:textColor="#000" 124 android:textSize="20sp" 125 android:gravity="center" 126 android:layout_marginLeft="20dp" 127 android:background="@drawable/select_boy_girl"/> 128 <RadioButton 129 android:id="@+id/rb_ranfa" 130 android:layout_width="80dp" 131 android:layout_height="40dp" 132 android:button="@null" 133 android:text="烫发" 134 android:textColor="#000" 135 android:textSize="20sp" 136 android:gravity="center" 137 android:layout_marginLeft="20dp" 138 android:background="@drawable/select_boy_girl"/> 139 </RadioGroup> 140 </LinearLayout> 141 142 143 <RelativeLayout 144 android:layout_width="match_parent" 145 android:layout_height="wrap_content" 146 android:gravity="center" 147 android:layout_below="@id/ll_2"> 148 <TextView 149 android:id="@+id/tv_appointment_information" 150 android:layout_width="match_parent" 151 android:layout_height="wrap_content" 152 android:text="当前时间段:" 153 android:textSize="20sp" 154 android:gravity="center"/> 155 <TextView 156 android:id="@+id/tv_lifa_number" 157 android:layout_width="match_parent" 158 android:layout_height="wrap_content" 159 android:text="理发预约人数:" 160 android:textSize="20sp" 161 android:gravity="center" 162 android:layout_below="@id/tv_appointment_information"/> 163 <TextView 164 android:id="@+id/tv_ranfa_number" 165 android:layout_width="match_parent" 166 android:layout_height="wrap_content" 167 android:text="染发预约人数:" 168 android:textSize="20sp" 169 android:gravity="center" 170 android:layout_below="@+id/tv_lifa_number"/> 171 <TextView 172 android:id="@+id/tv_tangfa_number" 173 android:layout_width="match_parent" 174 android:layout_height="wrap_content" 175 android:text="烫发预约人数:" 176 android:textSize="20sp" 177 android:gravity="center" 178 android:layout_below="@id/tv_ranfa_number"/> 179 <!--<TextView 180 android:id="@+id/tv_all_time" 181 android:layout_width="match_parent" 182 android:layout_height="wrap_content" 183 android:text="预计花费总时间:" 184 android:textSize="20sp" 185 android:gravity="center" 186 android:layout_below="@+id/tv_tangfa_number" />--> 187 <Button 188 android:id="@+id/btn_appointment" 189 android:layout_width="match_parent" 190 android:layout_height="40dp" 191 android:layout_marginRight="15dp" 192 android:layout_marginLeft="15dp" 193 android:layout_marginTop="10dp" 194 android:text="预约" 195 android:textSize="15sp" 196 android:background="@drawable/btn_appointment" 197 android:layout_below="@+id/tv_tangfa_number" /> 198 </RelativeLayout> 199 </RelativeLayout> 200 201 </RelativeLayout>