Some common tips for Android development (reproduced)

Source: Internet
Author: User

Http://www.jb51.net/article/61135.htm

activity.startactivities () is often used to start other Activity in the middle of an application.

textutils.isempty () simple tool class to detect whether it is empty

html.fromhtml () is used to generate an Html parameter, which can be a string. Personally think it's not very fast, so I don't use it very often. (I say it is not often used to highlight this sentence: Please build spannable manually to replace html.fromhtml), but it's good for rendering text that gets from the Web.

Textview.seterror () is great when validating user input

Build.version_codes This indicates the current version number, which is often used when dealing with compatibility issues. Click to see the different features of each version

log.getstacktracestring () Convenient log class tools, methods LOG.V (), LOG.D (), LOG.I (), LOG.W (), and LOG.E () all print information to Logcat, Sometimes it is necessary to insert the error information into the database or a custom log file, then it is necessary to return the error message as a string, that is, when using the static string getstacktracestring (Throwable tr) method .

Layoutinflater.from () as the name implies, is used to inflate a layout, the parameter is the ID of layout. This person who often writes adapter will use more.

viewconfiguration.getscaledtouchslop () uses the values provided in viewconfiguration to ensure that all touch interactions are uniform. The value obtained by this method indicates that the user's hand is sliding at this distance before it is determined to be slipping. Of course, this value can also be determined by itself. But for consistency, it's better to use standard values.

Phonenumberutils.convertkeypadletterstodigits as the name implies. Converts letters to numbers, similar to T9 input methods,

Context.getcachedir () Gets the path to the cached data folder, very simple but not many people know, this path is usually on the SD card (here the SD card refers to the generalized SD card, including external storage and internal storage) adnroid/data/ Your application package name/cache/below. When testing, you can go to see if the cache is successful. The advantage of caching here is that you do not have to manually create the folder yourself, don't worry about the user to delete the folder they created, and when the application unloads, it will be emptied. When using a third-party cleanup tool, it will also be emptied.

argbevaluator The gradient used to process the color. As Chris Banes says, this class does a lot of auto-boxing, so it's best to get rid of its logic and implement it yourself. This is useless, unclear why, back to add.

Contextthemewrapper makes it easy to modify the theme when running.

Space Space is a new control in Android 4.0 that can actually be used to separate different controls, which form a blank area. This is a lightweight view component that skips draw and is great for any scene that requires a placeholder.

Valueanimator.reverse () This method can smoothly cancel a running animation. I like it very much.

Dateutils.formatdatetime () is used for locale formatting, output formatting and localization of time or date.

alarmmanager.setinexactrepeating saves power by grouping alarms, which is a good option even if you only call an alarm (you can make sure that you automatically call Alarmmanager.cancel when you're finished using )。 The original said the more abstract, here in detail: Setinexactrepeating refers to the setting of non-accurate alarm, use: alarmmanager.setinexactrepeating (ALARMMANAGER.RTC, Starttime,intervall, Pendingintent), a non-accurate alarm clock can only guarantee approximate time interval, but not necessarily accurate, may appear to set the interval is 30 minutes, but actually one interval 20 minutes, another interval 40 minutes. The biggest advantage is that it can be combined with alarm events, such as interval settings every 30 minutes, do not wake up sleep, 8 hours after hibernation has accumulated 16 alarm events, and when the phone is awakened, non-punctual alarm can combine 16 events into one, so it seems that the non-punctual alarm is generally more energy-saving.

formatter.formatfilesize () a regionally formatted file size tool. In layman's terms, the size is converted to a string such as mb,g,kb.

actionbar.hide ()/.show ( ) as the name implies, hides and displays ActionBar, which can be gracefully converted between fullscreen and with ActionBar.

linkify.addlinks () adds a link to the text. Very practical.

Staticlayout is useful when rendering text in a custom View.

activity.onbackpressed () is a convenient way to manage the back key, sometimes you need to control the event of the return key, you can rewrite it. For example, add "point two down" back button to exit the function.

gesturedetector is used to monitor and correspond to the corresponding gesture events, such as Click, long press, slow sliding, fast sliding, easy to use, more convenient than your own implementation.

Drawfilter allows you to manipulate the canvas without invoking the Ondrew method, and you can set a drawfilterwhen creating a custom view, giving all the view in the parent view Sets the anti-alias.

Activitymanager.getmemoryclass () tells you how much memory your machine has and is useful when calculating the cache size.

Viewstub It is an initialization View that doesn't do anything, but can then load a layout file. This is a good place to make placeholders in a slow-load View. The only drawback is that tags aren't supported, so if you're not careful, you might want to include unwanted nesting in the view structure.

systemclock.sleep () This method is very convenient to ensure a certain amount of time sleep, usually I used to debug and analog network delay.

displaymetrics.density This method you can get the device pixel density, most of the time it is best to let the system to automatically zoom resources and other operations, but sometimes the effect of control is better. (especially when customizing the view).

pair.create () facilitates the method of building classes and constructors.

Some common tips for Android development (reproduced)

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.