New UI-use the keepScreenOn attribute of view to keep the screen always on, ui-keepscreenon

Source: Internet
Author: User

New UI-use the keepScreenOn attribute of view to keep the screen always on, ui-keepscreenon

New UI-set visible and invisible View

-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!


The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.

After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.

For more details, help more people, O (∩ _ ∩) O thank you!

Piggy Android Development Exchange Group: Piggy Android Development Exchange Group No.: 421858269

New Android UI instance Daquan Directory: http://blog.csdn.net/coder_pig/article/details/42145907



1) This section introduces:

In fact, I have already said this in my internship notes, and I have introduced three methods to keep the screen always bright,

In this example, partial view. keepScreenOn is used to demonstrate simple usage!


2) usage:

It is very simple to use the keepScreenOn attribute.

Step 1: Set android: keepScreenOn = "true" to a UI component in the layout file corresponding to the Activity that needs to be normally highlighted"

Then, as long as the Activity is not stopped, the screen will always be on!

Step 2: how to disable the always-on mode? It's easy ~ Modify the attributes of the UI component and set it to false when you need to disable the usual brightness.



Paste the following simple code:

Activity_main.xml:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <Button android: id =" @ + id/btnclose "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "close always bright"/> <TextView android: id = "@ + id/txtliang" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: keepScreenOn = "true" android: text = "I am so bright"/> </LinearLayout>


MainActvitiy. java

Package com. jay. example. keepscreenondemo; import android. OS. bundle; import android. app. activity; import android. view. view; import android. view. view. onClickListener; import android. view. windowManager; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private Button btnclose; private TextView txtliang; private int flag =-1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnclose = (Button) findViewById (R. id. btnclose); txtliang = (TextView) findViewById(R.id.txt liang); btnclose. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (flag =-1) {txtliang. setKeepScreenOn (false); txtliang. setText ("I cannot light up =-="); btnclose. setText ("bright");} else {txtliang. setKeepScreenOn (true); txtliang. setText ("I highlighted again ^-^"); btnclose. setText ("not bright ");}}});}}

Ps: As for the running effect, you don't need to post it. Set the phone backlight time to the minimum, the author's time is 15 s, and then run the demo.

Let the Activity of the component with this attribute be located at the beginning of the screen, and put it aside. After 15 s of no operation,

Check whether the phone screen is dimmed!




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.