记录一下Kotiln实现闪屏界面

记录一下Kotiln实现闪屏界面

这个是kotiln代码
记录一下Kotiln实现闪屏界面_第1张图片

class Splash : AppCompatActivity() {

    lateinit var timer: Timer;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        initData()
    }

    private fun initData() {
        val type: String = Sp(this).getString("Splash")
        if (type == "false") {
            val intent = Intent()
            intent.setClass(applicationContext, LoginActivity().javaClass)
            startActivity(intent)
            finish()
        } else {

            timer = Timer();
            timer.schedule(timerTask {
                Looper.prepare()
                val intent = Intent()
                intent.setClass(applicationContext, LoginActivity().javaClass)
                startActivity(intent)
                finish();
                Looper.loop()
            }, 3000);


            val jump: Button = findViewById(R.id.btn_jump)
            jump.setOnClickListener {
                val intent = Intent()
                intent.setClass(this, LoginActivity().javaClass)
                startActivity(intent)
                finish()
                timer.cancel()
            }
        }
    }
}

方法其实和Java一样,也可以直接把Java的代码占进来。

这是Java代码
记录一下Kotiln实现闪屏界面_第2张图片

public class SplashActivity extends ActivityBase {
    private Button btnJump;
    private Timer timer;

    @Override
    protected int GetLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void findView() {
        btnJump = findViewById(R.id.btn_jump);
    }

    @Override
    protected void initData() {
        Sp sp = new Sp(this);
        timer = new Timer();
        Log.e("打印闪屏值", String.valueOf(sp.getString("Splash")));
        if (sp.getString("Splash").equals("false")) {
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        } else {

            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                    timer.cancel();
                }
            }, 3000);

        }

    }

    @Override
    protected void setListener() {
        btnJump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
                timer.cancel();
            }
        });
    }
}

最后附上代码

这是base代码

public abstract class ActivityBase extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(GetLayout());
        findView();
        initData();
        setListener();
    }

    protected abstract int GetLayout();

    protected abstract void findView();

    protected abstract void initData();

    protected abstract void setListener();


}

布局的话就只有一个按钮这个自己写一下就好。

你可能感兴趣的:(kotiln,kotlin,android)