黑马52期学后总结笔记(一)

本文深入探讨了Android自定义控件的属性定义与布局实现,同时对比了Activity的几种上下文使用方式及其应用场景。通过实例解析,帮助开发者掌握自定义控件的构造与属性设置,以及Activity上下文的生命周期与正确使用方法。

1.Activity.this与getApplicationContext()的区别:

对话框是Activity的一部分。

 对话框是挂载在Activity上面的 。

 如果Activity不存在,对话框就不能被创建。

 Activity 实际上是应用程序context上下文的一个子集。

 

 子类有的东西父类不一定有

 父类有的东西子类一定有


 getApplicationContext();生命周期长,只要应用还存活它就存在;

 this 生命周期短,只要Activity不存在了,系统就会回收; 

 

  其中:getBaseContext(),getApplication(),getApplicationContext();

  都不能放在AlertDialog做上下文;

 getApplicationContext() 使用场景是比如频繁需要操作的数据库

  推荐用法:Activity.this


2.定义自定义控件的属性:

 1、声明一个View对象 继承相对布局,或者线性布局或者其他的ViewGroup。

 2、在自定义的View对象里面重写它的构造方法。在构造方法里面就把布局都初始化完毕。

 3、根据业务需求 添加一些api方法,扩展自定义的组合控件;

 

 

 4、希望在布局文件里面 可以自定义一些属性。

 5、声明自定义属性的命名空间。

         xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"

 

 6、在res目录下的values目录下创建attrs.xml的文件 声明你写的属性。

    <declare-styleable name="SettingItemView">

       <attr name="title" format="string" />

       <attr name="desc_on" format="string" />

       <attr name="desc_off" format="string" />

</declare-styleable>

 

7、在布局文件中写哪些你自定义的属性。

8、使用这些定义的属性。自定义View对象的构造方法里面 有一个带两个参数的构造方法

   布局文件里面定义的属性都放在 AttributeSet attrs

   获取那些定义的属性。


SettingItemView.java

package com.konka.tokgo.mobilesafe.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.konka.tokgo.mobilesafe.R;
/**
 * Created by Tokgo on 2015-12-11.
 */
public class SettingItemView extends RelativeLayout {

    private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.konka.tokgo.mobilesafe";

    TextView mTvTitle;
    TextView mTvDesc;
    CheckBox mCbStatus;

    String mTitle;
    String mDescOn;
    String mDescOff;

    public SettingItemView(Context context) {
        super(context);
        initView();
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

        //根据属性名称获取属性的值
        mTitle = attrs.getAttributeValue(NAMESPACE, "titles");
        mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on");
        mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off");
        initView();
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        //将自定义的布局文件设置给SettingItemView
        View.inflate(getContext(), R.layout.view_setting_item, this);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvDesc = (TextView) findViewById(R.id.tv_desc);
        mCbStatus = (CheckBox) findViewById(R.id.cb_status);
        setTitle(mTitle);
    }

    /**
     * 设置标题
     * */
    public void setTitle(String title) {
        mTvTitle.setText(title);
    }


    public void setDesc(String desc) {
        mTvDesc.setText(desc);
    }

    /**
     * 返回勾选状态
     * */
    public boolean isChecked() {
        return mCbStatus.isChecked();
    }


    /**
     * 设置勾选状态
     * */
    public void setChecked(boolean check) {
        mCbStatus.setChecked(check);
        if(check) {
            setDesc(mDescOn);
        } else {
            setDesc(mDescOff);
        }
        Log.d("TAG",mDescOff+mDescOn);
    }
}

<span style="font-size:18px;">view_setting_item.xml</span>
<span style="font-size:18px;"></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_below="@+id/textView"
    android:padding="5dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:layout_marginTop="3dp"
        android:textColor="#aa000000"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/cb_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_centerVertical="true" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:background="#aa000000" />

</RelativeLayout>

调用:

<com.konka.tokgo.mobilesafe.view.SettingItemView
    android:id="@+id/siv_update"
    tokgo:titles="自动更新设置"
    tokgo:desc_on="自动更新已开启"
    tokgo:desc_off="自动更新已关闭"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />



考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值