Handler实现3秒跳转==欢迎界面

该博客介绍了如何在Android应用中使用Handler实现3秒后自动跳转到欢迎界面的功能。通过在MainActivity的onCreate方法中设置Handler,延迟发送Runnable,当时间到达3秒时启动新的Intent跳转至HomeActivity,并使用SharedPreferences保存登录状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:先写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);

    }

}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值