部署一个充气布局的时间延迟 [英] deploying a time delay for an inflate layout

查看:188
本文介绍了部署一个充气布局的时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有这场比赛我尝试创建,并在课活动,我有一个很好的风景背景。我希望玩家看到这对之前我的教程弹出XML膨胀一秒2。的事情是透明弹出xml和背景的xml只要活动被启动同时出现。林新在这个和无法理解的Andr​​oid开发者主页上的解释。任何帮助将大大AP preciated。

 包com.jibushi;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;公共类LessonsShell延伸活动{    私人浏览视图。    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);        的setContentView(R.layout.lessons);        最终的ViewGroup父=(ViewGroup中)findViewById(R.id.lessons_bg);        螺纹splashTread =新主题(){
            @覆盖
            公共无效的run(){
                尝试{
                    等待(1000);
                }赶上(InterruptedException的E){                } {最后
                    视图();
                }
            }            私人无效视图(){
                // TODO自动生成方法存根
                鉴于= LayoutInflater.from(getBaseContext())。膨胀(
                        R.layout.lessons_dialog,NULL);
                parent.addView(视图);
            }
        };
        splashTread.start();
    }
}


解决方案

试试这个:

 公共类LessonsShell延伸活动{
私有静态最终诠释MESSAGE_SHOW_POPUP = 7;
私有静态最后长TIME_DELAY = 3000; //3秒
私人浏览视图。
私人处理程序处理程序=新的处理程序(){
   的handleMessage(消息MSG){
      开关(msg.what){
        案例MESSAGE_SHOW_POPUP:
           视图();
           打破;
       }
   };
};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);的setContentView(R.layout.lessons);
//这将消息发送给处理后3秒以显示弹出。
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);}私人无效视图(){
// TODO自动生成方法存根
 ViewGroup中父=(ViewGroup中)findViewById(R.id.lessons_bg);
 鉴于= LayoutInflater.from(getBaseContext())膨胀(R.layout.lessons_dialog,NULL);
 parent.addView(视图);
}

}

一个处理程序是一个很好的替代品在android系统的定时器。

你是什么previously所做的是创造的onCreate后台线程,试图从那里访问UI线程。从我的经验应该崩溃,因为你不能从后台线程访问UI线程。

So there is this game im trying to create, and in the lessons activity i have a nice scenic background. I want the player to see this for a sec or 2 before my tutorial popup xml inflates. The thing is the transparent popup xml and the background xml both appear as soon as the activity is initiated. Im new at this and cant understand the explanation in android developers homepage. Any help will be much appreciated.

package com.jibushi;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LessonsShell extends Activity {

    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.lessons);

        final ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);

        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    wait(1000);
                } catch (InterruptedException e) {

                } finally {
                    view();
                }
            }

            private void view() {
                // TODO Auto-generated method stub
                view = LayoutInflater.from(getBaseContext()).inflate(
                        R.layout.lessons_dialog, null);
                parent.addView(view);
            }
        };
        splashTread.start();
    }
}

解决方案

try this:

public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP=7;
private static final long TIME_DELAY=3000;//3 seconds
private View view;
private Handler handler=new Handler(){
   handleMessage(Message msg){
      switch(msg.what){
        case MESSAGE_SHOW_POPUP:
           view();
           break;
       }
   };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 3 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);

}

private void view() {
// TODO Auto-generated method stub
 ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
 view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
 parent.addView(view);
}

}

A handler is an nice replacement for a timer in android.

What you were previously doing was to create a background thread in onCreate, trying to access the UI thread from there. From my experience it should crash, since you cannot access the UI thread from a background thread.

这篇关于部署一个充气布局的时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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