您好,登錄后才能下訂單哦!
在Android項目中添加一個商品預訂功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
MainActivity.java代碼:
package siso.calendarselector; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import siso.calendarselector.lib.CalendarSelectorActivity; public class MainActivity extends Activity { private EditText orderEt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void jump(View v) { EditText et = (EditText) findViewById(R.id.et_input); orderEt = (EditText) findViewById(R.id.et_order); int days = Integer.valueOf(et.getText().toString()); String order = orderEt.getText().toString(); Intent i = new Intent(MainActivity.this, CalendarSelectorActivity.class); i.putExtra(CalendarSelectorActivity.DAYS_OF_SELECT, days); i.putExtra(CalendarSelectorActivity.ORDER_DAY, order); startActivityForResult(i, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK) { String orderInfo = data.getStringExtra(CalendarSelectorActivity.ORDER_DAY); orderEt.setText(orderInfo); /* *****注意***** // 如需轉換為Calendar // 正確轉換方法(因為2月沒有30天): String[] info = orderInfo.split("#"); Calendar c = Calendar.getInstance(); c.set(Integer.valueOf(info[0]), Integer.valueOf(info[1]) - 1, Integer.valueOf(info[2])); // 錯誤轉換方法: c.set(Integer.valueOf(info[0]), Integer.valueOf(info[1]), Integer.valueOf(info[2])); c.add(Calendar.MONTH, -1); **/ } } }
CalendarAdapter.java代碼:
package siso.calendarselector.lib; import java.util.ArrayList; import java.util.Calendar; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import siso.calendarselector.R; /** * @Description 日歷表格適配器 */ public class CalendarAdapter extends BaseAdapter { private ArrayList<Day> days; private LayoutInflater mInflater; private Calendar c; private Context context; private String orderDay; public CalendarAdapter(Context context, Calendar c, int passDays, String orderDay) { this.c = c; this.context = context; this.orderDay = orderDay; days = CalendarUtils.getDaysOfMonth(this.c, passDays, orderDay); mInflater = LayoutInflater.from(context); } @Override public int getCount() { return days.size(); } @Override public Day getItem(int arg0) { return days.get(arg0); } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View v = arg1; ViewHolder holder = null; if (v == null) { v = mInflater.inflate(R.layout.calendar_item, arg2, false); holder = new ViewHolder(); holder.tv = (TextView) v.findViewById(R.id.tv_calendar_item); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } Day d = getItem(arg0); switch (d.getType()) { case TODAY: setOrderThreeDayStyle(holder.tv, d.isOrdered(), context.getString(R.string.today)); break; case TOMORROW: setOrderThreeDayStyle(holder.tv, d.isOrdered(), context.getString(R.string.tomorrow)); break; case T_D_A_T: setOrderThreeDayStyle(holder.tv, d.isOrdered(), context.getString(R.string.t_d_a_t)); break; case ENABLE: holder.tv.setText(d.isOrdered() ? d.getName() + context.getString(R.string.order_day) : d.getName()); holder.tv.setEnabled(true); holder.tv.setTextColor(d.isOrdered() ? Color.WHITE : context.getResources().getColor( R.color.calendar_enable_color)); holder.tv.setBackgroundResource(d.isOrdered() ? R.drawable.calendar_order_item_bg : R.drawable.normal_calendar_order_item_bg); holder.tv.setTextSize(d.isOrdered() ? context.getResources().getDimension( R.dimen.calendar_item_order_day_size) : context.getResources().getDimension( R.dimen.calendar_item_nonorder_day_size)); break; case NOT_ENABLE: holder.tv.setText(d.getName()); holder.tv.setEnabled(false); holder.tv.setTextColor(context.getResources().getColor(R.color.calendar_disable_color)); holder.tv.setBackgroundColor(Color.WHITE); holder.tv.setTextSize(context.getResources().getDimension(R.dimen.calendar_item_nonorder_day_size)); break; } return v; } private void setOrderThreeDayStyle(TextView tv, boolean ordered, String dayStr) { tv.setText(ordered ? dayStr + context.getString(R.string.order_day) : dayStr); tv.setEnabled(true); tv.setTextColor(ordered ? Color.WHITE : context.getResources().getColor(R.color.calendar_threeday_color)); tv.setBackgroundResource(ordered ? R.drawable.calendar_order_item_bg : R.drawable.normal_calendar_order_item_bg); tv.setTextSize(ordered ? context.getResources().getDimension(R.dimen.calendar_item_order_day_size) : context .getResources().getDimension(R.dimen.calendar_item_nonorder_day_size)); } static class ViewHolder { TextView tv; } public void previous() { if (c.get(Calendar.MONTH) == c.getActualMinimum(Calendar.MONTH)) { c.set((c.get(Calendar.YEAR) - 1), c.getActualMaximum(Calendar.MONTH), 1); } else { c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1); } days = CalendarUtils.getDaysOfMonth(c, 0, orderDay); notifyDataSetChanged(); } public void next() { if (c.get(Calendar.MONTH) == c.getActualMaximum(Calendar.MONTH)) { c.set((c.get(Calendar.YEAR) + 1), c.getActualMinimum(Calendar.MONTH), 1); } else { c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1); } days = CalendarUtils.getDaysOfMonth(c, 0, orderDay); notifyDataSetChanged(); } }
CalendarListAdapter.java代碼:
package siso.calendarselector.lib; import java.util.Calendar; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.TextView; import siso.calendarselector.R; /** * @Description 日歷列表適配器 */ public class CalendarListAdapter extends BaseAdapter { private Context context; private LayoutInflater mInflater; private int daysOfSelect; private String orderDay; private OnCalendarOrderListener listener; public CalendarListAdapter(Context context, int daysOfSelect, String orderDay) { this.context = context; this.daysOfSelect = daysOfSelect; this.orderDay = orderDay; mInflater = LayoutInflater.from(context); } @Override public int getCount() { return CalendarUtils.throughMonth(Calendar.getInstance(), daysOfSelect) + 1; } @Override public Object getItem(int arg0) { return null; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View v = arg1; ViewHolder holder = null; if (v == null) { v = mInflater.inflate(R.layout.calendar, arg2, false); holder = new ViewHolder(); holder.yearAndMonth = (TextView) v.findViewById(R.id.tv_year_month); holder.calendarGrid = (NoScrollGridView) v.findViewById(R.id.gv_calendar_layout); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } final Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, arg0); holder.yearAndMonth.setText(c.get(Calendar.YEAR) + context.getString(R.string.year) + (c.get(Calendar.MONTH) + 1) + context.getString(R.string.month)); CalendarAdapter cAdapter = null; if (arg0 == 0) { cAdapter = new CalendarAdapter(context, c, daysOfSelect, orderDay); } else { int d = daysOfSelect - CalendarUtils.currentMonthRemainDays() - CalendarUtils.getFlowMonthDays(arg0 - 1); cAdapter = new CalendarAdapter(context, c, d, orderDay); } holder.calendarGrid.setAdapter(cAdapter); holder.calendarGrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Calendar cl = (Calendar) c.clone(); cl.set(Calendar.DAY_OF_MONTH, 1); int day = position + 2 - cl.get(Calendar.DAY_OF_WEEK); TextView dayTv = (TextView) view.findViewById(R.id.tv_calendar_item); if (day <= 0 || !dayTv.isEnabled()) return; String orderInfo = c.get(Calendar.YEAR) + "#" + (c.get(Calendar.MONTH) + 1) + "#" + day; cl.clear(); cl = null; if (listener != null) listener.onOrder(orderInfo); } }); return v; } static class ViewHolder { TextView yearAndMonth; NoScrollGridView calendarGrid; } public void setOnCalendarOrderListener(OnCalendarOrderListener listener) { this.listener = listener; } public interface OnCalendarOrderListener { void onOrder(String orderInfo); } }
CalendarSelectorActivity.java代碼:
package siso.calendarselector.lib; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ListView; import siso.calendarselector.R; /** * @Description 預訂日選擇 */ public class CalendarSelectorActivity extends Activity { /** * 可選天數 */ public static final String DAYS_OF_SELECT = "days_of_select"; /** * 上次預訂日 */ public static final String ORDER_DAY = "order_day"; private int daysOfSelect; private String orderDay; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calendar_selector); daysOfSelect = getIntent().getIntExtra(DAYS_OF_SELECT, 30); orderDay = getIntent().getStringExtra(ORDER_DAY); listView = (ListView) findViewById(R.id.lv_calendar); CalendarListAdapter adapter = new CalendarListAdapter(this, daysOfSelect, orderDay); listView.setAdapter(adapter); adapter.setOnCalendarOrderListener(new CalendarListAdapter.OnCalendarOrderListener() { @Override public void onOrder(String orderInfo) { Intent result = new Intent(); result.putExtra(ORDER_DAY, orderInfo); setResult(RESULT_OK, result); finish(); } }); } }
CalendarUtils.java代碼:
package siso.calendarselector.lib; import java.util.ArrayList; import java.util.Calendar; public class CalendarUtils { private static int dayOfMonth, monthOfYear, curYear; static { dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); monthOfYear = Calendar.getInstance().get(Calendar.MONTH); curYear = Calendar.getInstance().get(Calendar.YEAR); } /** * Gets the days in month. * * @param month the month * @param year the year * @return the days in month */ public static int getDaysInMonth(int month, int year) { switch (month) { case Calendar.JANUARY: case Calendar.MARCH: case Calendar.MAY: case Calendar.JULY: case Calendar.AUGUST: case Calendar.OCTOBER: case Calendar.DECEMBER: return 31; case Calendar.APRIL: case Calendar.JUNE: case Calendar.SEPTEMBER: case Calendar.NOVEMBER: return 30; case Calendar.FEBRUARY: return (year % 4 == 0) ? 29 : 28; default: throw new IllegalArgumentException("Invalid Month"); } } /** * Gets the flow month days. * * @param flowMonth the flow month * @return the flow month days */ public static int getFlowMonthDays(int flowMonth) { int totalDays = 0; for (int i = 0; i < flowMonth; i++) { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, i + 1); int days = getDaysInMonth(c.get(Calendar.MONTH), c.get(Calendar.YEAR)); totalDays += days; } return totalDays; } /** * Current month remain days. * * @return the int */ public static int currentMonthRemainDays() { Calendar c = Calendar.getInstance(); return getDaysInMonth(c.get(Calendar.MONTH), c.get(Calendar.YEAR)) - c.get(Calendar.DAY_OF_MONTH) + 1; } /** * Through month. * * @param calendar the calendar * @param passDays the pass days * @return the int */ public static int throughMonth(Calendar calendar, int passDays) { Calendar c = (Calendar) calendar.clone(); int curMonth = c.get(Calendar.MONTH); int curYear = c.get(Calendar.YEAR); c.add(Calendar.DAY_OF_MONTH, passDays - 1); int monthCount = (c.get(Calendar.YEAR) - curYear) * 12 + (c.get(Calendar.MONTH) - curMonth); return monthCount; } /** * Gets the days of month. * * @param calendar the calendar * @return the days of month */ public static String[] getDaysOfMonth(Calendar calendar) { Calendar month = (Calendar) calendar.clone(); String[] days; final int FIRST_DAY_OF_WEEK = 0; // Sunday = 0, Monday = 1 month.set(Calendar.DAY_OF_MONTH, 1); int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH); int firstDay = (int) month.get(Calendar.DAY_OF_WEEK); if (firstDay == 1) { days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)]; } else { days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)]; } int j = FIRST_DAY_OF_WEEK; if (firstDay > 1) { for (j = 0; j < firstDay - FIRST_DAY_OF_WEEK; j++) { days[j] = ""; } } else { for (j = 0; j < FIRST_DAY_OF_WEEK * 6; j++) { days[j] = ""; } j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7 } int dayNumber = 1; for (int i = j - 1; i < days.length; i++) { days[i] = "" + dayNumber; dayNumber++; } return days; } /** * Gets the days of month. * * @param calendar the calendar * @param passDays the pass days * @param orderDay the order day * @return the days of month */ public static ArrayList<Day> getDaysOfMonth(Calendar calendar, int passDays, String orderDay) { String[] orderInfo = null; boolean isOrdered = false; if (orderDay != null) orderInfo = orderDay.split("#"); Calendar month = (Calendar) calendar.clone(); final int FIRST_DAY_OF_WEEK = 0; // Sunday = 0, Monday = 1 month.set(Calendar.DAY_OF_MONTH, 1); int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH); int firstDay = (int) month.get(Calendar.DAY_OF_WEEK); ArrayList<Day> days = new ArrayList<Day>(); int size; if (firstDay == 1) { size = lastDay + (FIRST_DAY_OF_WEEK * 6); } else { size = lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1); } for (int i = 0; i < size; i++) { days.add(new Day("", Day.DayType.NOT_ENABLE, isOrdered)); } int j = FIRST_DAY_OF_WEEK; if (firstDay > 1) { for (j = 0; j < firstDay - FIRST_DAY_OF_WEEK; j++) { days.set(j, new Day("", Day.DayType.NOT_ENABLE, isOrdered)); } } else { for (j = 0; j < FIRST_DAY_OF_WEEK * 6; j++) { days.set(j, new Day("", Day.DayType.NOT_ENABLE, isOrdered)); } j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7 } int dayNumber = 1; for (int i = j - 1; i < days.size(); i++) { Day.DayType type; if (month.get(Calendar.YEAR) == curYear && month.get(Calendar.MONTH) == monthOfYear) { if (dayNumber >= dayOfMonth && dayNumber < dayOfMonth + passDays) { type = Day.DayType.ENABLE; if (dayNumber == dayOfMonth) { type = Day.DayType.TODAY; if (orderDay == null) { isOrdered = true; } else { isOrdered = false; } } else if (dayNumber == dayOfMonth + 1) { type = Day.DayType.TOMORROW; } else if (dayNumber == dayOfMonth + 2) { type = Day.DayType.T_D_A_T; } } else { type = Day.DayType.NOT_ENABLE; } } else { if (dayNumber <= passDays) { type = Day.DayType.ENABLE; // 明天/后天在下個月 int remainDays = getDaysInMonth(monthOfYear, curYear) - dayOfMonth; if (remainDays < 2 && dayNumber <= 2 && Math.abs(month.get(Calendar.MONTH) - monthOfYear) == 1 && month.get(Calendar.YEAR) == curYear) { if (remainDays == 1) { if (dayNumber == 1) { type = Day.DayType.T_D_A_T; } } else if (remainDays == 0) { if (dayNumber == 1) { type = Day.DayType.TOMORROW; } else if (dayNumber == 2) { type = Day.DayType.T_D_A_T; } } } } else { type = Day.DayType.NOT_ENABLE; } } if (orderInfo != null && orderInfo.length == 3 && Integer.valueOf(orderInfo[0]) == month.get(Calendar.YEAR) && Integer.valueOf(orderInfo[1]) == (month.get(Calendar.MONTH) + 1) && Integer.valueOf(orderInfo[2]) == dayNumber) { isOrdered = true; } else { isOrdered = false; } days.set(i, new Day("" + dayNumber, type, isOrdered)); dayNumber++; } return days; } }
Day.java代碼:
package siso.calendarselector.lib; public class Day { public Day(String name, DayType type, boolean isOrdered) { setName(name); setType(type); setOrdered(isOrdered); } public enum DayType { TODAY, TOMORROW, T_D_A_T, ENABLE, NOT_ENABLE } private String name; private DayType type; private boolean isOrdered; public boolean isOrdered() { return isOrdered; } public void setOrdered(boolean isOrdered) { this.isOrdered = isOrdered; } public String getName() { return name; } public void setName(String name) { this.name = name; } public DayType getType() { return type; } public void setType(DayType type) { this.type = type; } }
NoScrollGridView.java代碼:
package siso.calendarselector.lib; import android.content.Context; import android.util.AttributeSet; import android.widget.GridView; public class NoScrollGridView extends GridView { public NoScrollGridView(Context context) { super(context); } public NoScrollGridView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
AndroidManifest.xml內容:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ndroid="http://schemas.android.com/apk/res-auto" package="siso.calendarselector"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" ndroid:theme="@android:style/Theme.Black.NoTitleBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="siso.calendarselector.lib.CalendarSelectorActivity"> </activity> </application> </manifest>
項目運行結果如圖:
看完上述內容,你們掌握在Android項目中添加一個商品預訂功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。