Android app development often uses knowledge

Source: Internet
Author: User
Tags gety

1. Recently opened apps are not displayed in the recent task list

Android:excludefromrecents= "true"

Set to true to exclude from the recent task list. Does not appear in the recent Tasks list

2. Infer whether a string str is null or is an empty string

Textutils.isempty (str)

3. How to use android:imeoptions= "Actionsearch|flagnofullscreen"

When you do a function that puts edittext into Actionbar as the search box, the EditText property is set to Android:imeoptions= "Actionsearch". You will encounter a problem when you are on a horizontal screen. The width of the edittext will be filled out on the screen except for the soft keyboard, and the requirements do not match, changed to android:imeoptions= "Actionsearch|flagnofullscreen" after the OK.

4. How to change the brightness of the picture

1. Use Image.setcolorfilter (color.gray,porterduff.mode.multiply) to darken the image. Then use Image.clearcolorfilter (); Clear the filter to restore the original brightness;
1. function
int brightness =-80;
ColorMatrix matrix = new ColorMatrix ();
Matrix.set (New float[] {1, 0, 0, 0, brightness, 0, 1, 0, 0,
Brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0});
V.setcolorfilter (new Colormatrixcolorfilter (matrix));
But such a method would make the color less normal. The picture is left with a black edge;

5. Using handler to realize the inference of time interval events

See the Gesturedetector.java in Android is inferred by clicking and double clicking to implement gestures with the following code:

public boolean ontouchevent (motionevent ev) {... case MotionEvent.ACTION_DOWN:if (mdoubletaplistener! =                NULL) {Boolean hadtapmessage = Mhandler.hasmessages (TAP);                if (hadtapmessage) mhandler.removemessages (TAP);                        if (mcurrentdownevent! = null) && (mpreviousupevent! = null) && hadtapmessage &&                    Isconsidereddoubletap (mcurrentdownevent, mpreviousupevent, Ev)) {//This is a second tap                    Misdoubletapping = true; Give a callback with the first tap of the Double-tap handled |= Mdoubletaplistener.ondoubletap (mcurr                    Entdownevent); Give a callback with down event of the Double-tap handled |= mdoubletaplistener.ondoubletapevent (EV)                ; } else {//This is a first tap mhandler.sendemptymessagedelayed (tap, Double_tap_tim Eout);               } }......}        Private Boolean Isconsidereddoubletap (Motionevent firstdown, motionevent firstup, motionevent seconddown) {        if (!malwaysinbiggertapregion) {return false;        } final Long deltatime = Seconddown.geteventtime ()-firstup.geteventtime ();        if (Deltatime > Double_tap_timeout | | deltatime < double_tap_min_time) {return false;        } int deltax = (int) firstdown.getx ()-(int) seconddown.getx ();        int deltay = (int) firstdown.gety ()-(int) seconddown.gety ();    Return (DeltaX * deltax + deltay * DeltaY < Mdoubletapslopsquare);        } private class Gesturehandler extends Handler {Gesturehandler () {super ();        } gesturehandler (Handler Handler) {super (Handler.getlooper ()); } @Override public void Handlemessage (Message msg) {switch (msg.what) {case Show_pre SS:mListener.onShOwpress (mcurrentdownevent);                            Break                Case Long_press:dispatchlongpress ();                            Break Case TAP://If the user's finger is still down, does not count it as a tap If (mdoubletaplist Ener! = null) {if (!mstilldown) {mdoubletaplistener.onsingletapconfirmed (Mcurr                    Entdownevent);                    } else {Mdeferconfirmsingletap = true;            }} break; Default:throw new RuntimeException ("Unknown message" + msg); Never}}

Detailed can refer to the source code, here is the magical mhandler.sendemptymessagedelayed. Let's say Mhandler sent the TAP message within the double_tap_timeout time. Just click on the time, assuming that the time is not sent out, that is, double-click the event.

6, the Setimageresources method of ImageView

ImageView's Setimageresources method not only accepts drawable resources, but also accepts color resources:

Imageview.setimageresource (r.drawable.xx); Imageview.setimageresource (r.color.xx);

7. Android: The Addstatesfromchildren property describes whether the Drawable property of the ViewGroup includes the state of the drawable of its subclasses. The LinearLayout hypothesis does not include this property (false) when the widget is clicked and does not appear The selected state. That is, the state of the child class is not passed to the parent class.
For example, to implement a linearlayout that includes a textview and a edittext, the effect of focusing at the point of the user. Set Android:addstatesfromchildren to True when the EditText or button in the group gets the focus. Set the background of the layout to the drawable of the corresponding edittext or button, so that the view in the linear is a total.


8, Android:descendantfocusability

This property is defined as the relationship between ViewGroup and its child controls when one gets the focus for the view.

There are three types of values for the property:
Beforedescendants:viewgroup takes precedence over its subclass control and gets to focus
Afterdescendants:viewgroup only gets focus when its subclass control does not need to get focus
Blocksdescendants:viewgroup overrides the subclass control and gets the focus directly

9, mobile phone screen control

PowerManager pm = (powermanager) mactivity.getsystemservice (Context.power_service); WakeLock Mwakelock = Pm.newwakelock (Powermanager.full_wake_lock,tag); Mwakelock.acquire ();//Do what you want to do.

。 Mwakelock.release ();


Newwakelock has three different options for the first number of references:
Powermanager.partial_wake_lock
Powermanager.screen_dim_wake_lock
Powermanager.screen_bright_wake_lock

10, Android:windowsoftinputmode

1, when the focus is generated, the soft keyboard is hidden or displayed
2. Whether to reduce the active main form size to make room for the soft keyboard

11, Relativelayout to achieve similar linearlayout layout_weight effect:

    <textview        android:id= "@+id/text1"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:layout_alignparentstart=" true "        android:text=" Test1 "        android:background=" @ Android:color/holo_blue_light "        android:textsize=" 16DP "/>    <textview        android:id=" @+id/text2 "        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:layout_ Alignparentend= "true"        android:layout_toendof= "@id/text1"        android:text= "Test2"        android: Background= "@android: Color/holo_red_light"        android:textsize= "16DP"/></relativelayout>

12, handler.removecallbacksandmessages (NULL)

In using handler, it is necessary to run this code in Ondestory if we no longer have to send messages or run runnable after the activity exits. Failure to do so may result in an error. These handler will not be emitted until after ondestory.




Android app development often uses knowledge

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.