Summary of 16 small experiences developed by Android and 16 articles developed by Android

Source: Internet
Author: User

Summary of 16 small experiences developed by Android and 16 articles developed by Android

Summary of 16 small experiences in Android development. I hope it will be helpful to anyone who is engaged in Android development.

  1. The getTextSize returned value in TextView is in pixels (px,

The unit of setTextSize () is sp.

Therefore, an error occurs when the returned value is directly set. The solution is to use another form of setTextSize (). You can specify the unit:

setTextSize(int unit, int size)   TypedValue.COMPLEX_UNIT_PX : Pixels   TypedValue.COMPLEX_UNIT_SP : Scaled Pixels   TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels

  2. When inheriting from the View, you need to put the image in the new drawable-xdpi when creating a bitmap. Otherwise, the drawing size may change.

  3. Underline the text: textView. getPaint (). setFlags (Paint. STRIKE_THRU_TEXT_FLAG );

  4. scrollView is inherited from frameLayout, so frameLayout must be used when LayoutParams is used.

  5. Several network programming methods in Android:

(1) Socket and ServerSocket for TCP/IP

(2) UDP-specific initramsocket and initrampackage. Note that, considering that Android devices are usually handheld terminals, IP addresses are allocated along with the Internet. Not fixed. Therefore, development is also different from that of common Internet applications.

(3) HttpURLConnection for direct URLs

(4) Google integrates the Apache HTTP client and can use HTTP for network programming. For HTTP, Google integrates Appache Http core and httpclient 4. Therefore, Android does not support httpclient 3. x Series, and does not currently support Multipart (MIME), you need to add httpmime. jar

(5) use Web Service. Android can support Xmlrpc and Jsonrpc through open-source packages such as Jackson, and Ksoap2 can also be used to implement Webservice.

(6) directly use the WebView component to display webpages. Developed based on WebView, Google has provided a chrome-lite-based Web browser that can directly browse the Web page.

  6. TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

This is one of the most common constructor methods,

Float fromXDelta: the difference between the animation start point and the current View X coordinate;

Float toXDelta, which indicates the difference between the animation end point and the current View X coordinate;

Float fromYDelta. This parameter indicates the difference between the start point of the animation and the Y coordinate of the current View;

Float toYDelta) indicates the difference between the start point of the animation and the Y coordinate of the current View;

If the view is at A (x, y), the animation moves from point B (x + fromXDelta, y + fromYDelta) to point C (x + toXDelta, y + toYDelta.

  7. android provides several methods to access the UI thread in other threads.

Activity.runOnUiThread( Runnable ) View.post( Runnable ) View.postDelayed( Runnable, long )

Obtain 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; im Port 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 () {public void onClick (View arg0) {connect () ;}});} private void connect () {PageTask task = new PageTask (this); task.exe cute (url. getText (). toString ();} class PageTask extends AsyncTask <String, Integer, String> {// variable length input parameter, with AsyncTask. exucute () corresponds to ProgressDialog pdialog; public PageTask (Context context) {pdialog = new ProgressDialog (context, 0); pdialog. setButton ("cancel", new DialogInterface. onClickListener () {public void onClick (DialogInterface dialog, int I) {dia Log. 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 protected String doInBackground (String... params) {try {HttpClient client = new DefaultHttpClient (); // params [0] indicates the connected url HttpGet = new HttpGet (params [0]); HttpResponse response = client.exe cute (get); HttpEntity entity = response. getEntity (); long length = entity. getContentLength (); InputStream is = entity. getContent (); String s = null; if (is! = Null) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); byte [] buf = new byte [128]; int ch =-1; int count = 0; while (ch = is. read (buf ))! =-1) {baos. write (buf, 0, ch); count + = ch; if (length> 0) {// if you know the response length, call publishProgress () to update the publishProgress (int) (count/(float) length) * 100);} // sleep the Thread for 100 ms Thread. sleep (100);} s = new String (baos. toByteArray ();} // return s;} catch (Exception e) {e. printStackTrace ();} return null;} @ Override protected void onCancelled () {super. onCancelled () ;}@ Override protected void onPostExecute (String result) {// return the message of the HTML page. setText (result); pdialog. dismiss () ;}@ Override protected void onPreExecute () {// start the task. A dialog box is displayed here, where message is processed. setText (R. string. task_started) ;}@ Override protected void onProgressUpdate (Integer... values) {// Update Progress System. out. println ("" + values [0]); message. setText ("" + values [0]); pdialog. setProgress (values [0]) ;}}

  8. Solution for using the Spinner in dialog and tabhost

 9. Associate JDK source code with eclipse

(1). Click "window"-> "Preferences"-> "Java"-> "Installed JRES"

(2) At this time, the right side of "Installed JRES" is the List Pane, listing the system's JRE environment. Select Your JRE and click "Edit…" on the edge ...", A window (Edit JRE) appears)

(3) select this option for the rt. jar file: "c: \ program files \ java \ jre_1.5.0_06 \ lib \ rt. jar" and expand it by "+" on the left,

(4). After expansion, You can see "Source Attachment :( none)". Click this item and click "Source Attachment…" on the right ...", Select the “src.zip file under your JDK directory

  10. Unable to open sync connection!

Debug and restart the USB in the settings

  11. The problem of setting the cursor position in EditText

When there are some preset texts in EditText, you want to adjust the cursor to the beginning. setSelection (0) is used at the beginning. The result shows that there is a problem on the Samsung P1000. After research, it is found that EditText. requestFocus () needs to be called before setSelection (0 ). Otherwise, there is a problem on the 2.x machine, but it is good on the 3.x machine.

  12. in Android, the Home Key is retained by the system and cannot be used with onKeyDown like the listener rollback key. However, you can add your own processing code based on the activity and view events triggered when you press the home key. someone on the Internet said they could use onAttachWindow to intercept the Home key. I have never tried it.

  13. When using surfaceView for rendering, if you want to show other views as needed, you can place surfaceView and other views in layout. In normal cases, you can hide other views.

  14. You can use android: imeOptinos to set some interface settings for the soft keyboard that comes with Android:

Android: imeOptions = "flagNoExtractUi" // disable full screen display on the keyboard. This attribute only occupies part of the screen and controls the display content of buttons in the lower right corner of the keyboard. By default, the Return key is android: imeOptions = "actionNone" // the right side of the input box does not contain any prompts android: imeOptions = "actionGo" // the content of the buttons in the lower right corner is 'start' android: imeOptions = "actionSearch" // the button in the lower right corner is a magnifier image, and the search android: imeOptions = "actionSend" // the content in the lower right corner is 'send' android: imeOptions = "actionNext" // the content of the buttons in the lower right corner is 'Next' android: imeOptions = "actionDone" // the content of the buttons in the lower right corner is 'complete'

  15. Add a shadow for 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 Chinese in TextView to bold?

In the xml file, you can use android: textStyle = "bold" to set English to bold, but you cannot set Chinese to bold. The method to set Chinese to bold is:

TextView tv = (TextView)findViewById(R.id.TextView01); TextPaint tp = tv.getPaint(); tp.setFakeBoldText(true);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.