You can bind the UI and events in full annotation mode. No need for findViewById and setClickListener.// The view annotation of xUtils requires the id to be provided, so that code obfuscation is not affected. @ ViewInject (R. id. textView) TextView textView; // @ ViewInject (vale = R. id. textView, parentId = R. id. parentView) // TextView textView; @ ResInject (id = R. string. label, type = ResType. string) private String label; // cancels the method used to bind an event with the method name. Using id binding is not affected by confusion. // You can bind multiple IDs @ OnClick ({R. id. id1, R. id. id2, R. id. id3}) // or @ OnClick (value = {R. id. id1, R. id. id2, R. id. id3}, parentId = {R. id. pid1, R. id. pid2, R. id. pid3}) // For more event support, see ViewCommonE. VentListener class and package com. lidroid. xutils. view. annotation. event. @ OnClick (R. id. test_button) public void testButtonClick (View v) {// The method signature must be consistent with the requirements in the interface ...}... // inject in Activity: @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); ViewUtils. inject (this); // inject view and event... textView. setText (some text ...);...} // inject in Fragment: @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. bitmap_fragment, container, false); // load the fragment layout ViewUtils. inject (this, view); // inject view and event ...} // inject: public void onActivityCreated (Bundle savedInstanceState) {super. onActivityCreated (savedInstanceState); ViewUtils. inject (this, getPreferenceScreen (); // inject view and event ...} // other overload // inject (View view); // inject (Activity activity) // inject (PreferenceActivity preferenceActivity) // inject (Object handler, View view) // inject (Object handler, Activity activity) // inject (Object handler, PreferenceGroup preferenceGroup) // inject (Object handler, PreferenceActivity preferenceActivity)
HttpUtils usage: normal get MethodHttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.GET, http://www.lidroid.com, new RequestCallBack
(){ @Override public void onLoading(long total, long current, boolean isUploading) { testTextView.setText(current + / + total); } @Override public void onSuccess(ResponseInfo
responseInfo) { textView.setText(responseInfo.result); } @Override public void onStart() { } @Override public void onFailure(HttpException error, String msg) { }});
Use HttpUtils to upload files or submit data to the server (post method)RequestParams params = new RequestParams (); params. addHeader (name, value); params. addQueryStringParameter (name, value); // BodyParamsEntity is used by default when only string parameters are contained. // similar to UrlEncodedFormEntity (application/x-www-form-urlencoded ). Params. addBodyParameter (name, value); // by default, MultipartEntity (multipart/form-data) is used after file parameters are added. // If multipart/related is required, the MultipartEntity provided in xUtils supports setting subType to related. // Use params. setBodyEntity (httpEntity) to set more types of HttpEntity (for example: // MultipartEntity, BodyParamsEntity, FileUploadEntity, InputStreamUploadEntity, StringEntity ). // For example, send the json parameter params. setBodyEntity (new StringEntity (jsonStr, charset); params. addBodyParameter (file, new File (path ));... httpUtils http = new HttpUtils (); http. send (HttpRequest. httpMethod. POST, uploadUrl ...., params, new RequestCallBack
() {@ Override public void onStart () {testTextView. setText (conn ...);} @ Override public void onLoading (long total, long current, boolean isUploading) {if (isUploading) {testTextView. setText (upload: + current ++ total);} else {testTextView. setText (reply: + current +/+ total) ;}@ Override public void onSuccess (ResponseInfo
ResponseInfo) {testTextView. setText (reply: + responseInfo. result) ;}@ Override public void onFailure (HttpException error, String msg) {testTextView. setText (error. getExceptionCode () +: + msg );}});
Use HttpUtils to download files:
- Supports resumable download, stops download tasks at any time, and starts tasks.
HttpUtils http = new HttpUtils (); HttpHandler handler = http. download (http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip,/sdcard/httpcomponents-client-4.2.5-src.zip, true, // if the target file exists, then the unfinished part continues to download. When the server does not support RANGE, it will be downloaded again. True, // if the file name is obtained from the returned information of the request, it is automatically renamed after the download is complete. New RequestCallBack
() {@ Override public void onStart () {testTextView. setText (conn ...);} @ Override public void onLoading (long total, long current, boolean isUploading) {testTextView. setText (current ++ total) ;}@ Override public void onSuccess (ResponseInfo
ResponseInfo) {testTextView. setText (downloaded: + responseInfo. result. getPath () ;}@ Override public void onFailure (HttpException error, String msg) {testTextView. setText (msg );}});... // call the cancel () method to stop downloading handler. cancel ();
BitmapUtils usageBitmapUtils bitmapUtils = new BitmapUtils (this); // load the network image bitmapUtils. display (testImageView, http://bbs.lidroid.com/static/image/common/logo.png); // load the local image (path starts with/, absolute path) bitmapUtils. display (testImageView,/sdcard/test.jpg); // load the image in assets (the path starts with assets) bitmapUtils. display (testImageView, assets/img/wallpaper.jpg); // when using ListView and other containers to display images, you can use PauseOnScrollListener to control the loading of the image listView during sliding and fast sliding. setOnScrollListener (new PauseOnScrollListener (bitmapUtils, false, true); listView. setOnScrollListener (new PauseOnScrollListener (bitmapUtils, false, true, customListener ));
LogUtils// Automatically add a TAG in the format of className. methodName (L: lineNumber) // you can set global LogUtils. allowD = false, LogUtils. allowI = false ..., determines whether to output logs. // Custom log output LogUtils. customLogger = new xxxLogger (); LogUtils. d (wyouflf );
Instance, BitmapUtils:
Public class xUtilsImageLoader {// cache and asynchronous operations are set in the framework. You do not need to set the thread pool and cache mechanism separately (you can also customize the cache path) private BitmapUtils bitmapUtils; private Context mContext; public xUtilsImageLoader (Context context) {// TODO Auto-generated constructor stub this. mContext = context; bitmapUtils = new BitmapUtils (mContext); bitmapUtils. configdefaloadloadingimage (R. drawable. logo_new); // Default background image bitmapUtils. configdefaloadloadfailedimage (R. drawable. logo_new); // loading the failed image bitmapUtils. configDefaultBitmapConfig (Bitmap. config. RGB_565); // sets the image compression type}/***** @ author sunglasses * @ category image callback function */public class CustomBitmapLoadCallBack extends DefaultBitmapLoadCallBack
{@ Override public void onLoading (ImageView container, String uri, BitmapDisplayConfig config, long total, long current) {}@ Override public void onLoadCompleted (ImageView container, String uri, Bitmap bitmap, bitmapDisplayConfig config, BitmapLoadFrom from) {// super. onLoadCompleted (container, uri, bitmap, config, from); fadeInDisplay (container, bitmap) ;}@ Override public void onLoadFailed (ImageView container, String uri, Drawable drawable) {// TODO Auto-generated method stub} private static final ColorDrawable TRANSPARENT_DRAWABLE = new ColorDrawable (android. r. color. transparent);/*** @ author sunglasses * @ category image loading effect * @ param imageView * @ param bitmap */private void fadeInDisplay (ImageView imageView, Bitmap bitmap) {// currently popular gradient effect final TransitionDrawable transitionDrawable = new TransitionDrawable (new Drawable [] {TRANSPARENT_DRAWABLE, new BitmapDrawable (imageView. getResources (), bitmap)}); imageView. setImageDrawable (transitionDrawable); transitionDrawable. startTransition (500);} public void display (ImageView container, String url) {// external interface function bitmapUtils. display (container, url, new CustomBitmapLoadCallBack ());}}
Instance: HttpGet:
Public class xUtilsGet {// automatic asynchronous processing. You do not need to process public void getJson (String url, RequestParams params, final IOAuthCallBack iOAuthCallBack) {HttpUtils http = new HttpUtils (); http. configCurrentHttpCacheExpiry (1000*10); // set the timeout value http. send (HttpMethod. GET, url, params, new RequestCallBack
() {// Interface callback @ Override public void onFailure (HttpException arg0, String arg1) {// TODO Auto-generated method stub} @ Override public void onSuccess (ResponseInfo
Info) {// TODO Auto-generated method stub iOAuthCallBack. getIOAuthCallBack (info. result); // call back data transmission using the interface});} public void getCataJson (int cityId, IOAuthCallBack iOAuthCallBack) {// external interface function String url = http: // XXXXXXXX; requestParams params = new RequestParams (); params. addQueryStringParameter (currentCityId, cityId +); getJson (url, params, iOAuthCallBack );}}
Instance: HttpPost (similar to HttpGet ):
Public class xUtilsPost {// automatic asynchronous processing of public void doPost (String url, RequestParams params, final IOAuthCallBack iOAuthCallBack) {HttpUtils http = new HttpUtils (); http. configCurrentHttpCacheExpiry (1000*10); http. send (HttpMethod. POST, url, params, new RequestCallBack
() {@ Override public void onFailure (HttpException arg0, String arg1) {// TODO Auto-generated method stub} @ Override public void onSuccess (ResponseInfo
Info) {// TODO Auto-generated method stub iOAuthCallBack. getIOAuthCallBack (info. result) ;}}) ;}public void doPostLogin (int cityId, IOAuthCallBack iOAuthCallBack) {String url = http: // xxxxxxxxxx; RequestParams params = new RequestParams (); params. addQueryStringParameter (currentCityId, cityId +); params. addBodyParameter (path,/apps/postCatch); doPost (url, params, iOAuthCallBack );}}