//呼叫執行
new StartAsyncTask().execute();
//函式主體
class StartAsyncTask extends AsyncTask<String, Integer, Integer> {
//改變ProgressBar
@Override
protected void onProgressUpdate(Integer... progress) {
//UI Thread
}
//doInBackground之後
@Override
protected void onPostExecute(Integer result) {
//UI Thread
}
//doInBackground之前
@Override
protected void onPreExecute() {
//UI Thread
}
//背景Thread
@Override
protected Integer doInBackground(String... params) {
//可在此用Network
return 0;
}
}
2015年12月11日 星期五
2015年12月9日 星期三
Android Regular Expression正規表示法(Android的一百種奇技淫巧)
昨天看到了這個語法,就學習了一下,多用於字串處理及限制輸入,
順便找了網路上的一些範例:
1.
http://taichunmin.logdown.com/posts/178274-regular-expression-examples
正規表示法範例
2.
http://developer.android.com/intl/zh-tw/reference/java/util/regex/Pattern.html
Android上的API
3.
http://givemepass.blogspot.tw/2012/03/blog-post.html
如何使用正規表示法-1
4.
http://www.jackforfun.com/2008/07/regular-expression.html
驗證密碼的 Regular Expression
5.我覺得寫的蠻詳細的網頁是這個
https://www.javaworld.com.tw/jute/post/view?bid=20&id=130126&sty=1&tpg=1&age=-1
Java Regular Expression的學習筆記 [精華]
========================================================
手上的程式
需要密碼包含英文大小寫及數字,長度介於8-12之間
改寫後
收工。
順便找了網路上的一些範例:
1.
http://taichunmin.logdown.com/posts/178274-regular-expression-examples
正規表示法範例
2.
http://developer.android.com/intl/zh-tw/reference/java/util/regex/Pattern.html
Android上的API
3.
http://givemepass.blogspot.tw/2012/03/blog-post.html
如何使用正規表示法-1
4.
http://www.jackforfun.com/2008/07/regular-expression.html
驗證密碼的 Regular Expression
5.我覺得寫的蠻詳細的網頁是這個
https://www.javaworld.com.tw/jute/post/view?bid=20&id=130126&sty=1&tpg=1&age=-1
Java Regular Expression的學習筆記 [精華]
========================================================
手上的程式
需要密碼包含英文大小寫及數字,長度介於8-12之間
Java Regular Expression的學習筆記 [精華]public static int CheckPassword(String resource) { int length = resource.length(); if (length >= 12 || length < 8) return 0; try { int strong = 0; for (int i = 0; i < length; i++) { if (resource.charAt(i) >= 'a' && resource.charAt(i) <= 'z') { strong++; break; } } for (int i = 0; i < length; i++) { if (resource.charAt(i) >= 'A' && resource.charAt(i) <= 'Z') { strong++; break; } } for (int i = 0; i < length; i++) { if (resource.charAt(i) >= '0' && resource.charAt(i) <= '9') { strong++; break; } } for (int i = 0; i < length; i++) { if (!((resource.charAt(i) >= 'a' && resource.charAt(i) <= 'z') || (resource.charAt(i) >= 'A' && resource.charAt(i) <= 'Z') || (resource.charAt(i) >= '0' && resource.charAt(i) <= '9'))) { strong++; break; } } return strong; } catch (Exception e) { } // 例外 return 0;}
改寫後
參考了Patterns裡的EMAIL_ADDRESSpublic static boolean isValidPassword(String password){ return Pattern.compile("^(?=.*[a-zA-Z]+)(?=.*\\d+)[a-zA-Z0-9]{8,12}$") .matcher(password).matches();}
收工。
2015年11月25日 星期三
延遲執行的好方法(Android的一百種奇技淫巧)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("code", "Using in Delay");
}
}, 10000);
// In Java 8
new Handler().postDelayed(() -> {
Log.i("code", "Using in Delay");
}
}, 10000);
@Override
public void run() {
Log.i("code", "Using in Delay");
}
}, 10000);
// In Java 8
new Handler().postDelayed(() -> {
Log.i("code", "Using in Delay");
}
}, 10000);
2015年11月17日 星期二
Android NestedScrolling
http://www.race604.com/android-nested-scrolling/
先記錄
https://github.com/nuptboyzhb/SuperSwipeRefreshLayout
http://tech.meituan.com/mt-apk-adaptation.html
先記錄
https://github.com/nuptboyzhb/SuperSwipeRefreshLayout
http://tech.meituan.com/mt-apk-adaptation.html
2015年8月31日 星期一
Android automatic generate setter, getters, equals, hash code (Android的一百種奇技淫巧)
原本是想詢問有沒有辦法讓程式碼自動產生相關的程式,得知的時候非常shock!!!
原文:
方法也非常簡單,就是在你要用的程式碼上按 Alt+ Insert

剩下的自己研究研究吧!
相關資料:
原文:
How to automatically generate getters and setters in Android Studio
方法也非常簡單,就是在你要用的程式碼上按 Alt+ Insert
剩下的自己研究研究吧!
相關資料:
EqualsAndHashCode
2015年8月27日 星期四
使用newInstance來初始化Fragment (Android的一百種奇技淫巧)
原文出處:Using newInstance() to Instantiate a Fragment
問題: 使用
1. 方便使用者使用。
2. 它通過強制宣告一個static factory method去迫使使用者提供良好定義的行為,
並能同時確保使用者免受錯誤的初始化時機,使用者不再需要擔心不小心忘了初始化的參數或錯誤的使用。
問題: 使用
new MyFragment()和MyFragment.newInstance() 有什麼不同?哪個比較好?答: 使用newInstance()比較好,兩個原因:1. 方便使用者使用。
2. 它通過強制宣告一個static factory method去迫使使用者提供良好定義的行為,
並能同時確保使用者免受錯誤的初始化時機,使用者不再需要擔心不小心忘了初始化的參數或錯誤的使用。
I recently came across an interesting question on StackOverflow regarding Fragment instantiation:
What is the difference betweennew MyFragment()andMyFragment.newInstance()? Should I prefer one over the other?
Good question. The answer, as the title of this blog suggests, is a matter of proper design. In this case, the
newInstance() method is a "static factory method," allowing us to initialize and setup a new Fragment without having to call its constructor and additional setter methods. Providing static factory methods for your fragments is good practice because it encapsulates and abstracts the steps required to setup the object from the client. For example, consider the following code:public class MyFragment extends Fragment {
/**
* Static factory method that takes an int parameter,
* initializes the fragment's arguments, and returns the
* new fragment to the client.
*/
public static MyFragment newInstance(int index) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
}
Rather than having the client call the default constructor and manually set the fragment's arguments themselves, we provide a static factory method that does this for them. This is preferred over the default constructor for two reasons. One, it's convenient for the client, and two, it enforces well-defined behavior. By providing a static factory method, we protect ourselves from bugs down the line—we no longer need to worry about accidentally forgetting to initialize the fragment's arguments or incorrectly doing so.
Overall, while the difference between the two is mostly just a matter of design, this difference is really important because it provides another level of abstraction and makes code a lot easier to understand.
Feel free to leave a comment if this blog post helped (it will motivate me to write more in the future)! :)
2015年8月25日 星期二
ysl-好用的 Activity.getView()(Android的一百種奇技淫巧)
原文出處:好用的 Activity.getView()
TextView tv = (TextView)findViewById(R.id.my_text_view);
...
ImageView iv = (ImageView)findViewById(R.id.preview);
由於 findViewById() 傳回的是 View 這個通用型別。因此實務上每次都要強制轉型成實際的型別,這樣的程式碼看起來實在有點礙眼。
要解決這樣的問題,你只要在 Activity 中,加入底下這個 getView() 函式。
public final <E extends View> E
getView(int id)
{
return (E)findViewById(id);
}
之後,你就可以將原先的 findViewById() 呼叫改寫成底下這個方式:
TextView tv = getView(R.id.my_text_view);
...
ImageView iv = getView(R.id.preview);
一個小技巧,卻可以讓你的程式碼看起來清爽許多。當然在 Fragment 中,你也可加上自己的 getView(),至於如何寫?建議自己研究一下。
在 Activity 中最常看到的函式呼叫,就是 findViewById(),常見用法如下:
...
ImageView iv = (ImageView)findViewById(R.id.preview);
由於 findViewById() 傳回的是 View 這個通用型別。因此實務上每次都要強制轉型成實際的型別,這樣的程式碼看起來實在有點礙眼。
public final <E extends View> E
getView(int id)
{
return (E)findViewById(id);
}
之後,你就可以將原先的 findViewById() 呼叫改寫成底下這個方式:
TextView tv = getView(R.id.my_text_view);
...
ImageView iv = getView(R.id.preview);
一個小技巧,卻可以讓你的程式碼看起來清爽許多。當然在 Fragment 中,你也可加上自己的 getView(),至於如何寫?建議自己研究一下。
訂閱:
文章 (Atom)