记录:对话框弹出自动关闭:

public class MainActivity extends AppCompatActivity {
 
    private Button dialog1,dialog2;
    private AlertDialog alert;
    private Timer timerl;
    private MyHandler handler;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.activity_main);
         
        timerl=new Timer();
        handler=new MyHandler();
         
        dialog1=(Button)findViewById(R.id.dialog1);
        dialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAlertDialog();
            }
        });
 
 
    }
 
    private void showAlertDialog(){
        AlertDialog.Builder builder=new AlertDialog(this);
        builder.setTitle("confirm!");
        builder.setMessage("welcome to use~");
        builder.setCancelable(false);
        alert=builder.create();
        alert.show();
 
        timerl.schedule(new CloseDialogTask(),4000);
    }
     
    private class CloseDialogTask extends TimerTask
    {
     private void run(){
         handler.sendEmptyMessage(0);
     }   
    }
     
    private class MyHandler extends Handler{
        public void handleMessage(Message msg){
            alert.dismiss();
        }
    }

                                                                                                                                                                       -END