WebKit之binding案例分析(testMediaQueryListListener.idl)

本文深入探讨了在JavaScript环境中实现媒体查询监听器的细节,并展示了如何将其应用于实际项目中,以响应不同设备和屏幕尺寸的变化。

## testMediaQueryListListener.idl

module test {
    interface TestMediaQueryListListener {
        void method(in MediaQueryListListener listener);
    };
}

## JSTestMediaQueryListListener.h

#ifndef JSTestMediaQueryListListener_h
#define JSTestMediaQueryListListener_h

#include "JSDOMBinding.h"
#include <runtime/JSGlobalObject.h>
#include <runtime/JSObjectWithGlobalObject.h>
#include <runtime/ObjectPrototype.h>

namespace WebCore {

class TestMediaQueryListListener;

class JSTestMediaQueryListListener : public DOMObjectWithGlobalPointer {
    typedef DOMObjectWithGlobalPointer Base;
public:
    JSTestMediaQueryListListener(NonNullPassRefPtr<JSC::Structure>, JSDOMGlobalObject*, PassRefPtr<TestMediaQueryListListener>);
    static JSC::JSObject* createPrototype(JSC::ExecState*, JSC::JSGlobalObject*);
    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);
    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&);
    static const JSC::ClassInfo s_info;

    static PassRefPtr<JSC::Structure> createStructure(JSC::JSGlobalData& globalData, JSC::JSValue prototype)
    {
        return JSC::Structure::create(globalData, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), AnonymousSlotCount, &s_info);
    }

    static JSC::JSValue getConstructor(JSC::ExecState*, JSC::JSGlobalObject*);
    TestMediaQueryListListener* impl() const { return m_impl.get(); }

private:
    RefPtr<TestMediaQueryListListener> m_impl;
protected:
    static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | Base::StructureFlags;
};

JSC::JSValue toJS(JSC::ExecState*, JSDOMGlobalObject*, TestMediaQueryListListener*);
TestMediaQueryListListener* toTestMediaQueryListListener(JSC::JSValue);

class JSTestMediaQueryListListenerPrototype : public JSC::JSObjectWithGlobalObject {
    typedef JSC::JSObjectWithGlobalObject Base;
public:
    static JSC::JSObject* self(JSC::ExecState*, JSC::JSGlobalObject*);
    static const JSC::ClassInfo s_info;
    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);
    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);
    static PassRefPtr<JSC::Structure> createStructure(JSC::JSGlobalData& globalData, JSC::JSValue prototype)
    {
        return JSC::Structure::create(globalData, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), AnonymousSlotCount, &s_info);
    }
    JSTestMediaQueryListListenerPrototype(JSC::JSGlobalObject* globalObject, NonNullPassRefPtr<JSC::Structure> structure) : JSC::JSObjectWithGlobalObject(globalObject, structure) { }
protected:
    static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | Base::StructureFlags;
};

// Functions

JSC::EncodedJSValue JSC_HOST_CALL jsTestMediaQueryListListenerPrototypeFunctionMethod(JSC::ExecState*);
// Attributes

JSC::JSValue jsTestMediaQueryListListenerConstructor(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);

} // namespace WebCore

#endif

## JSTestMediaQueryListListener.cpp

#include "config.h"
#include "JSTestMediaQueryListListener.h"

#include "ExceptionCode.h"
#include "JSDOMBinding.h"
#include "MediaQueryListListener.h"
#include "TestMediaQueryListListener.h"
#include <runtime/Error.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

ASSERT_CLASS_FITS_IN_CELL(JSTestMediaQueryListListener);

/* Hash table */
#if ENABLE(JIT)
#define THUNK_GENERATOR(generator) , generator
#else
#define THUNK_GENERATOR(generator)
#endif

static const HashTableValue JSTestMediaQueryListListenerTableValues[2] =
{
    { "constructor", DontEnum | ReadOnly, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsTestMediaQueryListListenerConstructor), (intptr_t)0 THUNK_GENERATOR(0) },
    { 0, 0, 0, 0 THUNK_GENERATOR(0) }
};

#undef THUNK_GENERATOR
static JSC_CONST_HASHTABLE HashTable JSTestMediaQueryListListenerTable = { 2, 1, JSTestMediaQueryListListenerTableValues, 0 };
/* Hash table for constructor */
#if ENABLE(JIT)
#define THUNK_GENERATOR(generator) , generator
#else
#define THUNK_GENERATOR(generator)
#endif

static const HashTableValue JSTestMediaQueryListListenerConstructorTableValues[1] =
{
    { 0, 0, 0, 0 THUNK_GENERATOR(0) }
};

#undef THUNK_GENERATOR
static JSC_CONST_HASHTABLE HashTable JSTestMediaQueryListListenerConstructorTable = { 1, 0, JSTestMediaQueryListListenerConstructorTableValues, 0 };
class JSTestMediaQueryListListenerConstructor : public DOMConstructorObject {
public:
    JSTestMediaQueryListListenerConstructor(JSC::ExecState*, JSDOMGlobalObject*);

    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);
    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);
    static const JSC::ClassInfo s_info;
    static PassRefPtr<JSC::Structure> createStructure(JSC::JSGlobalData& globalData, JSC::JSValue prototype)
    {
        return JSC::Structure::create(globalData, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), AnonymousSlotCount, &s_info);
    }
protected:
    static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | JSC::ImplementsHasInstance | DOMConstructorObject::StructureFlags;
};

const ClassInfo JSTestMediaQueryListListenerConstructor::s_info = { "TestMediaQueryListListenerConstructor", &DOMConstructorObject::s_info, &JSTestMediaQueryListListenerConstructorTable, 0 };

JSTestMediaQueryListListenerConstructor::JSTestMediaQueryListListenerConstructor(ExecState* exec, JSDOMGlobalObject* globalObject)
    : DOMConstructorObject(JSTestMediaQueryListListenerConstructor::createStructure(globalObject->globalData(), globalObject->objectPrototype()), globalObject)
{
    ASSERT(inherits(&s_info));
    putDirect(exec->globalData(), exec->propertyNames().prototype, JSTestMediaQueryListListenerPrototype::self(exec, globalObject), DontDelete | ReadOnly);
}

bool JSTestMediaQueryListListenerConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
    return getStaticValueSlot<JSTestMediaQueryListListenerConstructor, DOMObject>(exec, &JSTestMediaQueryListListenerConstructorTable, this, propertyName, slot);
}

bool JSTestMediaQueryListListenerConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
{
    return getStaticValueDescriptor<JSTestMediaQueryListListenerConstructor, DOMObject>(exec, &JSTestMediaQueryListListenerConstructorTable, this, propertyName, descriptor);
}

/* Hash table for prototype */
#if ENABLE(JIT)
#define THUNK_GENERATOR(generator) , generator
#else
#define THUNK_GENERATOR(generator)
#endif

static const HashTableValue JSTestMediaQueryListListenerPrototypeTableValues[2] =
{
    { "method", DontDelete | Function, (intptr_t)static_cast<NativeFunction>(jsTestMediaQueryListListenerPrototypeFunctionMethod), (intptr_t)1 THUNK_GENERATOR(0) },
    { 0, 0, 0, 0 THUNK_GENERATOR(0) }
};

#undef THUNK_GENERATOR
static JSC_CONST_HASHTABLE HashTable JSTestMediaQueryListListenerPrototypeTable = { 2, 1, JSTestMediaQueryListListenerPrototypeTableValues, 0 };
const ClassInfo JSTestMediaQueryListListenerPrototype::s_info = { "TestMediaQueryListListenerPrototype", &JSC::JSObjectWithGlobalObject::s_info, &JSTestMediaQueryListListenerPrototypeTable, 0 };

JSObject* JSTestMediaQueryListListenerPrototype::self(ExecState* exec, JSGlobalObject* globalObject)
{
    return getDOMPrototype<JSTestMediaQueryListListener>(exec, globalObject);
}

bool JSTestMediaQueryListListenerPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
    return getStaticFunctionSlot<JSObject>(exec, &JSTestMediaQueryListListenerPrototypeTable, this, propertyName, slot);
}

bool JSTestMediaQueryListListenerPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
{
    return getStaticFunctionDescriptor<JSObject>(exec, &JSTestMediaQueryListListenerPrototypeTable, this, propertyName, descriptor);
}

const ClassInfo JSTestMediaQueryListListener::s_info = { "TestMediaQueryListListener", &DOMObjectWithGlobalPointer::s_info, &JSTestMediaQueryListListenerTable, 0 };

JSTestMediaQueryListListener::JSTestMediaQueryListListener(NonNullPassRefPtr<Structure> structure, JSDOMGlobalObject* globalObject, PassRefPtr<TestMediaQueryListListener> impl)
    : DOMObjectWithGlobalPointer(structure, globalObject)
    , m_impl(impl)
{
    ASSERT(inherits(&s_info));
}

JSObject* JSTestMediaQueryListListener::createPrototype(ExecState* exec, JSGlobalObject* globalObject)
{
    return new (exec) JSTestMediaQueryListListenerPrototype(globalObject, JSTestMediaQueryListListenerPrototype::createStructure(globalObject->globalData(), globalObject->objectPrototype()));
}

bool JSTestMediaQueryListListener::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
    return getStaticValueSlot<JSTestMediaQueryListListener, Base>(exec, &JSTestMediaQueryListListenerTable, this, propertyName, slot);
}

bool JSTestMediaQueryListListener::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
{
    return getStaticValueDescriptor<JSTestMediaQueryListListener, Base>(exec, &JSTestMediaQueryListListenerTable, this, propertyName, descriptor);
}

JSValue jsTestMediaQueryListListenerConstructor(ExecState* exec, JSValue slotBase, const Identifier&)
{
    JSTestMediaQueryListListener* domObject = static_cast<JSTestMediaQueryListListener*>(asObject(slotBase));
    return JSTestMediaQueryListListener::getConstructor(exec, domObject->globalObject());
}

JSValue JSTestMediaQueryListListener::getConstructor(ExecState* exec, JSGlobalObject* globalObject)
{
    return getDOMConstructor<JSTestMediaQueryListListenerConstructor>(exec, static_cast<JSDOMGlobalObject*>(globalObject));
}

EncodedJSValue JSC_HOST_CALL jsTestMediaQueryListListenerPrototypeFunctionMethod(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    if (!thisValue.inherits(&JSTestMediaQueryListListener::s_info))
        return throwVMTypeError(exec);
    JSTestMediaQueryListListener* castedThis = static_cast<JSTestMediaQueryListListener*>(asObject(thisValue));
    TestMediaQueryListListener* imp = static_cast<TestMediaQueryListListener*>(castedThis->impl());
    RefPtr<MediaQueryListListener> listener(MediaQueryListListener::create(ScriptValue(exec->globalData(), exec->argument(0))));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    imp->method(listener);
    return JSValue::encode(jsUndefined());
}

JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, TestMediaQueryListListener* object)
{
    return getDOMObjectWrapper<JSTestMediaQueryListListener>(exec, globalObject, object);
}
TestMediaQueryListListener* toTestMediaQueryListListener(JSC::JSValue value)
{
    return value.inherits(&JSTestMediaQueryListListener::s_info) ? static_cast<JSTestMediaQueryListListener*>(asObject(value))->impl() : 0;
}

}


【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
先看效果: https://pan.quark.cn/s/3756295eddc9 在C#软件开发过程中,DateTimePicker组件被视为一种常见且关键的构成部分,它为用户提供了图形化的途径来选取日期与时间。 此类控件多应用于需要用户输入日期或时间数据的场景,例如日程管理、订单管理或时间记录等情境。 针对这一主题,我们将细致研究DateTimePicker的操作方法、具备的功能以及相关的C#编程理念。 DateTimePicker控件是由.NET Framework所支持的一种界面组件,适用于在Windows Forms应用程序中部署。 在构建阶段,程序员能够通过调整属性来设定其视觉形态及运作模式,诸如设定日期的显示格式、是否展现时间选项、预设的初始值等。 在执行阶段,用户能够通过点击日历图标的下拉列表来选定日期,或是在文本区域直接键入日期信息,随后按下Tab键或回车键以确认所选定的内容。 在C#语言中,DateTime结构是处理日期与时间数据的核心,而DateTimePicker控件的值则表现为DateTime类型的实例。 用户能够借助`Value`属性来读取或设定用户所选择的日期与时间。 例如,以下代码片段展示了如何为DateTimePicker设定初始的日期值:```csharpDateTimePicker dateTimePicker = new DateTimePicker();dateTimePicker.Value = DateTime.Now;```再者,DateTimePicker控件还内置了事件响应机制,比如`ValueChanged`事件,当用户修改日期或时间时会自动激活。 开发者可以注册该事件以执行特定的功能,例如进行输入验证或更新关联的数据:``...
<think>我们遇到的问题是在switch语句中使用资源ID(如R.id.tab_one)作为case标签时,Android Studio报错"Constant expression required"。 原因:在Java中,switch的case标签要求是编译时常量。而在Android中,资源ID虽然在R.java中被声明为final static,但在编译过程中,这些值并不是编译时常量(尽管它们看起来像是常量)。这是因为在构建过程中,资源ID可能会发生变化(尤其是在使用多个资源包时),因此它们不是编译时确定的常量。 解决方案: 1. 我们可以将switch语句改为使用if-else if结构。 2. 或者,我们可以使用Android注解@IdRes来获取资源ID,但这并不能直接解决switch的问题,因为问题本质在于资源ID不是编译时常量。 因此,我们通常使用if-else if结构来代替switch。 下面我将重构代码,使用if-else if结构: 原代码: @Override public void onClick(View v) { switch (v.getId()) { case R.id.tab_one: // ... break; // ... 其他case } } 重构为: @Override public void onClick(View v) { int id = v.getId(); if (id == R.id.tab_one) { binding.vp.setCurrentItem(0); binding.tabOne.setSelected(true); binding.tabTwo.setSelected(false); binding.tabThree.setSelected(false); binding.tabFour.setSelected(false); } else if (id == R.id.tab_two) { binding.vp.setCurrentItem(1); binding.tabOne.setSelected(false); binding.tabTwo.setSelected(true); binding.tabThree.setSelected(false); binding.tabFour.setSelected(false); } else if (id == R.id.tab_three) { binding.vp.setCurrentItem(2); binding.tabOne.setSelected(false); binding.tabTwo.setSelected(false); binding.tabThree.setSelected(true); binding.tabFour.setSelected(false); } else if (id == R.id.tab_four) { binding.vp.setCurrentItem(3); binding.tabOne.setSelected(false); binding.tabTwo.setSelected(false); binding.tabThree.setSelected(false); binding.tabFour.setSelected(true); } else if (id == R.id.img_button) { // 处理img_button的点击事件 // 注释掉的代码可以在这里恢复 } } 注意:在重构中,我们使用了if-else if链来根据不同的ID执行不同的操作。这样避免了switch语句对常量的要求。 另外,我们注意到原代码中有一个注释掉的关于popupWindow的部分,我们可以根据需求取消注释并调整。 但是,我们也可以注意到,在设置tab的选中状态时,代码重复较多。我们可以考虑优化,例如使用一个数组或集合来存储这些tab,然后根据当前选中的位置来设置状态。不过,这取决于你的布局结构,这里我们先按照原逻辑重构。 优化重复代码的建议(可选): 我们可以将所有的tab(View)放入一个数组中,然后根据点击的tab索引来设置当前选中的tab,并取消其他tab的选中状态。 例如: // 在类中定义 private View[] tabs; // 在onCreate中初始化 tabs = new View[]{binding.tabOne, binding.tabTwo, binding.tabThree, binding.tabFour}; // 然后在onClick中 int id = v.getId(); if (id == R.id.tab_one || id == R.id.tab_two || id == R.id.tab_three || id == R.id.tab_four) { int index = -1; if (id == R.id.tab_one) index = 0; else if (id == R.id.tab_two) index = 1; else if (id == R.id.tab_three) index = 2; else if (id == R.id.tab_four) index = 3; if (index != -1) { binding.vp.setCurrentItem(index); for (int i = 0; i < tabs.length; i++) { tabs[i].setSelected(i == index); } } } else if (id == R.id.img_button) { // ... } 这样减少重复代码。但注意,我们这里只是提供一种优化思路,具体实现可以根据项目结构调整。 由于问题要求解决报错,我们先用第一种方式(if-else if)重构,不进行额外优化。 重构后的代码: ```java @Override public void onClick(View v) { int id = v.getId(); if (id == R.id.tab_one) { binding.vp.setCurrentItem(0); binding.tabOne.setSelected(true); binding.tabTwo.setSelected(false); binding.tabThree.setSelected(false); binding.tabFour.setSelected(false); } else if (id == R.id.tab_two) { binding.vp.setCurrentItem(1); binding.tabOne.setSelected(false); binding.tabTwo.setSelected(true); binding.tabThree.setSelected(false); binding.tabFour.setSelected(false); } else if (id == R.id.tab_three) { binding.vp.setCurrentItem(2); binding.tabOne.setSelected(false); binding.tabTwo.setSelected(false); binding.tabThree.setSelected(true); binding.tabFour.setSelected(false); } else if (id == R.id.tab_four) { binding.vp.setCurrentItem(3); binding.tabOne.setSelected(false); binding.tabTwo.setSelected(false); binding.tabThree.setSelected(false); binding.tabFour.setSelected(true); } else if (id == R.id.img_button) { // 处理img_button的点击事件 // 例如:弹出PopupWindow // 可以恢复之前注释的代码,并根据需要修改 /* if (pop_one != 1) { initPop(v); } else { if (popupWindow != null) { popupWindow.dismiss(); pop_one = 0; } } */ } } ``` 这样修改后,就不会出现"Constant expression required"的错误了。 另外,如果使用Kotlin,我们可以使用when语句,因为Kotlin的when语句可以支持非常量的条件(它编译后也是if-else结构)。但原代码是Java,所以我们用if-else if。 解释:在Java中,资源ID在R.java中虽然声明为public static final int,但它们的值是在编译时由aapt工具生成的,并且可能会在每次编译时发生变化(尤其是在添加或删除资源时)。因此,Java编译器在编译switch语句时无法将这些值视为编译时常量(尽管它们是final的),因为它们的值在编译时并不固定(在编译不同的模块时,同一个资源的ID可能不同)。所以,我们只能使用if-else if结构。 相关问题: 1. 为什么在Android中不能在switch语句中使用资源ID? 2. 除了if-else if,还有没有其他方法处理多个View的点击事件? 3. 在Android中如何优化多个View选中状态的管理? 4. 如何通过ViewBinding来简化多个View的状态设置?</think>针对 Android 报错 "Constant expression required",这是因为在 Java 的 `switch` 语句中,`case` 标签必须使用编译时常量(如 `final` 变量或字面量)。但 Android 资源 ID(如 `R.id.tab_one`)在编译时不是真正的常量(尽管它们声明为 `final`),这是 Android 资源系统的设计限制。 ### 解决方案: 使用 `if-else` 结构替代 `switch`,或者使用资源 ID 映射方案。以下是两种重构方案: #### 方案 1:直接改为 if-else 结构(推荐) ```java @Override public void onClick(View v) { int id = v.getId(); if (id == R.id.tab_one) { binding.vp.setCurrentItem(0); setTabSelectedStates(true, false, false, false); } else if (id == R.id.tab_two) { binding.vp.setCurrentItem(1); setTabSelectedStates(false, true, false, false); } else if (id == R.id.tab_three) { binding.vp.setCurrentItem(2); setTabSelectedStates(false, false, true, false); } else if (id == R.id.tab_four) { binding.vp.setCurrentItem(3); setTabSelectedStates(false, false, false, true); } else if (id == R.id.img_button) { // 处理图片按钮逻辑 /* if (pop_one != 1) { initPop(v); } else if (popupWindow != null) { popupWindow.dismiss(); pop_one = 0; } */ } } // 辅助方法简化状态设置 private void setTabSelectedStates(boolean one, boolean two, boolean three, boolean four) { binding.tabOne.setSelected(one); binding.tabTwo.setSelected(two); binding.tabThree.setSelected(three); binding.tabFour.setSelected(four); } ``` #### 方案 2:使用资源 ID 映射(适用于大量 case) ```java // 定义 ID 映射常量 private static final int TAB_ONE = R.id.tab_one; private static final int TAB_TWO = R.id.tab_two; private static final int TAB_THREE = R.id.tab_three; private static final int TAB_FOUR = R.id.tab_four; private static final int IMG_BUTTON = R.id.img_button; @Override public void onClick(View v) { int id = v.getId(); if (id == TAB_ONE) { handleTabSelection(0); } else if (id == TAB_TWO) { handleTabSelection(1); } else if (id == TAB_THREE) { handleTabSelection(2); } else if (id == TAB_FOUR) { handleTabSelection(3); } else if (id == IMG_BUTTON) { // 图片按钮逻辑 } } private void handleTabSelection(int position) { binding.vp.setCurrentItem(position); // 使用数组简化状态管理 View[] tabs = {binding.tabOne, binding.tabTwo, binding.tabThree, binding.tabFour}; for (int i = 0; i < tabs.length; i++) { tabs[i].setSelected(i == position); } } ``` ### 关键点解释: 1. **编译时常量问题**: - Android 资源 ID 在 R 文件中是 `public static final int` 类型 - 但 Java 编译器不将其视为编译时常量(因值在编译后可能变化) - 导致无法直接在 `switch` 的 `case` 中使用 2. **优化技巧**: - 提取重复代码到辅助方法(如 `setTabSelectedStates()`) - 使用数组管理多个 View 的状态 - 通过映射常量提高可读性 3. **ViewBinding 优势**: - 直接使用 `binding.tabOne` 替代 `findViewById()` - 类型安全且编译时检查 ### 进一步优化建议: 1. 考虑使用 `ViewPager2` + `TabLayout` 实现标准标签页 2. 使用单一选择器管理标签状态(如 `RadioGroup`) 3. 将点击逻辑与状态更新分离(MVVM 模式)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值