基类Activity、Fragment、Dialog以及LogUtils、CursorUtils的写法(方便开发)

基类Activity

子类只要重写抽象方法就行了

public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        initData();

    }

    public abstract void initView();

    public abstract void initListener();

    public abstract void initData();

    public abstract void processOnlick(View view);
    @Override
    public void onClick(View v) {
        processOnlick(v);
    }
}

基类Fragment的写法

public abstract class BaseFragment extends android.support.v4.app.Fragment implements View.OnClickListener{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListener();
        initData();

    }

    public abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    public abstract void initListener();

    public abstract void initData();

    public abstract void processListener(View view);

    @Override
    public void onClick(View v) {
        processListener(v);
    }
}

基类Dialog

public abstract class BaseDialog extends AlertDialog implements View.OnClickListener {
    protected BaseDialog(Context context) {
        super(context, R.style.BaseDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initDate();
    }

    public abstract void initView();

    public abstract void initListener();

    public abstract void initDate();

    public abstract void processListener(View view);

    @Override
    public void onClick(View v) {
        processListener(v);
    }
}

LogUtils

public class LogUtils {
    //可以控制是否打印
    public static boolean isDebug = true;
    public static void i(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }
    public static void i(Object tag, String msg) {
        if (isDebug) {
            Log.i(tag.getClass().getSimpleName(), msg);
        }
    }
    public static void e(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }
}

CursorUtils

//打印cursor
public class CursorUtils {
    public static void printCursor(Cursor cursor) {
        LogUtils.i(cursor,"一共有" + cursor.getColumnCount() + "条数据");
        while (cursor.moveToNext()) {
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                String name = cursor.getColumnName(i);
                String content = cursor.getString(i);
                LogUtils.i(cursor, "名字 " + name + " " + "内容 " + content);
            }
            LogUtils.i(cursor, "=======================");
        }
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guanhang89/article/details/51554260
个人分类: android
上一篇安卓小案例收集五(内容提供者、动画)
下一篇ViewPager和Fragment的使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭