Android |循环中的onCreate方法是否可行? [英] Android | loop in onCreate method possible or not?

查看:62
本文介绍了Android |循环中的onCreate方法是否可行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题扩展了该线程.如何在特定的延迟时间后隐藏ActionBar和NavigationBar?

这适用于我的应用程序.但是它只运行一次.如何使其保持执行状态,直到按下一个按钮(后退按钮)

顺便说一句,这是一个PhotoViewer应用程序(一部分),就像您在查看照片时在Android上的Gmail应用程序一样.

Update1:​​实际上,我实际上有一个touchListener来再次显示和隐藏actionBar的actionBar.所以我想要的是当我处于imageView布局中时,actionBar将在3000ms内消失,然后我轻击至show(),然后在3000ms之后它应该再次消失.同样,当您从电子邮件加载图片时,就像Gmail android应用一样

  @Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);//重叠模式setContentView(R.layout.fullscreen_image);ActionBar actionBar = getActionBar();actionBar.setCustomView(R.layout.actionbar_top);actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);//隐藏actionBar的时间延迟处理程序h = new Handler();h.postDelayed(new Runnable(){@Override公共无效run(){//做延迟的工作getActionBar().hide();getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);}},3000);//例如3000毫秒....后退按钮=(Button)findViewById(R.id.btnBack);back.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){onBackPressed();}}); 

解决方案

我认为您应该将计时器线程的创建移到onClick()上,以便每次单击时都启动一个新计时器./p>

This question extends this thread How to hide ActionBar and NavigationBar after a certain delay time?

This works for my application. But it only runs once. How can I make it to keep executing until a button is pressed (back button)

Btw this is a photoViewer app (part of) like Gmail app on android when you view a photo.

Update1: I actually have a touchListener to show() and hide() the actionBar again. So what I want is when I'm in imageView layout, the actionBar will disappear in 3000ms, then I tap to show(), then after 3000ms it should disappear again. Again, just like Gmail android app when you load a picture from email

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); // overlay mode
        setContentView(R.layout.fullscreen_image);

        ActionBar actionBar = getActionBar();               
        actionBar.setCustomView(R.layout.actionbar_top);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);         


        // time delay to hide actionBar
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                // DO DELAYED STUFF
                getActionBar().hide();
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
            }
        }, 3000); // e.g. 3000 milliseconds
....

    Button back = (Button)findViewById(R.id.btnBack);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();                
        }
    });

解决方案

I think you should move the creation of the timer-thread to your onClick(), so that every time it gets click, a new timer is started.

这篇关于Android |循环中的onCreate方法是否可行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆