2014年2月25日 星期二

[Android] eclipse除錯

1. 刪除support library後, 使用library的project再次讀取卻error?

2. 刪除project, 再次從workspace import進來, eclipse顯示錯誤無法匯入?
"Invalid project description. overlaps the location of another project"
改用 General > Existing Projects Into Workspace 就行了
http://blog.changyy.org/2013/08/android-eclipse-import-invalid-project.html


3. →刪除project, 再次從workspace import進來, 勾選copy project into workspace, 檔案會覆蓋(MainActivity.java呈現空白)

4. R.檔消失, 重開eclipse或重新匯入專案都不會好
→ Clean再Build project會好, 如果沒出現通常是XML檔名有問題, 這次是target=android-16版本太低不能供theme-color使用

http://wangshifuola.blogspot.tw/2011/06/androidrjava.html
http://ephrain.pixnet.net/blog/post/47238762-%5Bandroid%5D-android-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98%EF%BC%9A%E4%BF%AE%E5%BE%A9-r.java-%E4%B8%8D%E8%A6%8B%E7%9A%84%E5%95%8F
http://www.mybringback.com/travis-android-help/1/hello-world/

4. eclipse變數反白設定
http://blog.xuite.net/akuox/caffeine/43266804

5. The connection to adb is down, and a severe error has occured. 再重啟Eclipse沒好
Build Automatically忘了開, 裝置管理員關閉adb第一個, 再重啟Eclipse後OK

http://fecbob.pixnet.net/blog/post/36105987-android%E4%B8%ADthe-connection-to-adb-is-down%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95

6. 打字覆蓋到後面的字
案到insert(INS)鍵, 再按一次會回來

7. 更新ADT 22.6顯示No Updates were found.
http://blog.mosil.biz/2013/02/this-android-sdk-requires-android-developer-toolkit-above-version/

2014年1月16日 星期四

[Android] Android Development Tutorial 6速記- parsing XML use DOM (1)

1. android:ems=“10”

android:ems = "10" 设置TextView或者Edittext的宽度为10个字符的宽度。当设置该属性后,控件显示的长度就为10个字符的长度,超出的部分将不显示。

2.
<EditText  
     android:id="@+id/phone"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content">
     <requestFocus/>
</EditText>
get the focus, 滑鼠游標在的位置, 鍵盤自動跳出, 記得結尾要有>記號
當多個地方使用 requestFocus,只會有一個地方生效,就是最後執行 requestFocus 的地方
http://cw1057.blogspot.tw/2011/11/android-edittext-focus.html

EditText.requestFocus();//让EditText获得焦点,但是获得焦点并不会自动弹出键盘
http://blog.csdn.net/qq435757399/article/details/7947862

3. EditText設android:layout_weight="1", TextView不設,只有"wrap_content"
TextView 先佔用了"wrap_content"大小, 再讓EditText占用剩餘部分的1

weight是指某个组件在布局中【剩余空间】中的显示权重,那么所谓的剩余空间又是什么意思呢,其实是在没有设置weight属性的控件优先显示的情况,将未占用的布局空间合理分配给设置过weight的组件
<Button>     </>
<TextView> </>

5. Layout設Android 4.4(API 19)無法用colors寫字串直接在backgroud選取? →設Android4.3可以

6. dp & sp
px(pixel)是絕對大小, 在不同設備差異較大不建議使用
設定dp是依照設備pixel的密度分成四個係數, 係數相乘以該圖的dp就是pixel, 使每個設備差異不會太大, 最常用
sp(與縮放無關的抽象像素), 依設備用戶調整文字尺寸大小(小、正常、大、超大等等)時有所不同, 通常用在文字
http://rritw.com/a/caozuoxitong/Android/2012/1106/249015.html

7. 通过设置属性android:layout_span来声明一个widget跨越的列数
http://hi.baidu.com/hemisp/item/35e12acfd12285daef183bcf
android:layout_span="1"                     android:layout_span="2"


8. android:padding="5dp" →元件的上下領域大小(兩水平線的距離), 這邊用在TableLayout, ScrollView跟TextView, 也可以在layout控制面版上改

9. android:stretchColumns="yes" 元件延展到整個剩餘可用的範圍, 這邊用在TableLayout

10. android:background="@android:color/transparent" →透明效果

11. android:textIsSelectable="true"

Android SDK 3.0 版本之後所支援的文字選取 (textIsSelectable) 之新功能,以允許使用者在執行階段可選取並複製該TextView 文字標籤介面元件所顯示的資料。



2014年1月13日 星期一

[Android] Android Development Tutorial 7速記- parsing XML use DOM (2)

1. 資料儲存的方式之一
Shared Preferences





Store private primitive data in key-value pairs.
使用 Context.getSharedPreference(String name, int mode) 方法可以建立多個 SharedPreferences 檔案,每一個檔案透過 name 參數來命名,該檔案的讀寫權限則是利用 mode 參數來做設定。
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.




2. 鍵值對 key-value pairs, 用一個屬性對應一個值的方式 通过键key便可以找到相应的值 , 只有找到其他語言的解說如下列網址:




3.
// Table inside the scroll view that holds stock symbols
// and buttons
private TableLayout stockTableScrollView;
為什麼這裡宣告的是TableLayout, 而不適TableRow或ScrollView?


4.
// Get the saved stocks
String[] stocks = stockSymbolsEntered.getAll().keySet().toArray(new String[0]);
getSharedPreferences.getAll()後為什麼能呼叫.keySet()和.toArray? 

keySet() is a method that returns a set of all the keys of the Map(public Set<K> keySet ()). So, methods are not to be imported. Though Map is not imported but the return type of getAll()method is Map<String, ?> so it can be directly accessed



跟Hash Map相關, 暫時回去看java.util.Map<K, V>
→ Map介面的實作可用來建立鍵值對應資料(補充比喻: 像搜尋關鍵字可找到對應的資料for第二點疑惑)
Set介面實做Collection介面, 
Collection介面為收集物件用, Set介面為收集過程中, 不重複收集相同物件用

SharedPreferences.getAll()   public abstract Map<String, ?> getAll ()
Map.keySet()                      public abstract Set<K> keySet () →抽象方法,Set<K>型態
Set.toArray()                       
public abstract T[] toArray (T[] array)


Thanks for your help, I think I understand. Because you are returning a map reference through getAll() and then calling keySet() on that you can then use the keySet() with out having to import the Map class, unlike the situation you pointed out where returning getAll() directly to a defined map object would require the import of the Map class.
因為經由getAll()回傳一個Map參考後直接呼叫keySet()來使用(keySet是Map介面底下的方法),不是將getAll()回傳到一個Map定義的物件, 所以不用import Map底下實作的類別

→同理keySet()回傳Set參考後呼叫toArray()直接使用, , 所以不用import Set底下實作的類別

  1. allUserName = sp.getAll().keySet().toArray(new String[0]);  
  2.                 // sp.getAll()返回一张hash map  
  3.                 // keySet()得到的是a set of the keys.  
  4.                 // hash map是由key-value组成的  


5. (延伸至4) Set<K>的意思?
<>指的是泛型

? 表示不确定的java类型。 
T  表示java类型。 
K V 分别代表java键值中的Key Value。 
E 代表Element。 

<T>是 Type
<K,V>是说键值对~就是 Key 和 Value HashMap
<E> 是 Element
<?> 是 通配符

6. 
// Sort the stocks in alphabetical order
Arrays.sort(stocks, String.CASE_INSENSITIVE_ORDER);
sort方法的使用?
sort()為Array陣列的方法, collections也有sort()方法
http://openhome.cc/Gossip/JavaGossip-V1/ArraysClass.htm
Java技術手冊 9-23頁


对集合框架进行排序
如果已经理解了Arrays.sort()对数组进行排序的话,集合框架的使用也是大同小异。只是将Arrays替换成了Collections,注意Collections是一个类而Collection是一个接口.
http://hi.baidu.com/yljf184/item/5d7f8c2c36b9960b42634aca


sort(char[] array)
Sorts the specified array in ascending numerical order.
sort(T[] array, Comparator<? super T> comparator)
Sorts the specified array using the specified Comparator.
sort(Object[] array)
Sorts the specified array in ascending natural order.
sort(T[] array, int start, int end, Comparator<? super T> comparator)
Sorts the specified range in the array using the specified Comparator.
sort(Object[] array, int start, int end)
Sorts the specified range in the array in ascending natural order.
是哪一個API方法?
sorts()對物件排序有兩個版本, 一個是你收集在陣列中的物件必須是Comparable(否則會拋出ClassCastException), 另一個版本則可以傳入Comparator指定排序方式。
Java技術手冊 9-23頁

猜測是Comparable方式(但不知哪個?), 因為String類別有實作Comparable介面
→eclipse顯示為這個:
sort(T[] array, Comparator<? super T> comparator)
Sorts the specified array using the specified Comparator.


7. 二元搜尋要先排序
http://blog.ccc99.tw/2010/05/arraysbinarysearch.html

嚴格按字母表順序排序,也就是忽略大小寫排序 Case-insensitive sort
  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
http://www.javaworld.com.tw/jute/post/view?bid=29&id=308357

8.  if(newStockSymbol!=null) 
這裡newStockSymbol怎麼知道是輸入的值?


9.
insertStockInScrollView(newStockSymbol, Arrays.binarySearch(stocks, newStockSymbol));
→將stock插入到ScrollView(要插入的值是newStockSymbol, 順序用二元搜尋找出(陣列名, 要插入的值))

陣列中二元搜尋法?
在以排序的陣列中, 將欲搜尋的值跟中間值做比對, 比較後, 中間值以外的另一邊即可忽略不比較, 循著這個規則一值拆半, 是個用以節省時間的方法
http://program-lover.blogspot.tw/2008/08/binary-search.html

insertStockInScrollView(stocks[i], i);
                     →(值, 順序)

10. private void saveStockSymbol(String newStock){
這方法哪時呼叫?
→按下EnterButton時呼叫並傳入引數

11. SharedPreferences.Editor
→修改SharedPreferences物件的介面

Android的共享選項編輯器(SharedPreferences.Editor)

共享選項編輯器(SharedPreferences.Editor)是一個用於修改SharedPreferences對象值的接口. 編輯選項值後, 調用commit()或apply()方法以更新SharedPreferences.
http://androidbiancheng.blogspot.tw/2011/02/androidsharedpreferenceseditor.html

可透過SharedPreferences.Editor編輯介面來更新偏好設定內容。這項作業必須先使用edit()方法取得SharedPreferences.Editor編輯介面,再使用putBoolean()putFloat()putInt()putLong()putString()方法來分別寫入布林資料、浮點數資料、整數資料、長整數資料及字串資料到偏好設定檔中。 public abstract SharedPreferences.Editor putBoolean (String key, boolean value)
    public abstract SharedPreferences.Editor putFloat (String key, float value)     public abstract SharedPreferences.Editor putInt (String key, int value)     public abstract SharedPreferences.Editor putLong (String key, long value)     public abstract SharedPreferences.Editor putString (String key, String value) 在此key表示偏好設定檔中的資料名稱,value則是資料值。有一點要注意,這些偏好設定值並不會立即更新,而是等到執行commit()方法後才會寫回偏好設定檔。 →或apply()
http://nkeegamedev.blogspot.tw/2010/08/android-sharedpreference.html

12. stockSymbolsEntered.edit()
提供一editor給SharedPreferences物件, 經由它可以用來修改資料

13. if(isTheStockNew == null)
為什麼這裡是空值?

14. LayoutInflater的作用?
加載自定義布局文件
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
像這裡的TableLayout中ScrollView, 先用LayoutInflater抓進來, 並且把其中的元件初始化









15. 方法的命名與參數傳遞到底怎實作?
有回傳值時 :
因為回傳值是暫存於記憶體, 因此一定要在方法名稱前宣告回傳值的資料型別, 才知道要配置多大的記憶空間
當方法加上 "static" 修飾詞時稱為靜態方法, 這種方法屬於類別方法, 而非實體方法, 可以在類別外面直接以類別名稱呼叫, 不須事先建立物件實體.

參數與引數有何不同 ?
參數 (Parameter) 與引數 (Argument) 概念不同, 參數是方法宣告中欲傳入之變數, 而引數則是呼叫該方法時, 實際傳入的資料. 其實兩者是互相對應的.

從 static 的 main 方法中呼叫 add, 這個類別內方法 add 也必須宣告成 static, 否則該方法是無法被參考到的 :
"Javatest.java:3: non-static method add(int,int) cannot be referenced from a static context"

呼叫靜態方法 :
靜態方法為宣告時添加 "static" 修飾詞者, 係類別方法, 在類別外可以直接以類別名稱呼叫, 不須建立物件實體.
方法所屬類別名稱.方法名稱(引數);
例如 :
public class Javatest {
  public static void main(String[] args) {
    int sum=Dosomething.add(12,21);
    System.out.println("Sum=" + sum);
    }
  }
class Dosomething {
  public static int add(int a, int b) {
    return a+b;
    }
  }

呼叫非靜態方法 :
非靜態方法屬於物件實體方法, 必須先建立物件實體之後, 透過物件參考來呼叫 :
方法所屬類別名稱 物件參考變數名稱=new 方法所屬類別名稱();
物件參考變數名稱.方法名稱();
例如 :
public class Javatest {
  public static void main(String[] args) {
    Dosomething ds=new Dosomething();
    int sum=ds.add(12,21);
    System.out.println("Sum=" + sum);
    }
  }
class Dosomething {
  public int add(int a, int b) {
  return a+b;
  }
}
http://tony1966.myweb.hinet.net/java/core/method_parameter.htm

16. 這裡為什麼是傳給stockSymbolTextView?
TextView newStockTextView = (TextView) newStockRow.findViewById(R.id.stockSymbolTextView);

newStockTextView.setText(stock);

17. if(stockSymbolEditText.getText().length() < 0){
這邊可以這樣判斷字串大於0, 學起來用

18. 無焦點時阻止輸入法彈出
InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(stockSymbolEditText.getWindowToken(), 0);

InputMethodManagerhttp://my.oschina.net/jbcao/blog/61035
WindowManager
INPUT_METHOD_SERVICE
http://developer.android.com/reference/android/view/WindowManager.html

有焦點時:
http://4225953-163-com.iteye.com/blog/1235324

19. v.parent抓buttom上一層的TableRow

20. ACTION_VIEW 显示数据给用户
常见的Activity Action Intent常量
常量名称
常量值
意义
ACTION_MAIN
android.intent.action.MAIN
应用程序入口
ACTION_VIEW
android.intent.action.VIEW
显示数据给用户
http://hualang.iteye.com/blog/983471

Intent 用法大公開, 這邊有可用的例子

uri.parse
→加網址
一般找到的用法, uri分兩次寫: 
  1. /*查找某个地方并作出标注*/  
  2. Intent intent=new Intent(Intent.ACTION_VIEW);  
  3. String url = "https://maps.google.com/maps?q=31.207149,121.593086(金科路)&z=17&hl=en";  
  4. Uri uri = Uri.parse(url);  
  5. intent.setData(uri);  
  6. startActivity(intent);

2014年1月9日 星期四

[Android] Android training速記-安裝驅動

1. 安裝手機驅動 → win8 從裝置管理員手動安裝, 選android SDK資料夾(Google USB driver要先下載好)的\extras\....\android_winusb.inf, 選ADB interface即可

2. 找到RD之前用的改winusb.inf說明, XP安裝方式, 要找到android裝置, 手機名稱的可攜式裝置上的硬體識別碼看起來不太一樣, 選擇時Android ADB interface等三個都試試看, anyway後續有需要再研究: 只有手機名稱的可攜式裝置時? →改USB連接方式 (目前看HTC sensation選擇USB數據連線時硬體識別碼是對的, 但裝好連接上則是要選網際網路傳送才會出現Android ADB interface)
http://j796160836.pixnet.net/blog/post/29610725-%5Bandroid%5D-google%E5%87%BA%E7%9A%84android%E8%90%AC%E7%94%A8%E9%A9%85%E5%8B%95%EF%BC%8C%E9%A9%85%E5%8B%95%E8%A3%9D%E4%B8%8D%E4%B8%8A

3. 產生APK做法: →專案按右鍵→android tools→export signed application package, 同樣簽署名可以用同一模組

4. 沒有SD卡讀取手機資料夾放入APK檔案?
5. 從系統字元輸入adb指令方式放入APK檔案?

[Android] Android training速記-Adding Action Buttons

1. 新建res/menu/main_activity_actions.xml, 每一個想增加的buttom加入item→實作時加在menu.xml就成功了
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
2. follow XML裡的 @drawable/ , 圖檔存放在 res/drawable/ directory
3. Android 2.1以前, the showAsAction屬性不用 android: , 用APP名取代


menu/main.xml加入
xmlns:識別字="http://schemas.android.com/apk/res-auto"

而每個 item 的最後一列用
識別字:showAsAction="ifRoom"→ 雖說是舊版才要用, Nexus7 API4.3實作後才成功顯示icon, 而不是在menu裡
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...</menu>
4. 加入Action Buttons的程式碼, 確定按鍵是否被按下

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
5. action_search出現錯誤?→重建一單純範例成功

6. Android 4.1以上 (API level 16), up鍵根據activity's parent in the manifest導航, 不用額外設定up鍵event

7.  → Main.Activity加入以下程式碼, 加入按鈕後行為:
→ 順便運用Toast訊息

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle presses on the action bar items
    switch (item.getItemId())
    {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_edit:
            openEdit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
  
public void openSearch()
{
    Toast.makeText(this, "按了 尋找 鈕", Toast.LENGTH_LONG).show();
}
  
public void openEdit()
{
    Toast.makeText(this, "按了 Edit 鈕", Toast.LENGTH_LONG).show();
}

8. R.id.xxxx出現錯誤時, 回到extends Activity並把以下程式碼刪掉, 再重新key R.id.xxxxx, 用回extends ActionBarActivity