Search

Intellij IDEA - 出現 cannot resolve symbol 錯誤

2015-09-25 4:56 PM

有時 Intellij IDEA 在更新過後

或是沒有任何原因的出現 cannot resolve symbol 錯誤

但 Class 是確定存在的

此時解決的辦法就是到 File -> Invalidate Caches / Restart...

但要注意的是本地的檔案修改紀錄會完全消失

各項資料連結
Intellij IDEA

Java - JUnit + WebSockets 無法進行測試

2015-09-24 8:03 PM

當專案內有設定 Websocket 相關的設定時

啟動JUnit時會發生 No suitable default RequestUpgradeStrategy found 錯誤

這是因為一般進行測試時並不會依賴於一個執行中的Tomcat Server

因此當 Websocket 設定讀取時 DefaultHandshakeHandler 會嘗試尋找 Server 的相關 class 導致錯誤

此時只要在測試階段加入以下嵌入式的 Tomcat Library 即可

程式碼範例
  1. <!-- Libraries for testing Websocket -->
  2. <dependency>
  3. <groupId>org.apache.tomcat.embed</groupId>
  4. <artifactId>tomcat-embed-core</artifactId>
  5. <version>8.0.8</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.apache.tomcat.embed</groupId>
  10. <artifactId>tomcat-embed-websocket</artifactId>
  11. <version>8.0.8</version>
  12. <scope>test</scope>
  13. </dependency>
各項資料連結
Apache Tomcat
WebSockets

Java - 使用 Maven + JUnit 測試時中文出現亂碼

7:57 PM

若出現中文顯示為亂碼的情形

可直接在 pom.xml 加入編碼設定即可

程式碼範例
  1. <!-- Surefire test plugin -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-surefire-plugin</artifactId>
  5. <version>2.17</version>
  6. <!-- 預防使用 Maven test 時中文顯示為亂碼 -->
  7. <configuration>
  8. <argLine>-Dfile.encoding=UTF-8</argLine>
  9. </configuration>
  10. </plugin>
各項資料連結
Maven Surefire

Android - 隱藏 EditText 的虛擬鍵盤

2015-09-18 1:41 PM

在具有輸入介面時

Android 會自動跳出虛擬鍵盤給使用者輸入

但有時卻不會自動隱藏

或是有時我們需要手動隱藏虛擬鍵盤

就可以使用以下方式

顯示虛擬鍵盤使用方式詳見此篇介紹

Android - 顯示 EditText 的虛擬鍵盤

程式碼範例
  1. // 取得鍵盤管理物件
  2. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  3.  
  4. // 隱藏指定 view 的虛擬鍵盤, 通常會是 EditText
  5. imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
各項資料連結
Android - 顯示 EditText 的虛擬鍵盤
Android InputMethodManager

Android - 修改 Button 與 ListView, RecyclerView Item 的背景與點擊效果顏色

2015-09-06 1:31 PM

若直接使用 background 屬性修改按鈕等物件的背景顏色時通常無法保留點擊效果

如 android:background="#333333" 雖然修改了背景顏色

點擊觸發的預設動畫卻也跟著不見了

但其實是有辦法可以保留並自訂點擊特效的顏色

不論是背景顏色還是點擊效果的顏色都可以修改

方法就是將以下 xml 設定新增到 drawable 目錄底下即可

套用方式則是修改 background 屬性值

android:background="@drawable/YOUR_DRAWABLE_FILE_NAME"

程式碼範例

我們先看 API 21 以上的版本修改方式

新增一個 drawable 的 Resource

並將內容修改為以下 xml 格式

  1. <ripple xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:color="@color/main_login_button_ripple">
  3. <item android:drawable="@color/main_login_button_background"/>
  4. </ripple>

@color/main_login_button_ripple 為點擊後效果的顏色 可自行在 color.xml 設定

@color/main_login_button_background 為按鈕背景顏色 可自行在 color.xml 設定


API 20 以下的版本則要使用以下格式的 xml

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:drawable="@color/main_login_button_ripple" android:state_pressed="true"/>
  3. <item android:drawable="@color/main_login_button_ripple" android:state_focused="true"/>
  4. <item android:drawable="@color/main_login_button_background"/>
  5. </selector>

@color/main_login_button_ripple 為點擊後效果的顏色 可自行在 color.xml 設定

@color/main_login_button_background 為按鈕背景顏色 可自行在 color.xml 設定


所以其實就是自訂兩個 color 參數

分別給背景與特效使用即可

當然此方法也可以自訂背景的形狀 例如加上圓角

方法跟 layer-list 相同

  1. <ripple xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:color="@color/fav_item_ripple">
  3. <item>
  4. <shape>
  5. <solid android:color="@color/fav_item_background"></solid>
  6. <corners android:radius="10px"></corners>
  7. </shape>
  8. </item>
  9. </ripple>
各項資料連結
Drawable Resources

Android - 取消上一層 Layout 的點擊事件

2015-09-02 8:32 PM

當我們在同一個 activity 內需要跳出一個小型的 layout 供使用者操作時

如輸入方塊、提示、顯示圖片

在此若要同時遮蔽上一層 layout 的點擊狀態

我們就需要做一個滿版的 layout

並在 layout 上加入 clickable="true" 這個屬性

就可以遮蔽上一層 layout 內的所有點擊事件了

程式碼範例
  1. <RelativeLayout
  2. android:id="@+id/articleContentCommentHolder"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#99000000"
  6. android:visibility="gone"
  7. android:alpha="0"
  8. android:clickable="true">
  9. </RelativeLayout>