我有以下扩展方法。如何引用OnGlobalLayoutListener传递给addOnGLobalLayoutListener()方法的 ?我需要将侦听器传递给该removeOnGlobalLayoutListener()方法。
fun View.OnGlobalLayout(callback:() -> Unit ): Unit{
this.viewTreeObserver.addOnGlobalLayoutListener {
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
this.viewTreeObserver.removeOnGlobalLayoutListener(this);
}
else {
this.viewTreeObserver.removeGlobalOnLayoutListener(this);
}
callback();
}
}
Run Code Online (Sandbox Code Playgroud) 我想限制可以调用哪些常量值扩展函数。例如功能如下:
@IdRes
fun <T : View> Int.find() = findViewById<T>(this)
Run Code Online (Sandbox Code Playgroud)
如果这是在真实 id 上调用的,那很好:
R.id.someView.find<TextView>() // ok
Run Code Online (Sandbox Code Playgroud)
但这应该会导致编译错误:
42.find<TextView>() // should be compile error
Run Code Online (Sandbox Code Playgroud)
Kotlin 是否支持注释扩展接收器?
迁移到后,androidx我发现,在一些而非全部课程中,
kotlinx.android.synthetic 字段现在无法转换为实际的类。
窗口小部件具有无法解析的类型'androidx.core.widget.DrawerLayout',因此被上载至'android.view.View'
是kotlinx.android.synthetic目前与androidx不兼容?
我正在尝试在 Android 的数据绑定中使用 Kotlin 扩展方法。例如; 调用一个 onclick 处理程序。所以我做了这个代码:
posttest_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<data>
<import type="android.view.View"/>
<import type="com.example.test.post.posttest.PostTestItemViewModelExtensionKt" />
<variable
name="viewModel"
type="com.example.test.post.posttest.PostTestItemViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:clickable="true"
android:onClick="@{(view) -> viewModel.clicked(view)}"
>
[...]
Run Code Online (Sandbox Code Playgroud)
PostTestItemViewModel.kt
open class PostTestItemViewModel : ViewModel() {
val postTitle = MutableLiveData<String>()
val postBody = MutableLiveData<String>()
/**
* Binds the required properties/entities to this ViewModel
*/
fun bind(post: Post) {
postTitle.value = post.title
postBody.value = post.body
}
}
Run Code Online (Sandbox Code Playgroud)
PostTestItemViewModelExtension.kt
fun PostTestItemViewModel.clicked(v: View) {
this.postTitle.value = "clicked"
} …Run Code Online (Sandbox Code Playgroud) 代码 A 是Context、Fragment和的扩展函数Activity。
我认为这是多余的,我该如何优化它?
代码A
fun Context.toast(msg: String){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}
fun Context.toast(@StringRes resId: Int){
toast(getString(resId))
}
fun Context.toast(@StringRes resId: Int,msg: String){
toast(getString(resId) + msg)
}
fun Context.toast(msg: String,@StringRes resId: Int){
toast(msg + getString(resId))
}
//------------------------------------------------
fun Fragment.toast(msg:String) {
requireContext().toast(msg)
}
fun Fragment.toast(@StringRes resId: Int) {
toast(requireContext().getString(resId))
}
fun Fragment.toast(@StringRes resId: Int, msg: String) {
toast(requireContext().getString(resId) + msg)
}
fun Fragment.toast( msg: String, @StringRes resId: Int) {
toast(msg+ requireContext().getString(resId))
}
//------------------------------------------------
fun Activity.toast(msg: …Run Code Online (Sandbox Code Playgroud) 是否可以从另一个包调用扩展函数而不导入它?
给定一个扩展函数:
package ext
fun Int.plusOne() = this + 1
Run Code Online (Sandbox Code Playgroud)
有没有办法在不先导入函数的情况下调用该函数?
我可以在不导入的情况下调用非扩展函数(忽略该函数不需要导入,只需注意语法有效):
val el: List<Int> = kotlin.emptyList()
Run Code Online (Sandbox Code Playgroud)
我可以实例化类而无需导入:
val str = java.lang.String("yo.")
Run Code Online (Sandbox Code Playgroud)
但我还没有找到等效的扩展(我知道有些例子很愚蠢):
val i = 42
// All those are not valid syntax...
i.ext.plusOne()
ext.plusOne(i)
i.(ext.plusOne)()
i.(ext.plusOne())
ext.i.plusOne()
val pO = ext.plusOne
i.pO()
Run Code Online (Sandbox Code Playgroud)
奖励:同样的问题,但针对扩展属性。
编辑:要添加到无效示例列表中,即使在扩展接收者是隐式的地方,也不允许使用 FQDN:
// Good:
import ext.plusOne
val four = with(3) { plusOne() }
// Unresolved reference:
val four = with(3) { ext.plusOne() }
Run Code Online (Sandbox Code Playgroud) 我是Kotlin的新手.我有一个包含两个方法定义的接口:
fun onSuccess(result: T)
fun onFailure(e: Exception)
Run Code Online (Sandbox Code Playgroud)
现在,在我的片段中,我实现了这个接口,并希望在里面使用这些方法:
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
..................
..................
override fun onSuccess(result: String) {}
override fun onFailure(e: Exception) {}
}
Run Code Online (Sandbox Code Playgroud)
在java中我们可以使用@override但在这里我得到错误'修饰符'覆盖'不适用于本地函数'.我在kotlin工作了2-3天,我喜欢它.但有些时候小问题需要一些时间来调试.
我有一些活动,我使用Intent在它们之间传递数据。我想从第一个活动中传递我的自定义对象的数组,并在第二个活动中使其成为arraylist。我拥有的代码是:
data class Attachment(val Name: String, val Content: String)
class ActivityA {
private var attachments: Array<Attachment> = arrayOf()
fun callB() {
intent = Intent(this,ActivityB::class.java).apply {
putExtra("Attachments", attachments)
}
}
}
class ActivityB {
private var attachments: ArrayList<Attachment>?
override fun onCreate(savedInstanceState: Bundle?) {
// How do I get the passed array and store in the arraylist here ?
val a: Array<Attachment> = intent.getParcelableArrayExtra("Attachments") as Array<Attachment>
attachments = a // fails with a type mismatch error
attachments = ArrayList(a) // fails again …Run Code Online (Sandbox Code Playgroud) 我使用此代码为Logandroid类添加扩展名
fun Log.i2(msg:String):Unit{
Log.i("Test",msg)
}
Run Code Online (Sandbox Code Playgroud)
在活动中使用时
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.i2("activity_main")
}
}
Run Code Online (Sandbox Code Playgroud)
找不到Log.i2。怎么了?
是否可以向所有类添加扩展功能?我正在考虑将其添加到一些常见的基类(如Object)中。可能吗?