栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

公共的继承文件--android

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

公共的继承文件--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;


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();
    }

    //视频列表用
    
    protected VideoViewManager getVideoViewManager() {
        return VideoViewManager.instance();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489980.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号