You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Oreo(8.0)设备上禁用视图获焦时的自动高亮?

嘿,这个问题我之前帮不少开发者处理过,三星S8在Android 8.0上的这个默认焦点高亮确实有点烦人,给你几个靠谱的解决办法,你可以根据自己的场景选:

解决三星S8(Android 8.0)视图获焦自动高亮的方法

1. 布局XML中直接配置(最省心)

如果是固定不需要高亮的视图,直接在布局文件里加属性就行:

  • 要是不需要视图获取焦点,直接禁用焦点:
<Button
    android:id="@+id/my_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="测试按钮"
    android:focusable="false"
    android:focusableInTouchMode="false" />
  • 要是需要保留焦点功能但去掉高亮效果,就替换掉默认的高亮背景:
<TextView
    android:id="@+id/my_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="测试文本"
    android:background="@android:color/transparent" />

2. 代码中动态控制(适合逻辑切换场景)

如果需要根据业务逻辑动态开启/禁用高亮,就在Java或Kotlin代码里设置:

  • Kotlin示例:
// 禁用焦点
myBtn.isFocusable = false
myBtn.isFocusableInTouchMode = false

// 或者保留焦点但移除高亮背景
myBtn.background = ColorDrawable(Color.TRANSPARENT)
  • Java示例:
Button myBtn = findViewById(R.id.my_btn);
// 禁用焦点
myBtn.setFocusable(false);
myBtn.setFocusableInTouchMode(false);

// 或者移除高亮背景
myBtn.setBackground(new ColorDrawable(Color.TRANSPARENT));

3. 针对三星系统的特殊样式处理

三星早期的TouchWiz系统有自己的默认控件样式,上面的方法如果不管用,试试自定义样式覆盖系统默认:
先在styles.xml里创建一个无高亮的样式:

<style name="NoFocusHighlightStyle" parent="Widget.AppCompat.Button">
    <item name="android:stateListAnimator">@null</item>
    <item name="android:selectableItemBackground">@null</item>
</style>

然后把这个样式应用到你的视图上:

<Button
    android:id="@+id/my_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="测试按钮"
    style="@style/NoFocusHighlightStyle" />

4. 列表类控件(ListView/RecyclerView)的处理

如果是列表项获焦高亮,就在Adapter里处理:

  • RecyclerView的Kotlin示例:
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    // 禁用列表项焦点
    holder.itemView.isFocusable = false
    // 或者移除默认选择背景
    holder.itemView.background = null
}
  • ListView的Java示例:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    }
    // 禁用列表项焦点
    view.setFocusable(false);
    return view;
}

内容的提问来源于stack exchange,提问作者sider

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:13:21