一:先写main布局 <?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" android:orientation="vertical" tools:context="com.bw.lenovo.myapp_jingdong.MainActivity"> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/jd" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/imageView1" android:layout_alignTop="@+id/imageView1" android:layout_marginRight="18dp" android:layout_marginTop="29dp" android:textSize="20sp"/> </RelativeLayout> 二:MainActivity代码操作public class MainActivity extends AppCompatActivity { // SharedPreferences就进行一次,第二次直接跳转 TextView tv; SharedPreferences preferences; SharedPreferences.Editor editor; Handler handler = new Handler(); // 设置跳转时长为3秒 int time = 3; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置不显示标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //根据id找到控件 tv = (TextView) findViewById(R.id.tv); imageView = (ImageView) findViewById(R.id.imageView1); //设置图片缩放 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); //使用sharedPreference preferences = getSharedPreferences("cof", MODE_PRIVATE); //sharedPreference的编辑状态 final SharedPreferences.Editor editor = preferences.edit(); boolean login = preferences.getBoolean("isLogin", false); //判断是第一次登陆就跳转 if (login) { Intent intent = new Intent(MainActivity.this, HomeActivity.class); startActivity(intent); //关闭 finish(); return; } //handler发送消息 handler.postDelayed(new Runnable() { @Override public void run() { //文字显示3秒跳转 tv.setText(time+"秒跳转"); time--; if (time==0){ Intent intent=new Intent(MainActivity.this,HomeActivity.class); startActivity(intent); //进行判断是否是第一次 editor.putBoolean("isLogin",true); //提交 editor.commit(); finish(); return; } handler.postDelayed(this,1000); } }, 1000); } }
Handler实现3秒跳转==欢迎界面
最新推荐文章于 2019-08-12 16:33:45 发布