我对 kotlin 不熟悉。我有一个应用程序,它的构建工具版本为 29.0.3,以下代码运行良好。
init {
// Make sure that the view finder reference is valid
val viewFinder = viewFinderRef.get()
?: throw IllegalArgumentException("Invalid reference to view finder used")
// Initialize the display and rotation from texture view information
viewFinderRotation = getDisplaySurfaceRotation(viewFinder.display)
// Initialize public use-case with the given config
previewUseCase = Preview(config)
// Every time the view finder is updated, recompute layout
previewUseCase.setOnPreviewOutputUpdateListener { output ->
val vFinder = viewFinderRef.get() ?: return@setOnPreviewOutputUpdateListener
// To update the SurfaceTexture, we have to …Run Code Online (Sandbox Code Playgroud) 在基于 Kotlin 的 Android 应用程序中,我使用样式属性开发了一个启动画面,如下代码所示:
可绘制文件(这个是浅色的,同样是深色的,具有不同的渐变颜色代码):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#43dc91"
android:startColor="#29abe2" />
</shape>
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/My_Image" />
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
Style 也有 2 个 style.xml 文件——一个用于深色,一个用于浅色:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_back</item>
</style>
Run Code Online (Sandbox Code Playgroud)
它是从清单中调用的:
<activity
android:name="com.app.ui.splash.SplashActivity"
android:theme="@style/SplashTheme">
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我的应用程序从浅色模式切换到暗模式时,用户将关闭应用程序然后再次打开应用程序,2 秒后,它仍然显示浅色模式闪屏,2 秒后,暗模式闪屏将加载。
这可能是因为当应用程序在启动活动 -> Oncreate 中加载时,我正在验证应用程序是否具有深色主题的偏好,然后加载深色模式,然后将我的应用程序切换到深色模式,如下所示:
private fun setAppTheme() {
when {
userHolder.theme != null -> {
if (userHolder.theme == string_(R.string.text_dark_mode))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
}
Run Code Online (Sandbox Code Playgroud)
遇到这种情况怎么办?在从样式和可绘制对象加载时,如何在第一个实例本身中加载暗模式飞溅?
我们有一个PreferenceFragmentCompat,然后点击一个首选项,我们想从当前的 切换PreferenceFragmentCompat到新的PreferenceFragmentCompat。(在新屏幕上进行某些设置)。
但是,无论我们尝试过什么,我们都会遇到以下错误:
Fragment 声明的目标片段不属于此 FragmentManager
主活动.kt
class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback, PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_preferences
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
val …Run Code Online (Sandbox Code Playgroud) 我想创建一个大小矩阵,nxn其中 n 是 input 的长度message:String。到目前为止,这是我想到的唯一解决方案,它也有四个 for 循环。
fun main(){
println("Enter the message:")
var message:String = readLine().toString()
var cipher = Array(message.length) { Array<Int>(message.length) {0} }
for(i in 0 .. (message.length - 1)){
for(j in 0 .. (message.length - 1)){
cipher[i][j] = readLine()!!.toInt()
}
}
//print the matrix
for(i in 0..(message.length -1)){
for(j in 0..(message.length -1)){
print(cipher[i][j])
}
println()
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单的代码?如何改进此代码?
如何提供范围或如何从服务 Android 调用挂起功能?通常,活动或视图模型为我们提供了范围,从那里我们可以启动暂停但服务中没有类似的东西
你能解释一下返回的 Flux/Mono 到底发生了HttpClient.response() 什么吗?我认为在 Mono 完成之前,http 客户端生成的值不会传递到下游,但我看到生成了大量请求,但最终以reactor.netty.internal.shaded.reactor.pool.PoolAcquirePendingLimitException: Pending acquire queue has reached its maximum size of 8异常告终。它按预期工作(项目正在处理一个接一个),如果我更换呼叫testRequest()用Mono.fromCallable { }。
我错过了什么?
测试代码:
import org.asynchttpclient.netty.util.ByteBufUtils
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.netty.http.client.HttpClient
import reactor.netty.resources.ConnectionProvider
class Test {
private val client = HttpClient.create(ConnectionProvider.create("meh", 4))
fun main() {
Flux.fromIterable(0..99)
.flatMap { obj ->
println("Creating request for: $obj")
testRequest()
.doOnError { ex ->
println("Failed request for: $obj")
ex.printStackTrace()
}
.map { res ->
obj to res
}
}
.doOnNext …Run Code Online (Sandbox Code Playgroud) 比较两种方法scan和runningFold(来自kotlin.collections)的页面,除了名称外,两者似乎相同。
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/scan.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/running-fold.html
我有一个数据类,其中包含一个名为“prio”的成员(下面的示例)。这个“prio”应该创建recyclerView如何显示项目的顺序->带有数字1的“prio”应该显示在rv的顶部,数字2在数字1的下方,依此类推。
附加信息:但是用户可以决定该项目应该具有哪个编号(prio)。所以“prio”不是固定的。
数据类
data class TaskModel(
val id: Int,
val task: String,
var prio: Int
)
Run Code Online (Sandbox Code Playgroud)
问题:你能帮我创建一个函数来对“prio”进行排序并在 recyclerView 中初始化这个函数作为 recyclerView 显示项目的顺序吗?
依恋:
适配器:
open class TaskAdapter (private val context: Context, private val list: ArrayList<TaskModel>)
: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val mTaskDetails : TaskModel? = null
private var onClickListener: OnClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MyViewHolder(
LayoutInflater.from(context).inflate(R.layout.activity_item_adapter, parent, false)
)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) …Run Code Online (Sandbox Code Playgroud) 我正在尝试覆盖类中的值。我有以下代码:
open class Balloon() {
open var textSize: Float = 20f
init {
Log.i("textSize", textSize.toString())
}
}
class BigBalloon(): Balloon() {
override var textSize = 30f
}
Run Code Online (Sandbox Code Playgroud)
但是,日志会打印出这些值:
第一个日志来自Balloon(),第二个来自BigBalloon()。0.0当我将其覆盖为 时,它如何打印30?我是否错误地实施了所有这些?
我们目前如何从 swift收集Flow?
通过在 Swift流的collect方法中对 Kotlin 的挂起函数的新支持,可以在 swift 中获得这个相当笨重的签名
vm.topStoriesFlow.collect(
collector: Kotlinx_coroutines_coreFlowCollector,
completionHandler: @escaping (KotlinUnit?, Error?) -> Void
)
Run Code Online (Sandbox Code Playgroud)
知道如何使用它,或者即使当前支持它的使用吗?
kotlin kotlin-native kotlin-multiplatform kotlin-coroutines kotlin-flow
kotlin ×10
android ×5
class ×1
coroutine ×1
fragment ×1
inheritance ×1
kotlin-flow ×1
mobile ×1
sorting ×1