我试图用一些时间计数器在几秒钟后自动关闭我的膨胀弹出窗口。我不知道如何计算计时器(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 :(得分:5)
您是否考虑将Toast
与自定义布局一起使用?在Android上查看Toast documentation
以开发人员网站为例。
否则您可以使用Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// close your dialog
alertDialog.dismiss();
}
}, 10000); // 10,000 ms delay
答案 1 :(得分:2)
像这样扩展你自己的PopupWindow窗口类
public class ToolTipWindow extends PopupWindow implements PopupWindow.OnDismissListener {
private long closeDelayTime;
private Handler mHandler;
private OnDismissListener mOnDismissListener;
public ToolTipWindow(Context context) {
super(context);
super.setOnDismissListener(this);
}
public ToolTipWindow(Context context, long closeDelayTime) {
this(context);
this.closeDelayTime = closeDelayTime;
}
@Override
public void setOnDismissListener(OnDismissListener onDismissListener) {
mOnDismissListener = onDismissListener;
}
//override all show or update method of super class that you use tho call onshow
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
onShow();
super.showAtLocation(parent, gravity, x, y);
}
public void onShow() {
schedule();
}
private void schedule() {
if (closeDelayTime > 0) {
if (mHandler == null) {
mHandler = new Handler();
}
mHandler.removeCallbacks(closeRunnable);
mHandler.postDelayed(closeRunnable, closeDelayTime);
}
}
@Override
public void onDismiss() {
if (mHandler != null) {
mHandler.removeCallbacks(closeRunnable);
}
if (mOnDismissListener != null) {
mOnDismissListener.onDismiss();
}
}
private Runnable closeRunnable = new Runnable() {
@Override
public void run() {
dismiss();
}
};
}
这里我只覆盖showAtLocation方法,如果你使用另一个PopUpWindow方法来显示它,覆盖该方法并在其中调用onShow()来调用自动调度。
代码中的某个地方我调用mHandler.removeCallbacks(closeRunnable),调用它是非常重要的,因为如果寡妇在时间过去之前关闭,它就会避免崩溃。
答案 2 :(得分:0)
如果您想在延迟一段时间后执行某项操作,可以执行以下操作:
final long TOTAL_TIME = 5000; // miliseconds
new Handler().postDelayed(new Runnable() {
public void run() {
// do action here
}
}, TOTAL_TIME);