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 ));
Other (for more sample code, see the code in the sample folder) Output logs 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 );