Some common tips for Android development

  • 2020-06-15 10:09:45
  • OfStack

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

TextUtils.isEmpty () simple utility class for detecting null

Html.fromHtml () is used to generate 1 Html with 1 string argument. I don't think it's very fast, so I don't use it very often. (I say not often to highlight this sentence: please build Spannable more manually instead of Html.fromHtml), but it's good for rendering text taken from web.

TextView. setError() is great at validating user input

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

Log.getStackTraceString () is a convenient logging tool. Methods Log.v (), Log.d (), Log.i (), Log.w () and Log.e () all print the information into LogCat. This is when using the static String getStackTraceString(Throwable tr) method.

LayoutInflater.from(), as the name implies, is used for Inflate1 layout and the parameter is id of layout.

ViewConfiguration.getScaledTouchSlop() USES the values provided in ViewConfiguration to ensure that all touch interactions are unified. The value obtained by this method means that the user is judged to be sliding only after he slides this distance. Of course, this value can be determined by himself. However, for the sake of 1, it is better to use the standard value.

PhoneNumberUtils.convertKeypadLettersToDigits as the name implies, converts letters to Numbers, similar to the T9 input method,

Context.getCacheDir () gets the path to the cached data folder, which is simple but not well known and is usually on an SD card (here SD refers to the broader SD card, both external and internal)Adnroid/data/ your application package name /cache/ . Under the test of time, you can go there to see whether the cache success. Cached in the benefit here is: don't go to manually create the folder, don't worry about the user to create your own folder deleted, at the time of application uninstall, here will be cleared, use third party cleaning tools, here will be empty.

ArgbEvaluator is used for color gradients. As Chris Banes said, this class does a lot of autoboxing, so it's best to remove the logic and implement it yourself. This has not been used, do not know why, go back to add.

ContextThemeWrapper makes it easy to change themes at run time.

Space space is a new addition to Android 4.0 that can actually be used to separate different controls into a blank area. This is a lightweight view component that can skip Draw, great for any scenario that requires placeholders.

ValueAnimator.reverse() this method cancels running animations very easily. I love it.

DateUtils. formatDateTime() is used to format the region and output the time or date of formatting and localization.

Save power by grouping alarms, which is a good option even if you only call one alarm (make sure to call AlarmManager.cancel () automatically when you're done. setInexactRepeating refers to the setting of an inaccurate alarm clock (ES104en. setInexactRepeating(AlarmManager, startTime, intervalL, pendingIntent). The inaccurate alarm clock can only guarantee the approximate time interval, but it is not accurate. Its biggest advantage is can merge the alarm events, such as interval set once every 30 minutes, don't wake up dormant, after 8 hours sleep have amassed 16 alarm events, and in the mobile phone is awakened, the alarm clock can combine 16 events on time for 1, so so, not on time alarm 1 kind is to save energy.

Formatter.formatFileSize() 1 localized file size formatting tool. In general, this translates the size to MB, G, KB, etc.

ActionBar. hide()/.show () as the name implies, hides and shows ActionBar, gracefully switching between full-screen and with Actionbar.

Linkify. addLinks() adds a link to Text. To be very practical.

StaticLayout is useful for rendering text in custom View.

Activity. onBackPressed() is a very convenient way to manage the back key. Sometimes you need to control the event of the return key by yourself, you can override 1. For example, add the "click back key twice to exit" function.

GestureDetector is used for listening and corresponding gesture events, such as click, long press, slow slide, fast slide. It is easy to use and much more convenient than your own implementation.

DrawFilter allows you to manipulate canvas without calling onDrew. For example, you can set one DrawFilter when creating custom View and set the anti-alias for all View in the parent View.

ActivityManager. getMemoryClass() tells you how much memory your machine has left, which is useful when calculating the cache size.

ViewStub is an View that initializes and does nothing, but can then load a layout file. A good placeholder for slow loading View. The downside of only 1 is that it doesn't support tags, so if you're not careful, you might add unwanted nesting to the view structure.

This method is very convenient for ensuring 1 fixed sleep, which I usually use for debug and analog network latency.

DisplayMetrics. density allows you to get the pixel density of the device, and most of the time it's better to let the system do things like scale automatically, but sometimes the control is better (especially if you're customizing View).

Pair. create() is a convenient way to build classes and constructors.


Related articles: