3

如何根据时间填充弹出窗口,然后在 android 中 20 秒后隐藏?

这是我的代码:

public class ExPopup extends Activity {

Dialog myDialog;
Button myButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(ExPopup.this);
    helpBuilder.setTitle("Pop Up");
    helpBuilder.setMessage("This is a Simple Pop Up");
    helpBuilder.setPositiveButton("Positive",
            new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // Do nothing but close the dialog
        }
    });
    helpBuilder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do nothing
        }
    });
    helpBuilder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do nothing
        }
    });
    // Remember, create doesn't show the dialog
    final AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();
    Timer tShow = new Timer();
    final Timer tHide = new Timer();

    // This timer will show the popup every 10 second
    tShow.schedule(new TimerTask(){

         public void run(){
              helpDialog.show();
              Date d = new Date(0);
              d.setSeconds(d.getSeconds()+20);

               //This will hide the popup after 20 secnds
               tHide.schedule(new TimerTask(){
                      public void run(){
                          helpDialog.dismiss();
                      }
               },d);
         }
    },0,10*1000);
}   
   }

嘿,我收到此错误并且应用程序已forcibly关闭:

03-27 10:55:21.371: E/AndroidRuntime(523): FATAL EXCEPTION: Timer-0
03-27 10:55:21.371: E/AndroidRuntime(523): java.lang.IllegalArgumentException
03-27 10:55:21.371: E/AndroidRuntime(523):  at      java.sql.Date.getSeconds(Date.java:109)
03-27 10:55:21.371: E/AndroidRuntime(523):  at com.example.android.ExPopup$4.run(ExPopup.java:60)
03-27 10:55:21.371: E/AndroidRuntime(523):  at java.util.Timer$TimerImpl.run(Timer.java:284)
03-27 10:55:24.601: E/WindowManager(523): Activity com.example.android.ExPopup has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051bd00 that was originally added here
03-27 10:55:24.601: E/WindowManager(523): android.view.WindowLeaked: Activity com.example.android.ExPopup has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051bd00 that was originally added here
03-27 10:55:24.601: E/WindowManager(523):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
03-27 10:55:24.601: E/WindowManager(523):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-27 10:55:24.601: E/WindowManager(523):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-27 10:55:24.601: E/WindowManager(523):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.Dialog.show(Dialog.java:241)
03-27 10:55:24.601: E/WindowManager(523):   at com.example.android.ExPopup.onCreate(ExPopup.java:50)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-27 10:55:24.601: E/WindowManager(523):   at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 10:55:24.601: E/WindowManager(523):   at android.os.Looper.loop(Looper.java:123)
03-27 10:55:24.601: E/WindowManager(523):   at android.app.ActivityThread.main(ActivityThread.java:3683)
03-27 10:55:24.601: E/WindowManager(523):   at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:55:24.601: E/WindowManager(523):   at java.lang.reflect.Method.invoke(Method.java:507)
03-27 10:55:24.601: E/WindowManager(523):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-27 10:55:24.601: E/WindowManager(523):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-27 10:55:24.601: E/WindowManager(523):   at dalvik.system.NativeStart.main(Native Method)
03-27 10:55:25.941: I/Process(523): Sending signal. PID: 523 SIG: 9
4

2 回答 2

0

使用处理程序,

Handler handler=new Handler();
handler.postDelayed(new Runnable(){
public void run(){
//ToDo your function
//hide your popup here
 helpDialog.dismiss();
}
},2000);
于 2013-03-27T04:46:34.323 回答
0

您可以创建一个计时器,然后像这样隐藏对话框:

Timer tShow = new Timer();
Timer tHide = new Timer();

// This timer will show the popup every 10 second
tShow.schedule(new TimerTask(){

     public void run(){
          helpDialog.show();
          Date d = new Date();
          d.setSeconds(d.getSeconds()+20);

           //This will hide the popup after 20 secnds
           tHide.schedule(new TimerTask(){
                  public void run(){
                      helpDialog.dismiss();
                  }
           },d);
     }
},0,10*1000);

将 Timers 和 Dialog 声明为类的变量。但是,我不明白,如果你每 10 秒显示一次弹出窗口并每 20 秒隐藏一次,它会如何工作?

编辑

private Timer tShow = new Timer();
private Timer tHide = new Timer();
private AlertDialog helpDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(MainActivity.this);
    helpBuilder.setTitle("Pop Up");
    helpBuilder.setMessage("This is a Simple Pop Up");
    helpBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // Do nothing but close the dialog
        }
    });
    helpBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do nothing
        }
    });
    helpBuilder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do nothing
        }
    });
    helpDialog = helpBuilder.create();


    // This timer will show the popup every 10 second
    tShow.schedule(new TimerTask(){

        @Override
         public void run(){
              abc();
         }
    },0,10*1000);
}


public void abc(){
    this.runOnUiThread(my_Function);
}

private Runnable my_Function = new Runnable() {

    @Override
    public void run() {
        helpDialog.show();
        Date d = new Date();
        d.setSeconds(d.getSeconds()+20);

         //This will hide the popup after 20 secnds
         tHide.schedule(new TimerTask(){
                public void run(){
                    helpDialog.dismiss();
                }
         },d);

    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
于 2013-03-27T04:49:09.200 回答