所以有这个游戏我试图创建,并在课程活动我有一个很好的风景背景。我希望玩家在我的教程弹出xml膨胀之前看到这一秒或2秒。事情是透明弹出窗口xml和背景xml都会在活动启动后立即出现。我是新手,并且无法理解android开发者主页中的解释。任何帮助将不胜感激。
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();
}
}
答案 0 :(得分:2)
试试这个:
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);
}
}
处理程序是android中计时器的一个很好的替代品。
您之前所做的是在onCreate中创建后台线程,尝试从那里访问UI线程。根据我的经验,它应该崩溃,因为你无法从后台线程访问UI线程。
答案 1 :(得分:0)
你不能改变UI元素和非UI线程,所以这段代码不起作用。
1)显示空白背景。
2)执行Async任务
DoinBackground()
{
wait
}
onPostExecute()
{
addView
}
(如果任何术语未知,请参阅Google。)