新建一个基类,就是父类,每个Activity都继承他,也可以不写,主要是为了各自方法的调用
public abstract class Base extends AppCompatActivity {
private TextView mTitle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
super.setContentView(R.layout.title);
setContentView(getLayoutId());
view();
data(); //数据
setListaner();
}
//全屏设置
protected void init() {
}
//设置监听
protected void setListaner(){
}
//初始化数据,适配器
protected abstract void data();
//初始化视图
protected abstract void view();
protected abstract int getLayoutId();
//启动开始的ACtivity的界面
protected void openActivity(Class<? extends Base>cls, boolean isFinish){
startActivity(new Intent(this,cls));
if (isFinish)finish();
}
//T:泛值 ,将视图与布局的ID结合起来
protected <T extends View>T getViewById(@IdRes int viewId){
return (T) findViewById(viewId);
}
}
新建个App类,用于承接上下文
public class App extends Application {
public static App appcontext;
@Override
public void onCreate() {
super.onCreate();
appcontext=this;
}
}
闪屏的界面,相当于主界面
public class ShanActivity extends Base {
private Timer timer;
private TextView mTVtimer;
private int time=6;
@Override //数据,主要是上面数字的变化
protected void data() {
//计时器
timer = new Timer(false);
timer.schedule(new TimerTask() {
@Override
public void run() {
//变化的text可写为%1$s跳转
//字time<string name="shan_tv_timer">%1$s跳转</string>
//%1$ 代表一个可变化的字 中间的数字代表第几位的的字变化
ShanActivity.this.runOnUiThread(new TimerTask() {
@Override
public void run() {
String timeStr=getString(R.string.shan_tv_timer);
//()中timeStr代表样式,time是代表变化的数字,
timeStr= String.format(timeStr,--time);
mTVtimer.setText(timeStr);
if (time==0){
openActivity(HomeActivity.class,true);
timer.cancel();//取消定时任务
}
}
});
}
},0,1000);
}
@Override //视图
protected void view() {
mTVtimer = getViewById(R.id.tv_shan_time);
}
@Override //关联布局的
protected int getLayoutId() {
return R.layout.activity_shan;
}
@Override
protected void setListaner() {
//直接点击跳转
mTVtimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
openActivity(HomeActivity.class,true);
}
});
}
}
闪屏界面的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/logo_1"
tools:context="com.bf.com.myapplication.Activity.ShanActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安全卫士"
android:textSize="50dp"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/tv_shan_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shan_tv_timer"
android:textSize="30dp"
android:background="#5ddded"
android:layout_alignParentRight="true"
android:layout_margin="10dp"/>
</RelativeLayout>