1.布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.shengquanjituan.aboutdemo.activity.WelcomeActivity"> <TextView android:id="@+id/Welcome_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:text="欢迎页" android:textColor="#fff000" android:textSize="20sp" android:gravity="center"/> </RelativeLayout>
2.代码:
public class WelcomeActivity extends AppCompatActivity { private int time=3; final Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case 1: time--; Log.e("TAG",time+""); if (time>0){ tv.setText(time+"s"); handler.sendMessageDelayed(handler.obtainMessage(1),1000); } // else { // //启动页面 // startMainActivity(); // } break; } super.handleMessage(msg); } }; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); tv= (TextView) findViewById(R.id.Welcome_tv); //每隔一秒发送消息 handler.sendMessageDelayed(handler.obtainMessage(1), 1000); //延迟3秒后进入主界面 handler.postDelayed(new Runnable() { @Override public void run() { //执行在主线程 //启动页面 startMainActivity(); } },3000); } private void startMainActivity() { startActivity(new Intent(WelcomeActivity.this,MainActivity.class)); //关闭当前页面 finish(); } @Override public boolean onTouchEvent(MotionEvent event) { //点击快速进入主界面 if (event.getAction()==MotionEvent.ACTION_DOWN){ startMainActivity(); return true; } return super.onTouchEvent(event); } @Override protected void onDestroy() { super.onDestroy(); //移除消息 handler.removeCallbacksAndMessages(null); } }