我希望从Kotlin代码中将Button的focussable和focussableInTouchMode属性设置为true/false。
实际上是在科特林:
stb_help_btn.focusable = false
给出错误:
The Boolean literal does not conform to the expected type Int
有人能帮忙吗?
发布于 2020-10-30 04:42:34
从视图源代码来看,setFocusable()
是带有两个签名的方法重载:
/**
* 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形式传递布尔值的,这是编译器给出错误的原因。
解决方案:将两种方法重载结合在一起
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
}
发布于 2020-10-29 12:26:38
If(button.hasFocus) {
button.setFocusable(false);
button.setFocusableInTouchMode(false);
}
更新:
@RequiresApi(Build.VERSION_CODES.O)
button.focusable = View.NOT_FOCUSABLE
button.isFocusableInTouchMode = false
需要API Oreo
和更高的focusable
https://stackoverflow.com/questions/64590987
复制相似问题