按下android中的按钮后如何启动一个不同的活动?

Pra*_*hur 10 android

我希望新的活动开始时按下按钮会有一些延迟.是否有可能这样做,并且该程序是什么.

mad*_*mad 30

使用postDelayed()调用runnable启动您的活动.示例代码可以是

    //will care for all posts
    Handler mHandler = new Handler();

    //the button's onclick method
    onClick(...)
    {
        mHandler.postDelayed(mLaunchTask,MYDELAYTIME);
    }

    //will launch the activity
    private Runnable mLaunchTask = new Runnable() {
        public void run() {
            Intent i = new Intent(getApplicationContext(),MYACTIVITY.CLASS);
            startActivity(i);
        }
     };
Run Code Online (Sandbox Code Playgroud)

请注意,这使接口保持反应.然后,您应该关注从按钮中删除onclick侦听器.


xil*_*il3 13

您可以使用Handler postDelayed()方法调用Runnable.

这是一个例子(http://developer.android.com/resources/articles/timed-ui-updates.html):

private Handler mHandler = new Handler();

...

OnClickListener mStartListener = new OnClickListener() {
   public void onClick(View v) {
            mHandler.postDelayed(mUpdateTimeTask, 100);
   }
};

private Runnable mUpdateTimeTask = new Runnable() {
   public void run() {
       // do what you need to do here after the delay
   }
};
Run Code Online (Sandbox Code Playgroud)

推荐给@mad,让他们第一次做对.


Mil*_*kla 12

使用此代码

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            final Intent mainIntent = new Intent(CurrentActivity.this, SecondActivity.class);
            LaunchActivity.this.startActivity(mainIntent);
            LaunchActivity.this.finish();
        }
    }, 4000);
Run Code Online (Sandbox Code Playgroud)