公共的继承文件--android

1.BaseActivity文件


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.app.SkinAppCompatDelegateImpl;

import com.example.zixunapp.MainActivity;

/**
 *基类    公众的方法
 *
 * abstract  抽象类
 **/
public abstract class BaseActivity extends AppCompatActivity {
    private Context mcontext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mcontext=this;
        setContentView(initLayout());
        initView();
        initData();
    }

    protected abstract int initLayout();
    protected abstract void initView();
    protected  abstract void initData();

    public  void ShowToast(String msg){
        Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show();
    }
    public  void ShowToastSync(String msg){
        Looper.prepare();//消息队列自学
        Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show();
        Looper.loop();
    }
    public void navigateTo(Class cls){
        Intent i=new Intent(this, cls);
        startActivity(i);
    }
    protected void saveStringToSp(String key,String val){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(key,val);
        editor.commit();
    }


    @NonNull
    @Override
    public AppCompatDelegate getDelegate() {
        return SkinAppCompatDelegateImpl.get(this, this);
    }

}

2.

BaseFragment文件

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zixunapp.adapter.listener.OnItemChildClickListener;

import xyz.doikki.videoplayer.player.VideoViewManager;

public abstract class BaseFragment extends Fragment implements OnItemChildClickListener {

    public View mRootView;

    //onCreateView是创建该fragment对应的视图,必须在这里创建自己的视图并返回给调用者。
    //onViewCreated在onCreateView执行完后立即执行.onCreateView返回的就是fragment要显示的view.
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //如果mRootView为空  创建mRootView,不为空  直接返回  复用mRootView
        if(mRootView==null){
            mRootView=inflater.inflate(initLayout(),container,false);
            initView();//初始化view
        }
        return mRootView;
    }

    //onViewCreated在onCreateView执行完后立即执抄行.onCreateView返回的就袭是fragment要显示的view.
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    protected abstract int initLayout();
    protected abstract void initView();
    protected  abstract void initData();


    public  void ShowToast(String msg){
        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }
    public  void ShowToastSync(String msg){
        Looper.prepare();//消息队列自学
        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
        Looper.loop();
    }
    public void navigateTo(Class cls){
        Intent i=new Intent(getActivity(), cls);
        startActivity(i);
    }
    public void navigateToWithBundle(Class cls, Bundle bundle) {
        Intent in = new Intent(getActivity(), cls);
        in.putExtras(bundle);
        startActivity(in);
    }

    public void navigateToWithFlag(Class cls, int flags) {
        Intent in = new Intent(getActivity(), cls);
        in.setFlags(flags);
        startActivity(in);
    }

//    插入键
    protected void saveStringToSp(String key,String val){
        //        文件名  userInfo
        SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString(key,val);
        editor.commit();
    }
//    查询键
    protected String getStringFromSp(String key){
//        文件名  userInfo
        SharedPreferences sharedPreferences=getActivity().getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        String value=sharedPreferences.getString(key,"-1");
        return value;
    }
//    删除键
    protected void removeStringToSp(String key){
        //        文件名  userInfo
        SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.remove(key);
        editor.commit();
    }

    //视频列表用
    /**
     * 子类可通过此方法直接拿到VideoViewManager
     */
    protected VideoViewManager getVideoViewManager() {
        return VideoViewManager.instance();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值