我是Android新手,并尝试使用一些时间计数器在几秒钟后自动关闭我的膨胀弹出窗口。 我不知道如何计算计时器(5秒)。
LayoutInflater inflater = (LayoutInflater)screen.getSystemService(screen.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.log_viewer,null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button btn0= (Button)layout.findViewById(R.id.btn0);
Button btn1= (Button)layout.findViewById(R.id.btn1);
Button btn2= (Button)layout.findViewById(R.id.btn2);
btn0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i =new Intent(Main_Activity.this,Act.class);
startActivity(i);
overridePendingTransition(R.anim.animation,R.anim.animation2);
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i =new Intent(Main_Activity.this,Activity2.class);
startActivity(i);
overridePendingTransition(R.anim.animation,R.anim.animation2);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i =new Intent(Main_Activity.this,Activity1.class);
startActivity(i);
overridePendingTransition(R.anim.animation,R.anim.animation2);
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
答案 0 :(得分:2)
你可以像 -
那样关闭它new Handler().postDelayed(new Runnable(){
public void run() {
popup.dismiss();
}
}, 5 *1000);
答案 1 :(得分:0)
public void destroy(){
if (popup != null) {
popup.dismiss();
popup = null;
}
}
并在处理程序中:
new Handler().postDelayed(new Runnable(){
public void run() {
if (popup != null) {
popup.dismiss();
popup = null;
}
}}, 5 *1000);
希望对你有所帮助。