首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Kotlin代码中将focusable和focusableInTouchMode属性设置为false

从Kotlin代码中将focusable和focusableInTouchMode属性设置为false
EN

Stack Overflow用户
提问于 2020-10-29 12:19:08
回答 2查看 1.1K关注 0票数 1

我希望从Kotlin代码中将Button的focussable和focussableInTouchMode属性设置为true/false。

实际上是在科特林:

代码语言:javascript
运行
复制
stb_help_btn.focusable = false 

给出错误:

代码语言:javascript
运行
复制
The Boolean literal does not conform to the expected type Int

有人能帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-30 04:42:34

视图源代码来看,setFocusable()是带有两个签名的方法重载:

代码语言:javascript
运行
复制
/**
 * Set whether this view can receive the focus.
 * <p>
 * Setting this to false will also ensure that this view is not focusable
 * in touch mode.
 *
 * @param focusable If true, this view can receive the focus.
 *
 * @see #setFocusableInTouchMode(boolean)
 * @see #setFocusable(int)
 * @attr ref android.R.styleable#View_focusable
 */
public void setFocusable(boolean focusable) {
    setFocusable(focusable ? FOCUSABLE : NOT_FOCUSABLE);
}

/**
 * Sets whether this view can receive focus.
 * <p>
 * Setting this to {@link #FOCUSABLE_AUTO} tells the framework to determine focusability
 * automatically based on the view's interactivity. This is the default.
 * <p>
 * Setting this to NOT_FOCUSABLE will ensure that this view is also not focusable
 * in touch mode.
 *
 * @param focusable One of {@link #NOT_FOCUSABLE}, {@link #FOCUSABLE},
 *                  or {@link #FOCUSABLE_AUTO}.
 * @see #setFocusableInTouchMode(boolean)
 * @attr ref android.R.styleable#View_focusable
 */
public void setFocusable(@Focusable int focusable) {
    if ((focusable & (FOCUSABLE_AUTO | FOCUSABLE)) == 0) {
        setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
    }
    setFlags(focusable, FOCUSABLE_MASK);
}

从科特林打电话:

setFocusable(boolean focusable):使用isFocusable属性

setFocusable(@Focusable int focusable):使用focusable属性

根原因:调用focusable并以param形式传递布尔值的,这是编译器给出错误的原因。

解决方案:将两种方法重载结合在一起

代码语言:javascript
运行
复制
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
    stb_help_btn.isFocusable = false
} else {
    stb_help_btn.focusable = View.NOT_FOCUSABLE
}
票数 2
EN

Stack Overflow用户

发布于 2020-10-29 12:26:38

代码语言:javascript
运行
复制
If(button.hasFocus) {
button.setFocusable(false);
button.setFocusableInTouchMode(false);
}

更新:

代码语言:javascript
运行
复制
@RequiresApi(Build.VERSION_CODES.O)
button.focusable = View.NOT_FOCUSABLE
button.isFocusableInTouchMode = false

需要API Oreo和更高的focusable

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64590987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档