visited 13 states (5.77%)
Create your own visited map of The World or Triposo world travel guide for Android

2015-02-06

Android應用程式開發於Eclipse中誤刪檔案的復原方法

在Eclipse中開發Android應用程式時,常常會添加或是刪除一些class 或java file,但是在此環境中刪除的檔案是不會丟到系統中的資源回收區的,如果又沒有用管控版本的軟體來控制,就必須用Eclipse內建的本機端系統復原功能。

1.Restore from Local History (本地端歷史還原)可在該project上按右鍵可看到這些選單



2.選擇所要還原回來的檔案

2015-01-28

Android中取得各種resource的定義與初始值

常常在設計Android程式的時候,需要從系統撈一些參數與資訊,但是卻必須要提供其在Android resource xml檔案中的名稱與索引,寫一個範例如下方便使用。


int size;
size = GetResourceSize("status_bar_height");

private int GetResourceSize(String resource_name)
{
 int resourceId = getResources().getIdentifier(resource_name, "dimen", "android");
 if(resourceId >0)
  return getResources().getDimensionPixelSize(resourceId);
 return 0;
}

相關的資源檔範例:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/dimens.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<resources>
    <!-- The width that is used when creating thumbnails of applications. -->
    <dimen name="thumbnail_width">164dp</dimen>
    <!-- The height that is used when creating thumbnails of applications. -->
    <dimen name="thumbnail_height">145dp</dimen>
    <!-- The standard size (both width and height) of an application icon that
         will be displayed in the app launcher and elsewhere. -->
    <dimen name="app_icon_size">48dip</dimen>
    <!-- The maximum number of action buttons that should be permitted within
         an action bar/action mode. This will be used to determine how many
         showAsAction="ifRoom" items can fit. "always" items can override this. -->
    <integer name="max_action_buttons">2</integer>
    <dimen name="toast_y_offset">64dip</dimen>
    <!-- Height of the status bar -->
    <dimen name="status_bar_height">25dip</dimen>
    <!-- Height of the bottom navigation / system bar. -->
    <dimen name="navigation_bar_height">48dp</dimen>
    <!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
    <dimen name="navigation_bar_height_landscape">48dp</dimen>
    <!-- Width of the navigation bar when it is placed vertically on the screen -->
    <dimen name="navigation_bar_width">42dp</dimen>
    <!-- Height of notification icons in the status bar -->
    <dimen name="status_bar_icon_size">24dip</dimen>
    <!-- Size of the giant number (unread count) in the notifications -->
    <dimen name="status_bar_content_number_size">48sp</dimen>
    <!-- Height of the system bar (combined status & navigation); used by
         SystemUI internally, not respected by the window manager. -->
    <dimen name="system_bar_height">@dimen/navigation_bar_height</dimen>
    <!-- Height of notification icons in the system bar -->
    <dimen name="system_bar_icon_size">32dip</dimen>
    <!-- Margin at the edge of the screen to ignore touch events for in the windowshade. -->
    <dimen name="status_bar_edge_ignore">5dp</dimen>
    <!-- Size of the fastscroll hint letter -->
    <dimen name="fastscroll_overlay_size">104dp</dimen>
    <!-- Width of the fastscroll thumb -->
    <dimen name="fastscroll_thumb_width">64dp</dimen>
    <!-- Height of the fastscroll thumb -->
    <dimen name="fastscroll_thumb_height">52dp</dimen>
    <!-- Min width for a tablet device -->
    <dimen name="min_xlarge_screen_width">800dp</dimen>
    <!-- Default height of a key in the password keyboard for alpha (used by keyguard) -->
    <dimen name="password_keyboard_key_height_alpha">56dip</dimen>
    <!-- Default height of a key in the password keyboard for numeric (used by keyguard) -->
    <dimen name="password_keyboard_key_height_numeric">56dip</dimen>
    <!-- Default correction for the space key in the password keyboard  (used by keyguard) -->
    <dimen name="password_keyboard_spacebar_vertical_correction">4dip</dimen>
    <!-- Default horizontal gap between keys in the password keyboard (used by keyguard) -->
    <dimen name="password_keyboard_horizontalGap">3dip</dimen>
    <!-- Default vertical gap between keys in the password keyboard (used by keyguard) -->
    <dimen name="password_keyboard_verticalGap">9dip</dimen>
    <!-- Size of lockscreen outerring on unsecure unlock LockScreen -->
    <dimen name="keyguard_lockscreen_outerring_diameter">270dp</dimen>
    <!-- Default target placement radius for MultiWaveView -->
    <dimen name="multiwaveview_target_placement_radius">135dip</dimen>
    <!-- Default distance beyond which MultiWaveView snaps to the target radius -->
    <dimen name="multiwaveview_snap_margin">20dip</dimen>
    <!-- Default distance from each snap target that MultiWaveView considers a "hit" -->
    <dimen name="multiwaveview_hit_radius">60dip</dimen>
    <!-- Preference activity side margins -->
    <dimen name="preference_screen_side_margin">0dp</dimen>
    <!-- Preference activity side margins negative-->
    <dimen name="preference_screen_side_margin_negative">0dp</dimen>
    <!-- Preference activity top margin -->
    <dimen name="preference_screen_top_margin">0dp</dimen>
    <!-- Preference activity bottom margin -->
    <dimen name="preference_screen_bottom_margin">0dp</dimen>
    <!-- Preference widget area width (to the left of the text) -->
    <dimen name="preference_widget_width">48dp</dimen>
    <!-- Preference fragment padding, bottom -->
    <dimen name="preference_fragment_padding_bottom">0dp</dimen>
    <!-- Preference fragment padding, sides -->
    <dimen name="preference_fragment_padding_side">16dp</dimen>
    <!-- Weight of the left pane in a multi-pane preference layout. -->
    <integer name="preferences_left_pane_weight">4</integer>
    <!-- Weight of the right pane in a multi-pane preference layout. So the split is 40:60 -->
    <integer name="preferences_right_pane_weight">6</integer>
    <!-- Padding to the left of the preference panel breadcrumb -->
    <dimen name="preference_breadcrumb_paddingLeft">0dp</dimen>
    <!-- Padding to the right of the preference panel breadcrumb -->
    <dimen name="preference_breadcrumb_paddingRight">0dp</dimen>
    <!-- Minimum space to allocate to the left of a preference item for an icon.
        This helps in aligning titles when some items have icons and some don't. When space is
        at a premium, we don't pre-allocate any space. -->
    <dimen name="preference_icon_minWidth">0dp</dimen>
    <!-- The platform's desired minimum size for a dialog's width when it
         is along the major axis (that is the screen is landscape).  This may
         be either a fraction or a dimension. -->
    <item type="dimen" name="dialog_min_width_major">65%</item>
    <!-- The platform's desired fixed width for a dialog along the major axis
         (the screen is in landscape). This may be either a fraction or a dimension.-->
    <item type="dimen" name="dialog_fixed_width_major">320dp</item>
    <!-- The platform's desired fixed width for a dialog along the minor axis
         (the screen is in portrait). This may be either a fraction or a dimension.-->
    <item type="dimen" name="dialog_fixed_width_minor">320dp</item>
    <!-- The platform's desired fixed height for a dialog along the major axis
         (the screen is in portrait). This may be either a fraction or a dimension.-->
    <item type="dimen" name="dialog_fixed_height_major">80%</item>
    <!-- The platform's desired fixed height for a dialog along the minor axis
         (the screen is in landscape). This may be either a fraction or a dimension.-->
    <item type="dimen" name="dialog_fixed_height_minor">100%</item>
    <!-- Preference activity, vertical padding for the header list -->
    <dimen name="preference_screen_header_vertical_padding">0dp</dimen>
    <dimen name="preference_screen_header_padding_side">16dip</dimen>
    <integer name="preference_screen_header_scrollbarStyle">0x02000000</integer> <!-- outsideOverlay -->
    <integer name="preference_fragment_scrollbarStyle">0x02000000</integer> <!-- outsideOverlay -->
    <dimen name="preference_item_padding_side">8dip</dimen>
    <dimen name="preference_item_padding_inner">8dip</dimen>
    <dimen name="preference_child_padding_side">16dip</dimen>
    <!-- The platform's desired minimum size for a dialog's width when it
         is along the minor axis (that is the screen is portrait).  This may
         be either a fraction or a dimension. -->
    <item type="dimen" name="dialog_min_width_minor">95%</item>
    <!-- The width of the big icons in notifications. -->
    <dimen name="notification_large_icon_width">64dp</dimen>
    <!-- The width of the big icons in notifications. -->
    <dimen name="notification_large_icon_height">64dp</dimen>
    <!-- Minimum width of the search view text entry area. -->
    <dimen name="search_view_text_min_width">160dip</dimen>
    <!-- Preferred width of the search view. -->
    <dimen name="search_view_preferred_width">320dip</dimen>
    <!-- Dialog title height -->
    <dimen name="alert_dialog_title_height">64dip</dimen>
    <!-- Dialog button bar height -->
    <dimen name="alert_dialog_button_bar_height">48dip</dimen>
    <!-- Default height of an action bar. -->
    <dimen name="action_bar_default_height">48dip</dimen>
    <!-- Vertical padding around action bar icons. -->
    <dimen name="action_bar_icon_vertical_padding">8dip</dimen>
    <!-- Text size for action bar titles -->
    <dimen name="action_bar_title_text_size">18dp</dimen>
    <!-- Text size for action bar subtitles -->
    <dimen name="action_bar_subtitle_text_size">14dp</dimen>
    <!-- Top margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_top_margin">-3dp</dimen>
    <!-- Bottom margin for action bar subtitles -->
    <dimen name="action_bar_subtitle_bottom_margin">5dip</dimen>
    <!-- Size of clock font in LockScreen on Unsecure unlock screen. -->
    <dimen name="keyguard_lockscreen_clock_font_size">80dip</dimen>
    <!-- Size of status line font on Unsecure unlock LockScreen. -->
    <dimen name="keyguard_lockscreen_status_line_font_size">14dip</dimen>
    <!-- Size of right margin on Unsecure unlock LockScreen -->
    <dimen name="keyguard_lockscreen_status_line_font_right_margin">42dip</dimen>
    <!-- Size of top margin on Clock font to edge on unlock LockScreen -->
    <dimen name="keyguard_lockscreen_status_line_clockfont_top_margin">22dip</dimen>
    <!-- Size of top margin on Clock font to edge on unlock LockScreen -->
    <dimen name="keyguard_lockscreen_status_line_clockfont_bottom_margin">12dip</dimen>
    <!-- Padding on left margin of PIN text entry field to center it when del button is showing -->
    <dimen name="keyguard_lockscreen_pin_margin_left">40dip</dimen>
    <!-- Minimum popup width for selecting an activity in ActivityChooserDialog/ActivityChooserView. -->
    <dimen name="activity_chooser_popup_min_width">200dip</dimen>
    <!-- The default gap between components in a layout. -->
    <dimen name="default_gap">8dip</dimen>
    <!-- Text padding for dropdown items -->
    <dimen name="dropdownitem_text_padding_left">8dip</dimen>
    <!-- Text padding for dropdown items -->
    <dimen name="dropdownitem_text_padding_right">8dip</dimen>
    <!-- Width of the icon in a dropdown list -->
    <dimen name="dropdownitem_icon_width">32dip</dimen>
    <!-- Default width for a textview error popup -->
    <dimen name="textview_error_popup_default_width">240dip</dimen>
    <!-- Volume panel y offset -->
    <dimen name="volume_panel_top">80dp</dimen>
    <!-- Default padding to apply to AppWidgetHostViews containing widgets targeting API level 14 and up. -->
    <dimen name="default_app_widget_padding_left">8dp</dimen>
    <dimen name="default_app_widget_padding_top">8dp</dimen>
    <dimen name="default_app_widget_padding_right">8dp</dimen>
    <dimen name="default_app_widget_padding_bottom">8dp</dimen>
    <!-- Minimum width for an action button in the menu area of an action bar -->
    <dimen name="action_button_min_width">56dip</dimen>
    <!-- Maximum height for a stacked tab bar as part of an action bar -->
    <dimen name="action_bar_stacked_max_height">48dp</dimen>
    <!-- Maximum width for a stacked action bar tab. This prevents
         action bar tabs from becoming too wide on a wide screen when only
         a few are present. -->
    <dimen name="action_bar_stacked_tab_max_width">180dp</dimen>
 <!-- Size of notification text (see TextAppearance.StatusBar.EventContent) -->
    <dimen name="notification_text_size">14dp</dimen>
 <!-- Size of notification text titles (see TextAppearance.StatusBar.EventContent.Title) -->
    <dimen name="notification_title_text_size">18dp</dimen>
 <!-- Size of smaller notification text (see TextAppearance.StatusBar.EventContent.Line2, Info, Time) -->
    <dimen name="notification_subtext_size">12dp</dimen>
</resources>

2015-01-22

Android中以timer / sleep / delay / 更新 UI 的方法

主要是希望達成Sleep / Delay 的功並讓UI可更新,Android 不能套用 Java or C 的 sleep 方法,列出幾種可以取代的方法

以下幾個方法都可以達到 "Delay" 的效果 (A1 -> (wait 10s) -> A2 -> (wait 10s) -> A3...)

  1. Thread + sleep (Thread.sleep(xxx))
  2. Handler (handler.postDelayed(Runnable, 2000);) => Call back 到 Runnable 的 run()
  3. Timer (timer.schedule(TimerTask, 10000);) => Call back 到 TimerTask 的 run()
  4. Thread + Handler
  5. Timer + Handler
  6. SystemClock.sleep(2000);
  7. asynctask
  8. Handlerthread
  9. IntentService

若要更新 UI,可以在 UI/Main thread 或者 other thread:
  1. Thread + Handler
  2. Timer + Handler
  3. Timer: postInvalidate (update a view when you're not in the current UI thread.) => http://blog.csdn.net/lixinso/archive/2009/06/23/4290432.aspx
  4. Activity.runOnUiThread(Runnable) => http://lak4cyut.blogspot.com/2011/03/android-tip-activityrunonuithread.html
  5. View.post(Runnable)
  6. View.postDelayed(Runnable, long)
Handler / Timer 簡單的應用區分:

Handler / Timer 使用的時機:
  1. 當 Delay or Sleep 用
  2. 類似迴圈的應用
  3. 定期觸發的定期行為
只要講到 Timer or Handler,一定會跟幾個名詞綁在一起: Thread / Handler / Timer / Timertask / Message / Looper / AsyncTask

2015-01-07

BCB製作靜態Library並提供其他專案使用的方法

在C++ Builder中建立及使用libary(靜態聯結函式庫)需要依照以下步驟進行

1. 開啟C++ Builder

2. File/New/Other 選擇Libray 按OK

3. 此時會自動產生一個 Project1.bpf檔

4. 接著將要被打包的 .cpp透過 Add to project加入到專案中 ( .h不用)

5. 將此專案儲存起來 (可依照功能/目的來命名,不一定要和 .cpp/.h同名)

6. 將這些 .cpp用到的 .h放到此專案目錄下

7. Build此專案

8. 完成後,在目錄下會產生一個和專案同名的 .lib檔

9. 在拿到 .lib及 .h檔後,將這兩個檔案放到專案執行的目錄下或者,

10. .lib可放在Windows能搜尋到的目錄下即可,但 .h 一定要放在專案執行的目錄下

11. 透過Add File to project將 .lib加入到專案中 ( .h不用)

12. 在程式開頭處加入 #include "xxx.h"

13. 完成

2014-12-09

Android應用程式中設定螢幕方向或設定旋轉後行為與動態變更銀幕顯示方向

強制設定螢幕方向:

打開工程中的 AndroidManifest.xml 檔,在 中,添加屬性資訊:

android:screenOrientation="portrait"  ->垂直
android:screenOrientation="landscape" ->水平

設定螢幕旋轉後行為:

打開工程中的 AndroidManifest.xml 檔,在 中,添加一條屬性資訊:

android:configChanges=”orientation|keyboardHidden|keyboard”  ->API level <= 12
android:configChanges=”orientation|keyboardHidden|keyboard|screenSize” -> API level > 13

這個屬性指的是,當後邊屬性值代表的事件發生時,Activity 會執行某個函數,orientation 指的是當螢幕旋轉時,keyboardHidden 與keyboard指鍵盤輔助功能改變。“|”為或符號,指這兩個中任意一個發生,就執行 Activity 某個函數。如果你的開發 API 等級等於或高於 13,你還需要再加入screenSize,因為 screenSize 會在螢幕旋轉時改變。

再重寫 onConfigurationChanged() 方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // your code
    }
    else {
        // your code
    }
}

在 if、else 中可以設定所需要的動作與畫面規劃,若使用了 setContentView(R.layout.xxxx) 函數,就可以實現:每次螢幕旋轉時,調用不同的佈局。

動態更改螢幕方向:

有些時候,我們不需要把程式寫死,需要在程式中有需要的時候旋轉螢幕,例如:在“設置”裏添加一個 ListView 項,可通過點擊選擇橫屏或豎屏。(比如:電子書軟體)
假設有一個按鈕,我們僅重寫 OnClick() 函數:

@Override
public void onClick(View v) {
    // 如果豎排,則改為橫排
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    // 如果橫排,則改為豎排
    else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}
重點就是:getRequestedOrientation() 函數、setRequestedOrientation() 函數的使用。
注意:使用這種方法,必須事先在 AndroidManifest.xml 的 中,添加android:screenOrientation 屬性值,不然 getRequestedOrientation() 可能會出問題。

2014-12-08

Android應用程式中Timer元件的使用範例

以Timer元件做一個計時器來了解Timer的設計與應用

activity_main.xml:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="碼表:" />

        <TextView
            android:id="@+id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開始" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />
        
        <Button
            android:id="@+id/zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="歸零" />        
            
        <Button
            android:id="@+id/end"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結束" />        
    </LinearLayout>

</LinearLayout>
MainActivity.java:



package com.example.timer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView timer;
	private Button start;
	private Button stop;
	private Button zero;
	private Button end;
	private boolean startflag=false;
	private int tsec=0,csec=0,cmin=0;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		timer = (TextView)findViewById(R.id.timer);
		start = (Button)findViewById(R.id.start);
		stop = (Button)findViewById(R.id.stop);
		zero = (Button)findViewById(R.id.zero);
		end = (Button)findViewById(R.id.end);
		
		//宣告Timer
		Timer timer01 =new Timer();
		
		//設定Timer(task為執行內容,0代表立刻開始,間格1秒執行一次)
		timer01.schedule(task, 0,1000);

		//Button監聽
		start.setOnClickListener(listener);
		stop.setOnClickListener(listener);
		zero.setOnClickListener(listener);
		end.setOnClickListener(listener);
	}
	
	//TimerTask無法直接改變元件因此要透過Handler來當橋樑
	private Handler handler = new Handler(){
		 public  void  handleMessage(Message msg) {
			 super.handleMessage(msg);
			 switch(msg.what){
			 case 1:
		 csec=tsec%60;
		 cmin=tsec/60;
                 String s="";
                 if(cmin <10 s="<span" style="color: #2a00ff;">"0"
+cmin; }else{ s=""+cmin; } if(csec < 10){ s=s+":0"+csec; }else{ s=s+":"+csec; } //s字串為00:00格式 timer.setText(s); break; } } }; private TimerTask task = new TimerTask(){ @Override public void run() { // TODO Auto-generated method stub if (startflag){ //如果startflag==true則每秒tsec+1 tsec++; Message message = new Message(); //傳送訊息1 message.what =1; handler.sendMessage(message); } } }; private OnClickListener listener =new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.start: startflag=true; break; case R.id.stop: startflag=false; break; case R.id.zero: tsec=0; //TextView 初始化 timer.setText("00:00"); break; case R.id.end: finish(); break; } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }