How to get started with Android Development (3): how to get started with android Development

Source: Internet
Author: User

How to get started with Android Development (3): how to get started with android Development
Preface:

Honestly, I don't know what to talk about. I hope people who have read this article can give me comments. I am very grateful.

Current knowledge point:

Long press event, simple use of log, double-click to exit program function implementation

Body:

In the previous issue, we learned that the click event is actually to bind the control to a listener, and then write related processing code in the listener, such as the Toast pop-up window. The long-press event is similar to the click event, the process is almost the same.

The problem is that the listener settings are the same. Can the five listener setting methods mentioned in the click event be implemented in the long-press event? The answer is yes. limited by time, I will not test it one by one. With the previous click event, I directly implemented an onlongclick interface, and then bound the listener to the control to rewrite the methods in the listener.

Effect:

 

The above picture shows our results very well. I don't know if you have noticed it. The Toast pop-up window we use is used for inspection and testing, here, we have less code and Toast is not a problem. However, in the future, we may write a lot of code, but it is also very complicated. A bug occurs during testing, is it a headache if we use Toast to check? So next I will give you a brief introduction to the use of Log.

The use of Log is actually very simple. We only need to press Log, and then press the tab key to complete the code like Toast. My computer does not know what's going on recently, and pressing the tab key cannot complete the code, however, your computer should be okay.

I added the code in the image, Log. d receives two String parameters. The first parameter is the Tag and the second parameter is the content. Generally, we can declare a static String constant in the current activity, when you call the Log, the Tag directly uses the constant, as shown in the following example.

Private static final TAG = "MainActivity ";

 

How can I check it? Open Android Monitor, and a panel will appear. Then, we will perform related operations in the simulator, and the message will be displayed when we return and view it.

 

In many apps, we have the function of double-clicking and exiting the program. You should be interested in this function, so let's explain it.

There is a method named onBackPressed in the Activity. For example, the method can be called when we press the return key.

The idea of implementation: When you press the return key, a Toast is displayed, prompting the user to press it again to exit the program. If the user presses the return key again within two seconds, the user will exit the program, otherwise, do not exit the program.

Here we also need to add System. exit (0) in onDestroy );

Indicates that the system exits completely. If no program is added, an error is returned.

Effect:

 

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.