Android development of 16 small experience summarized, I hope you have to engage in Android development friends to help.
1. The GetTextSize return value in TextView is measured in pixels (px),
The Settextsize () is in SP units.
So if you set an error directly with the value returned, the workaround is to use a different form of settextsize () to specify the unit:
settextsize (int unit, int size) TypedValue.COMPLEX_UNIT_PX:Pixels TypedValue.COMPLEX_UNIT_SP:Scaled Pixels Typed Value.COMPLEX_UNIT_DIP:Device Independent Pixels
2. When drawing bitmap when inheriting from view, it is necessary to put the picture in the new drawable-xdpi, otherwise the drawing size will change easily.
3. Underline the text: Textview.getpaint (). SetFlags (Paint.strike_thru_text_flag);
4. ScrollView is inherited from Framelayout, so you need to use the framelayout when using Layoutparams
5. How to program several networks in Android:
(1) socket, ServerSocket for TCP/IP
(2) for UDP datagramsocket, Datagrampackage. It is important to note that, given that Android devices are usually handheld terminals, IP is distributed over the Internet. are not fixed. So the development is also a little different from ordinary Internet applications.
(3) HttpURLConnection for direct URLs
(4) The Apache HTTP client is integrated with Google and can be programmed using HTTP for network programming. Appache Http Core and HttpClient 4 are integrated for Http,google, so pay special attention to Android does not support HttpClient 3.x series, and currently does not support multipart (MIME), Need to add Httpmime.jar yourself
(5) Use a Web Service. Android can support XMLRPC and JSONRPC through open source packages such as Jackson, and can also use KSOAP2 to implement WebService
(6) Display the Web page directly using the WebView view component. Based on the development of WebView, Google has provided a Web browser based on Chrome-lite that allows you to surf the Web directly.
6. Translateanimation (float Fromxdelta, float toxdelta, float fromydelta, float toydelta)
This is one of our most common construction methods,
Float Fromxdelta: This parameter represents the difference between the point at which the animation starts and the current view x coordinate;
Float Toxdelta, which represents the difference between the point at which the animation ends and the current view x coordinate;
Float Fromydelta, which represents the difference between the starting point of the animation and the current view y-coordinate;
float Toydelta) This parameter represents the difference between the point at which the animation starts and the current view y-coordinate;
If the view is in a (x, y) point then the animation is moved from point B (X+fromxdelta, Y+fromydelta) to point C (X+toxdelta,y+toydelta).
7.android provides several ways to access the UI thread in other threads.
Activity.runonuithread (Runnable) view.post (Runnable) view.postdelayed (Runnable, long)
Get a webpage from the Internet and display its source code in a TextView
package org.unique.async; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.textview; public class networkactivity extends activity{ Private TextView message; private button open; private edittext url; @Override Public void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (r.layout.network); message= (TextView) findviewbyid (r.id.message); url= (EditText) findviewbyid (R.id.url); open= (Button) findviewbyid (R.id.open); open.setonclicklistener (New view.onclicklistener () { &NBsp public void onclick (view arg0) { Connect (); } }); private void connect () { Pagetask task = new pagetask (this); Task.execute (Url.gettext (). toString ()); class pagetask extends asynctask<string, integer, string> { // variable-length input parameters, with Asynctask.exucute () Correspondence progressdialog pdialog; Public pagetask (context context) { & nbsp Pdialog = new progressdialog (context, 0); Pdialog.setbutton ("Cancel", new Dialoginterface.onclicklistener () { public void onclick (dialoginterface dialog, int i) { Dialog.cancel (); } }); Pdialog.setoncancellistener (new Dialoginterface.oncancellistener () { public Void oncancel (dialoginterface dialog) { Finish (); } }); PdialoG.setcancelable (TRUE); Pdialog.setmax (100); Pdialog.setprogressstyle (progressdialog.style_horizontal); pdialog.show (); } @Override &nbs P Protected string doinbackground (string... params) { try{ HttpClient client = new defaulthttpclient (); // params[0] represents the URL of the connection & nbsp httpget get = new httpget (params[0]); HttpResponse response = Client.execute (get); HttpEntity entity = Response.getentity (); long length = Entity.getcontentlength (); inputstream is = entity.getcontent ( ); String s = null; if (is != null) { & nbsp ByteArrayOutputStream baos = new Bytearrayoutputstream (); byte[] buf = new BYTE[128]; int ch = -1; int count = 0; while (Ch = is.read (BUF)) & nbsp;! = -1) { Baos.write (buf, &NBSP;0,&NBSP;CH); count += ch; if (length > 0)   ; { // If you know the length of the response , call publishprogress () update progress & nbsp Publishprogress ((int) (count / (float) length) * 100); &NBSp } // let thread hibernate 100m S Thread.Sleep (100); } s = new string (Baos.tobytearray ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP,} & nbsp // return results return s; } catch (exception e) { & nbsp e.printstacktrace (); } return Null } @Override &nbs P protected void oncancelled () { Super.oncancelled (); } @Override &nbs P Protected void onpostexecute (String result) { // Returns the contents of the HTML page message.settext (result); Pdialog.dismiss (); } @Override &NB Sp Protected void onpreexecute () { // Task start, you can display a dialog box here, simple handling &NBSp Message.settext (r.string.task_started); } @Override &nbs P Protected void onprogressupdate (integer... values) { // update progress SYSTEM.OUT.PRINTLN ("" +valu Es[0]); Message.settext ("+values[0"); pdialog.setprogress (values[0]); } } }
8.Spinner cannot be used in dialog and Tabhost solutions
9. Eclipse Associated JDK Source code
(1). "Window", "Preferences", "Java", "Installed JREs"
(2). At this point, "installed JREs" to the right of the list pane, list the JRE environment in the system, select your JRE, and then click "Edit ..." on the edge, a window will appear (edit JRE)
(3). Select this entry for the Rt.jar file: "C:\Program Files\java\jre_1.5.0_06\lib\rt.jar" point to the left of the "+" sign to expand it,
(4). After you expand, you can see "source Attachment: (None)", point this item, point to the right button "Source Attachment ...", select "Src.zip" file under your JDK directory
10.Unable to open Sync connection!
Reboot the USB debug in the Setup
11.EditText setting the cursor position problem
There are some pre-set text in the EditText, want to put the cursor to the front, the first is the use of setselection (0), the results found in Samsung P1000 above the problem. The study found that it was necessary to call Edittext.requestfocus () before calling SetSelection (0). Otherwise, there is a problem on the 2.x machine, but the 3.x is good.
The home key in 12.Android is reserved by the system and cannot be used as onkeydown as a fallback key. However, you can add your own processing code based on activity and view events that are triggered when the home key is pressed. Online Some people say can use Onattachwindow to intercept home button, never tried
13. When rendering with Surfaceview, you can put Surfaceview and other view in layout if you want to have other view in it when you need it, you can hide other view in normal time
14. Use the Android:imeoptinos to make some interface settings on the Android-powered soft keyboard:
android:imeoptions= "Flagnoextractui"//Make the soft keyboard not full-screen, only occupy a portion of the screen at the same time, this property can also control the soft keyboard lower right button display content, by default, enter the key android:imeoptions= " Actionnone "//input box to the right without any hint android:imeoptions=" Actiongo "//Bottom right corner of the key content is ' start ' android:imeoptions= ' actionsearch '//Bottom Right The corner button is a magnifying glass image, search android:imeoptions= "actionsend"//Bottom right button content for ' send ' android:imeoptions= ' actionnext '///lower right corner key content for ' next ' android:imeoptions= "Actiondone"//Bottom right button content is ' done '
15. Add a shadow to TextView
<style name= "Overlay" > <item name= "Android:paddingleft" > 2dip</item> <item name= "Android:paddingbottom" >2dip</item> <item name= "Android:textcolor" > #ffffff </item> <item name= "android:textsize" >12sp</item> <item name= "Android:shadowcolor" > #00ff00 </item> <item name= " Android:shadowdx ">5</item> <item name=" Android:shadowdy ">3 </item> <item name= "Android:shadowradius" >6</item> </style> <textview android:id= "@+id/test" android:layout_width= "fill_parent" android:layout_height= "wrap_content" style= "@style/< Span style= "RGB (250, 250, 250); font-family: helvetica, tahoma, arial, sans-serif; ">Overlay</span>" android:text= "test" android:gravity= "center" />
16. How do I set the Chinese in TextView to bold?
Use the android:textstyle= "bold" in the XML file to set the English to bold, but you cannot set the Chinese to bold, and the Chinese to be bold in the following way:
TextView TV = (TextView) Findviewbyid (R.ID.TEXTVIEW01); Textpaint TP = Tv.getpaint (); Tp.setfakeboldtext (TRUE);
16 Small lessons from Android development summary