摘要: 轉自:http://www.cnblogs.com/-OYK/archive/2011/08/03/2126657.html Android的消息機制(一) android 有一種叫消息隊列的說法,這里我們可以這樣理解:假如一個隧道就是一個消息隊列,那么里面的每一部汽車就是一個一個消息,這里我們先忽略掉超車等種種因素,只那么先進隧道的車將會先出,這個機制跟我們and...
摘要: 我們知道,Android應用程序是通過消息來驅動的,即在應用程序的主線程(UI線程)中有一個消息循環,負責處理消息隊列中的消息。我們也知 道,Android應用程序是支持多線程的,即可以創建子線程來執行一些計算型的任務,那么,這些子線程能不能像應用程序的主線程一樣具有消息循環呢?這 些子線程又能不能往應用程序的主線程中發送消息呢?本文將分析Android應用程序線程消息處理模型,為讀者解答這兩個問...
1.軟鍵盤的顯示原理
軟鍵盤其實是一個Dialog。InputMethodService為我們的輸入法創建了一個Dialog,并且對某些參數進行了設置,使之能夠在底部 或者全屏顯示。當我們點擊輸入框時,系統會對當前的主窗口進行調整,以便留出相應的空間來顯示該Dialog在底部,或者全屏。
2.活動主窗口調整
Android定義了一個屬性windowSoftInputMode, 用它可以讓程序控制活動主窗口調整的方式。我們可以在配置文件AndroidManifet.xml中對Activity進行設置。這個屬性的設置將會影響兩件事情:
1>軟鍵盤的狀態——隱藏或顯示。
2>活動的主窗口調整——是否減少活動主窗口大小以便騰出空間放軟鍵盤或是否當活動窗口的部分被軟鍵盤覆蓋時它的內容的當前焦點是可見的。
故該屬性的設置必須是下面列表中的一個值,或一個“state…”值加一個“adjust…”值的組合。在任一組設置多個值,各個值之間用|分開。
"stateUnspecified":The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an appropriate state or rely on the setting in the theme. This is the default setting for the behavior of the soft keyboard. 軟鍵盤的狀態(隱藏或可見)沒有被指定。系統將選擇一個合適的狀態或依賴于主題的設置。這個是軟件盤行為的默認設置。
"stateUnchanged":The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore. 軟鍵盤被保持上次的狀態。
"stateHidden":The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. 當用戶選擇該Activity時,軟鍵盤被隱藏。
"stateAlwaysHidden":The soft keyboard is always hidden when the activity's main window has input focus. 軟鍵盤總是被隱藏的。
"stateVisible":The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window). 軟鍵盤是可見的。
"stateAlwaysVisible":The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. 當用戶選擇這個Activity時,軟鍵盤是可見的。
"adjustUnspecified":It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the currentfocus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area. This is the default setting for the behavior of the main window. 它不被指定是否該Activity主窗口調整大小以便留出軟鍵盤的空間,或是否窗口上的內容得到屏幕上當前的焦點是可見的。系統將自動選擇這些模式中一種 主要依賴于是否窗口的內容有任何布局視圖能夠滾動他們的內容。如果有這樣的一個視圖,這個窗口將調整大小,這樣的假設可以使滾動窗口的內容在一個較小的區 域中可見的。這個是主窗口默認的行為設置。也就是說,系統自動決定是采用平移模式還是壓縮模式,決定因素在于內容是否可以滾動。
"adjustResize":(壓縮模式)The activity's main window is always resized to make room for the soft keyboard on screen. 當軟鍵盤彈出時,要對主窗口調整屏幕的大小以便留出軟鍵盤的空間。
"adjustPan":(平移模式:當輸入框不會被遮擋時,該模式沒有對布局進行調整,然而當輸入框將要被遮擋時,窗口就會進行平移。也就是說,該模式始終是保持輸入框為可見。)The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window. 該Activity主窗口并不調整屏幕的大小以便留出軟鍵盤的空間。相反,當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分。這個通常是不期望比調整大小,因為用戶可能關閉軟鍵盤以便獲得與被覆蓋內容的交互操作。。
3.偵聽軟鍵盤的顯示隱藏
有時候,借助系統本身的機制來實現主窗口的調整并非我們想要的結果,我們可能希望在軟鍵盤顯示隱藏的時候,手動的對布局進行修改,以便使軟鍵盤彈出時更加美觀。這時就需要對軟鍵盤的顯示隱藏進行偵聽。
我們可以借助軟鍵盤顯示和隱藏時,對主窗口進行了重新布局這個特性來進行偵聽。如果我們設置的模式為壓縮模式,那么我們可以對布局的onSizeChanged函數進行跟蹤,如果為平移模式,那么該函數可能不會被調用。
4.EditText默認不彈出軟件鍵盤
方法一:
在AndroidMainfest.xml中選擇哪個activity,設置windowSoftInputMode屬性為adjustUnspecified|stateHidden
例如:<activity android:name=".Main"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
方法二:
讓EditText失去焦點,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
強制隱藏Android輸入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
5.EditText始終不彈出軟件鍵盤
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
先來介紹幾個核心函數:
mktime 函數
mktime() 函數返回一個日期的 Unix 時間戳。
參數總是表示 GMT 日期,因此 is_dst 對結果沒有影響。
參數可以從右到左依次空著,空著的參數會被設為相應的當前 GMT 值。
語法
mktime(hour,minute,second,month,day,year,is_dst)
Java代碼

- 參數 描述
- hour 可選。規定小時。
- minute 可選。規定分鐘。
- second 可選。規定秒。
- month 可選。規定用數字表示的月。
- day 可選。規定天。
- year 可選。規定年。在某些系統上,合法值介于 1901 - 2038 之間。不過在 PHP 5 中已經不存在這個限制了。
- is_dst 可選。如果時間在日光節約時間(DST)期間,則設置為1,否則設置為0,若未知,則設置為-1。
-
- 自 5.1.0 起,is_dst 參數被廢棄。因此應該使用新的時區處理特性。
Java代碼

- 例子
-
- mktime() 函數對于日期運算和驗證非常有用。它可以自動校正越界的輸入:
-
- <?php
- echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
- echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
- echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
- echo(date("M-d-Y",mktime(0,0,0,1,1,99)));
- ?>
-
- 輸出:
-
- Jan-05-2002
- Feb-01-2002
- Jan-01-2001
- Jan-01-1999
strtotime 函數
strtotime() 函數將任何英文文本的日期時間描述解析為 Unix 時間戳。
語法
strtotime(time,now)
Java代碼

- 參數 描述
- time 規定要解析的時間字符串。
- now 用來計算返回值的時間戳。如果省略該參數,則使用當前時間。
一周之后: strtotime("+1 week") ;
一周之前: strtotime("-1 week") ;
一月之后: strtotime("+1 months") ;
一天之后: strtotime("+1 days") ;
30秒之后 strtotime( " +30 seconds " );
20分鐘之后 strtotime( " +20 minutes " );
12個小時之后 strtotime( " +12 hours " );
date 函數
date() 函數格式化一個本地時間/日期。
語法
date(format,timestamp)
date_default_timezone_set 函數
date_default_timezone_set() 函數設置用在腳本中所有日期/時間函數的默認時區。
語法
date_default_timezone_set(timezone)
Java代碼

- 參數 描述
- timezone 必需。時區標識符,比如 "UTC" 或 "Europe/Paris"。
- 合法時區的列表:http://www.php.net/manual/en/timezones.php
實例
第一種情況是沒有數據庫,只是得到的日期值進行比較的話,那就得完全用php的時間日期函數計算了,如下:
比如要計算2009-9-5到2009-9-18還有多少天:
Java代碼

- <?php
- $startdate=strtotime("2009-9-5");
- $enddate=strtotime("2009-9-18"); //上面的php時間日期函數已經把日期變成了時間戳,就是變成了秒。這樣只要讓兩數值相減,然后把秒變成天就可以了,比較的簡單,如下:
- $days=round(($enddate-$startdate)/3600/24) ;
- echo $days; //days為得到的天數;
- ?>
第二種 孩子的成長
Java代碼

- <?
- date_default_timezone_set('Asia/Shanghai');
- //以上一句為設置時區,其實不設也行,但是zde debug的時候會有提示,說什么不安全的函數…添上吧。
-
- echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';
-
- $stime='2005-11-03 10:08';
- echo "<br/><br/>***自出生(<font color=blue>$stime</font>)以來…:<br/><br/>";
- echo "今天是第<font color=red><b>".Lnbsp(daysofnow($stime),3)."</b></font>天<br/>";
- echo "今天是第<font color=red><b>".Lnbsp(weeksofnow($stime),3)."</b></font>周<br/>";
- echo "今天是第<font color=red><b>".Lnbsp(monthsofnow($stime),3)."</b></font>個月<br/>";
- echo "今天是第<font color=red><b>".Lnbsp(yearsofnow($stime),3)."</b></font>年<br/>";
- /*
- $output=sprintf(" 今 天是第<font color=red><b>%03d</b></font>天<br/& gt;今天是第< font color=red><b>%03d</b></font>周< br/>今天是第< font color=red><b>%03d</b></font>個 月<br/>今天是第< font color=red><b>%03d</b></font& gt; 年<br/& gt;",daysofnow($stime),weeksofnow($stime),monthsofnow($stime),yearsofnow($stime));
- echo $output;
- */
-
- function weeksofnow($stime)
- {
- $ftime=strtotime($stime);
- $fweeks=date('w',$ftime);
- if ($fweeks==0) $fweeks=7;
- $nweeks=date('w');
- if ($nweeks==0) $nweeks=7;
- $ftemp=strtotime(date('Y-m-d 00:00:00',$ftime))-$fweeks*60*60*24;
- $ntemp=strtotime(date('Y-m-d 00:00:00',time()))+(7-$nweeks)*60*60*24;
- //echo date('w',$ftemp)."<br/>....<br/>".date('w',$ntemp)."<br/>";
- return ($ntemp-$ftemp)/60/60/24/7;
- }
-
- function daysofnow($stime)
- {
- $ftime=strtotime($stime);
- return ceil(abs((time()-$ftime)/(60*60*24)));
- }
-
- function monthsofnow($stime)
- {
- $ftime=strtotime($stime);
- $fmonth=date('m',$ftime);
- $fyear=date('Y',$ftime);
- $nmonth=date('m');
- $nyear=date('Y');
- $result=($nyear-$fyear)*12+$nmonth-$fmonth+1;
- return $result;
- }
-
- function yearsofnow($stime)
- {
- $ftime=strtotime($stime);
- $fyear=date('Y',$ftime);
- $nyear=date('Y');
- return $nyear-$fyear+1;
- }
-
- // 下面的函數只是加空格用的,不是核心內容,只為美觀
- function Lnbsp($data,$num)
- {
- $result=trim($data);
- for($i=$num;$i>=strlen($data);$i--) {
- $result=' '.$result;
- }
- return $result;
- }
- ?>
第三種 明天,下個月和明年的日期,就可以用以下的代碼:
Java代碼

- $tomorrow = date('Y-m-d',mktime (0,0,0,date("m"),date("d")+1,date("Y")));
- $nextmonth = date('Y-m',mktime (0,0,0,date("m")+1,date("d")+1,date("Y")));
- $nextyear = date('Y',mktime (0,0,0,date("m"),date("d"),date("Y")+1));
-
- echo $tomorrow.'<br/>';
- echo $nextmonth.'<br/>';
- echo $nextyear.'<br/>';
第四種 工作時間(刨除假日)
Java代碼

- <?
- $startDate="2001-12-12";
- $endDate="2002-11-1";
-
- $holidayArr=array("05-01","05-02","10-01","10-01","10-02","10-03","10-04","10-05","01-26","01-27","01-28","01-29");
- //假期日期數組,比方國慶,五一,春節等
- $endWeek=2;
- //周末是否雙休.雙休為2,僅僅星期天休息為1,沒有休息為0
-
- $beginUX=strtotime($startDate);
- $endUX=strtotime($endDate);
-
- for($n=$beginUX;$n<=$endUX;$n=$n+86400){
- $week=date("w",$n);
- $MonDay=date("m-d",$n);
- if($endWeek){//去處周末休息
- if($endWeek==2){
- if($week==0||$week==6) continue;
- }
- if($endWeek==1){
- if($week==0) continue;
- }
- }
- if(in_array($MonDay,$holidayArr)) continue;
- $totalHour+=10;//每天工作10小時
- }
- echo "開始日期:$startDate<BR>";
- echo "結束日期:$endDate<BR>";
- echo "共花了".$totalHour."小時";
- ?>
第五種 mysql 處理日期 (防灌水)
Java代碼

- // $ip = getenv('REMOTE_ADDR');
-
- $sql = "SELECT TIME_TO_SEC(NOW())-TIME_TO_SEC(time_at) from test ";
- // 或者 select time_to_sec("1980-8-20 12:25:33") - time_to_sec("1997-1-23 6:04:56")
-
-
或者用
Java代碼

- SELECT DATEDIFF('2008-12-30','2008-12-29') AS DiffDate
-
- /*
- 結果:
- DiffDate
- 1
- */
或者
Java代碼

- /*
- 假設我們有如下的表:
- OrderId ProductName OrderDate
- 1 'Computer' 2008-12-29 16:25:46.635
- */
-
- SELECT OrderId,DATE_ADD(OrderDate,INTERVAL 2 DAY) AS OrderPayDate FROM Orders
-
- /*
- 結果:
- OrderId OrderPayDate
- 1 2008-12-31 16:25:46.635
- */
詳見:http://www.w3school.com.cn/sql/func_date_format.asp
http://www.w3school.com.cn/sql/sql_dates.asp
給出秒算小時 php
Php代碼

- <?php
- function transform($sec){
-
- $output = '';
-
- $hours = floor($sec / 3600);
- $remainSeconds = $sec % 3600;
-
- $minutes = floor($remainSeconds / 60);
- $seconds = $sec - $hours * 3600 - $minutes * 60;
-
- if($sec >= 3600){
- $output .= $hours.' h / ';
- $output .= $minutes.' m / ';
- }
-
- if($sec >= 60 && $sec < 3600){
- $output .= $minutes.' m / ';
- }
-
- return $output .= $seconds.' s ';
- }
-
- echo transform(3231803);
-
- ?>
帖子發表于**前 (時間戳與日期換算的應用)
Php代碼

- <?php
- function units($time){
- $year = floor($time / 60 / 60 / 24 / 365);
- $time -= $year * 60 * 60 * 24 * 365;
- $month = floor($time / 60 / 60 / 24 / 30);
- $time -= $month * 60 * 60 * 24 * 30;
- $week = floor($time / 60 / 60 / 24 / 7);
- $time -= $week * 60 * 60 * 24 * 7;
- $day = floor($time / 60 / 60 / 24);
- $time -= $day * 60 * 60 * 24;
- $hour = floor($time / 60 / 60);
- $time -= $hour * 60 * 60;
- $minute = floor($time / 60);
- $time -= $minute * 60;
- $second = $time;
- $elapse = '';
-
- $unitArr = array('年' =>'year', '個月'=>'month', '周'=>'week', '天'=>'day',
- '小時'=>'hour', '分鐘'=>'minute', '秒'=>'second'
- );
-
- foreach ( $unitArr as $cn => $u ) {
- if ( $$u > 0 ) {
- $elapse = $$u . $cn;
- break;
- }
- }
-
- return $elapse;
- }
-
- function stamp($past){
- date_default_timezone_set("America/New_York"); // 解決php5.1以上時間戳會與實際時間相差8小時,找時區請到http://www.php.net/manual/en/timezones.php
-
- $year =(int)substr($past,0,4); // 取得年份
- $month =(int)substr($past,5,2); // 取得月份
- $day =(int)substr($past,8,2); // 取得幾號
-
- $hour =(int)substr($past,11,2); // 取得小時
- $minutes =(int)substr($past,14,2); // 取得分鐘
- $second =(int)substr($past,17,2); // 取得秒數
-
- $past = mktime($hour,$minutes,$second,$month,$day,$year);
- $now = time();
- $diff = $now - $past;
-
- return '發表于' . units($diff) . '前';
- }
-
- $past = '2009-12-24 16:49:00'; // 從數據庫得到日期
-
- echo stamp($past);
-
- ?>
兩個日期相差的天數
#方法一:簡單方法
Php代碼

- $olddate = '2010-02-11'; //如果要用mktime函數,則要用explode拆解日期。
- $oldtime = strtotime($olddate);
- $passtime = time()-$oldtime; //經過的時間戳。
- echo '你在網上泡了'.floor($passtime/(24*60*60)).'天了'.'<br />'; //12天。
#方法二:用減去全年天數的時間戳來獲取。
Php代碼

- $yDate=1;
- $yDate_Y=date('Y',time())-1; //年份-1,即去年
- $yDateYMD="$yDate_Y-01-01";
- $yYMD=strtotime($yDateYMD); //去年的1月1號時間戳。
- $d=date('L',$yYMD)?366:365; //是否是閏年
- $yYearTime=$d*24*60*60;
-
- $yYear=date('Y-m-d',time()-$yYearTime);
- echo "去年的今天:$yYear<br />"; //2009-02-23
#方法三:用直接截取當前日期的年份減一,但不嚴謹,沒有考慮到閏年。
Php代碼

- #計算60年前的今天。忽略當中經過的閏年。
- $yDate_Y=$yDate_Y-59;
- $md=explode('-',date('Y-m-d'));
- $yYMD="$yDate_Y-{$md[1]}-{$md[2]}";
- echo "60年前的今天:$yYMD <br />"; //1950-02-23
#方法四: 用strtotime()和 GNU日期語法---------推薦!
Php代碼

- //3天后; //當前時間為2010-02-23
- $d=strtotime('3 days');
- echo '3天后'.date('Y-m-d',$d)."<br />";
- //3天前:
- $d=strtotime('-3 days');
- echo '3天前'.date('Y-m-d',$d)."<br />"; //2010-02-20
- //一個月前:
- $d=strtotime('-1 months');
- echo '一個月前'.date('Y-m-d',$d)."<br />"; //2010-01-23
-
- //2個月后:
- $d=strtotime('2 months');
- echo '二個月后'.date('Y-m-d',$d)."<br />"; //2010-04-23
-
- //1年前:
- $d=strtotime('-1 years');
- echo '1年前'.date('Y-m-d',$d)."<br />"; //2009-02-23
-
- //2小時前:
- $d=strtotime('-2 hours');
- echo '目前:'.date('Y-m-d H:i:s',time()).',2小時前'.date('Y-m-d H:i:s',$d)."<br />"; //目前:2010-02-23 13:38:49,2小時前2010-02-23 11:38:49
重設時間
Java代碼

- //DateTime構造函數:o bject DateTime([string $time [,dateTimeZone $timezone])
- $date = new DateTime('2010-02-23 12:26:36');
- echo $date->format('Y-m-d H:i:s')."<br />"; //和date()函數相同。2010-02-23 12:26:36
-
- //重設時間:
- //1、重設日期: boolean setDate(int year,int month,int day)
- //2、重設時間: boolean setDate(int hour,int minute[,int second])
- $date->setDate(2010,2,28);
- echo $date->format('Y-m-d H:i:s')."<br />"; //2010-02-28 12:26:36
-
- //日期計算,相當于上面的strtotime()
- $date->modify("+7 hours");
- echo $date->format('Y-m-d H:i:s')."<br />"; //2010-02-28 19:26:36
- $date->modify("3 days");
- echo $date->format('Y-m-d H:i:s')."<br />"; //2010-03-03 19:26:36 //從上面被改過的28號開始
-
- /*PHP5在WIN不支持money_format函數?
- setlocale(LC_MONETARY,'zh_CN');
- echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format()
- */
摘要: [轉載]馬甲神功之android版 (2011-11-23 22:27:40)轉載▼ 標簽: 轉載 分類: android ...