編輯:關於Android編程
package com.wwj.datetimepicker; import java.text.SimpleDateFormat; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; /** * 日期時間選擇控件 使用方法: private EditText inputDate;//需要設置的日期時間文本編輯框 private String * initDateTime=2012年9月3日 14:44,//初始日期時間值 在點擊事件中使用: * inputDate.setOnClickListener(new OnClickListener() { * * @Override public void onClick(View v) { DateTimePickDialogUtil * dateTimePicKDialog=new * DateTimePickDialogUtil(SinvestigateActivity.this,initDateTime); * dateTimePicKDialog.dateTimePicKDialog(inputDate); * * } }); * * @author */ public class DateTimePickDialogUtil implements OnDateChangedListener, OnTimeChangedListener { private DatePicker datePicker; private TimePicker timePicker; private AlertDialog ad; private String dateTime; private String initDateTime; private Activity activity; /** * 日期時間彈出選擇框構造函數 * * @param activity * :調用的父activity * @param initDateTime * 初始日期時間值,作為彈出窗口的標題和日期時間初始值 */ public DateTimePickDialogUtil(Activity activity, String initDateTime) { this.activity = activity; this.initDateTime = initDateTime; } public void init(DatePicker datePicker, TimePicker timePicker) { Calendar calendar = Calendar.getInstance(); if (!(null == initDateTime || .equals(initDateTime))) { calendar = this.getCalendarByInintData(initDateTime); } else { initDateTime = calendar.get(Calendar.YEAR) + 年 + calendar.get(Calendar.MONTH) + 月 + calendar.get(Calendar.DAY_OF_MONTH) + 日 + calendar.get(Calendar.HOUR_OF_DAY) + : + calendar.get(Calendar.MINUTE); } datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), this); timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE)); } /** * 彈出日期時間選擇框方法 * * @param inputDate * :為需要設置的日期時間文本編輯框 * @return */ public AlertDialog dateTimePicKDialog(final EditText inputDate) { LinearLayout dateTimeLayout = (LinearLayout) activity .getLayoutInflater().inflate(R.layout.common_datetime, null); datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker); timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker); init(datePicker, timePicker); timePicker.setIs24HourView(true); timePicker.setOnTimeChangedListener(this); ad = new AlertDialog.Builder(activity) .setTitle(initDateTime) .setView(dateTimeLayout) .setPositiveButton(設置, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { inputDate.setText(dateTime); } }) .setNegativeButton(取消, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { inputDate.setText(); } }).show(); onDateChanged(null, 0, 0, 0); return ad; } public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { onDateChanged(null, 0, 0, 0); } public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 獲得日歷實例 Calendar calendar = Calendar.getInstance(); calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), timePicker.getCurrentHour(), timePicker.getCurrentMinute()); SimpleDateFormat sdf = new SimpleDateFormat(yyyy年MM月dd日 HH:mm); dateTime = sdf.format(calendar.getTime()); ad.setTitle(dateTime); } /** * 實現將初始日期時間2012年07月02日 16:45 拆分成年 月 日 時 分 秒,並賦值給calendar * * @param initDateTime * 初始日期時間值 字符串型 * @return Calendar */ private Calendar getCalendarByInintData(String initDateTime) { Calendar calendar = Calendar.getInstance(); // 將初始日期時間2012年07月02日 16:45 拆分成年 月 日 時 分 秒 String date = spliteString(initDateTime, 日, index, front); // 日期 String time = spliteString(initDateTime, 日, index, back); // 時間 String yearStr = spliteString(date, 年, index, front); // 年份 String monthAndDay = spliteString(date, 年, index, back); // 月日 String monthStr = spliteString(monthAndDay, 月, index, front); // 月 String dayStr = spliteString(monthAndDay, 月, index, back); // 日 String hourStr = spliteString(time, :, index, front); // 時 String minuteStr = spliteString(time, :, index, back); // 分 int currentYear = Integer.valueOf(yearStr.trim()).intValue(); int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1; int currentDay = Integer.valueOf(dayStr.trim()).intValue(); int currentHour = Integer.valueOf(hourStr.trim()).intValue(); int currentMinute = Integer.valueOf(minuteStr.trim()).intValue(); calendar.set(currentYear, currentMonth, currentDay, currentHour, currentMinute); return calendar; } /** * 截取子串 * * @param srcStr * 源串 * @param pattern * 匹配模式 * @param indexOrLast * @param frontOrBack * @return */ public static String spliteString(String srcStr, String pattern, String indexOrLast, String frontOrBack) { String result = ; int loc = -1; if (indexOrLast.equalsIgnoreCase(index)) { loc = srcStr.indexOf(pattern); // 取得字符串第一次出現的位置 } else { loc = srcStr.lastIndexOf(pattern); // 最後一個匹配串的位置 } if (frontOrBack.equalsIgnoreCase(front)) { if (loc != -1) result = srcStr.substring(0, loc); // 截取子串 } else { if (loc != -1) result = srcStr.substring(loc + 1, srcStr.length()); // 截取子串 } return result; } }
package com.wwj.datetimepicker; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; /** * 時間拾取器界面 * * @author wwj_748 * */ public class DateTimePickerActivity extends Activity { /** Called when the activity is first created. */ private EditText startDateTime; private EditText endDateTime; private String initStartDateTime = 2013年9月3日 14:44; // 初始化開始時間 private String initEndDateTime = 2014年8月23日 17:44; // 初始化結束時間 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 兩個輸入框 startDateTime = (EditText) findViewById(R.id.inputDate); endDateTime = (EditText) findViewById(R.id.inputDate2); startDateTime.setText(initStartDateTime); endDateTime.setText(initEndDateTime); startDateTime.setOnClickListener(new OnClickListener() { public void onClick(View v) { DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil( DateTimePickerActivity.this, initEndDateTime); dateTimePicKDialog.dateTimePicKDialog(startDateTime); } }); endDateTime.setOnClickListener(new OnClickListener() { public void onClick(View v) { DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil( DateTimePickerActivity.this, initEndDateTime); dateTimePicKDialog.dateTimePicKDialog(endDateTime); } }); } }
講到http就必需要了解URI和URL URI (uniform resource identifier)統一資源標志符; URL(uniform resource lo
本節引言: 本節我們繼續來學習Android中的Drawable資源,上一節我們學習了: ColorDrawable;NinePatchDrawa
好了下面進入正題,我們先看一下實現效果吧:下面來介紹一下代碼: 本思路就是: 1.先到手機中掃描jpeg和png的圖片 2.獲取導圖片的路徑和圖片的父路徑名
完美解決解決Android使用Zxing掃描二維碼改成豎屏後,後面的預覽畫面出現了拉伸,扭曲的情況第一步:找到com.zxing.camera包下的CameraConf