基本上我想将一个OnClickListener附加到ConstraintLayout中的多个视图.
在迁移到ConstraintLayout之前,我可以添加一个监听器的一个布局内的视图.现在,它们与ConstraintLayout下的其他视图位于同一层.
我尝试将视图添加到a android.support.constraint.Group
并以编程方式向其添加了OnClickListener.
group.setOnClickListener {
Log.d("OnClick", "groupClickListener triggered")
}
Run Code Online (Sandbox Code Playgroud)
但是,从ConstraintLayout版本开始,这似乎不起作用 1.1.0-beta2
我做错了什么,有没有办法实现这种行为或者我是否需要将监听器附加到每个单一视图?
我想定义Ord
一个自定义类型,以便根据到Advent of Code 2019 day 10 的Point
原点的距离进行排序:
impl std::cmp::Ord for Point {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let this = self.x * self.x + self.y + self.y;
let that = other.x * other.x + other.y * other.y;
return this.cmp(&that);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了这个编译错误:
= 帮助:该特征
std::cmp::PartialOrd
尚未实现helpers::models::Point
的文档PartialOrd
仅解释如何导出或实现它。这些都很清楚。
为什么Ord
取决于这个?Partial
特征名称中的名称意味着什么?什么时候会用到呢?
从 Kotlin 1.6.0 开始,对于 Kotlin/JVM 项目,可以指定-jvm-target version
最高可达 Java 的选项17
,请参阅常规和Gradle 插件文档。
这样做有什么好处呢?除了 的默认值之外,我找不到太多关于指定其他值的好处1.8
。
我能找到的唯一的东西是:
Record
支持 Java,请参阅Kotlin 1.5.0 的发布博客。两者对我来说似乎都可以忽略不计。
特别是因为当指定更高的目标时,人们将失去在使用 Java 1.8 的项目中使用生成的工件的能力,这似乎是不可取的,尤其是对于库而言。
我在这里错过了什么吗?
即使AsyncTask在internalHandler中使用Looper.getMainLooper(),为什么也必须按文档中所述在UI线程上调用AsyncTask
的方法?execute(Params...)
我尝试创建任务并从后台线程调用其execute(Params ...),它工作正常。
我正在尝试将 JSON 文件反序列化为我无法使用kotlinx.serialization
.
该类看起来类似于:
public data class Lesson(
val uid: String,
val start: Instant,
val end: Instant,
val module: String,
val lecturers: List<String>,
val room: String?,
val type: String?,
val note: String?
)
Run Code Online (Sandbox Code Playgroud)
我尝试解析的 JSON 如下所示:
{
"lessons": [
{
"uid": "sked.de956040",
"start": "2020-11-02T13:30:00Z",
"end": "2020-11-02T16:45:00Z",
"module": "IT2101-Labor SWE I: Gruppe 1 + 2",
"lecturers": [
"Kretzmer"
],
"room": "-",
"type": "La",
"note": "Prüfung Online"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是通过以下方式尝试的:
@Serializable
data class ExpectedLessons(
val lessons: List<Lesson>
)
val …
Run Code Online (Sandbox Code Playgroud) 我们使用 Bean 工厂函数来允许将 Logger 对象注入到我们的 Bean 中。这看起来类似于 Simar Paul Singh 在他的文章“ Inject loggers in Spring ”中描述的内容
import org.slf4j.*
import org.springframework.beans.factory.InjectionPoint
import org.springframework.context.annotation.*
@Bean
@Scope("prototype")
fun logger(injectionPoint: InjectionPoint): Logger {
return LoggerFactory.getLogger(
injectionPoint.methodParameter?.containingClass // constructor
?: injectionPoint.field?.declaringClass // or field injection
)
}
Run Code Online (Sandbox Code Playgroud)
今天我尝试使用Springs 功能性 bean 定义 DSL将此声明转换为 Bean 声明。然而,我没有成功地获取InjectionPoint
用于检索记录器注入的类的信息。
import org.slf4j.*
import org.springframework.beans.factory.InjectionPoint
import org.springframework.context.support.beans
import org.springframework.context.support.BeanDefinitionDsl.Scope.PROTOTYPE
fun beans() = beans {
bean(scope = PROTOTYPE) {
val injectionPoint = ref<InjectionPoint>()
LoggerFactory.getLogger(
injectionPoint.methodParameter?.containingClass // constructor
?: injectionPoint.field?.declaringClass // …
Run Code Online (Sandbox Code Playgroud) 我使用一些用 Kotlin 编写的业务逻辑。我的方法中有这样的情况 - 我检查一个值是否为 null,如果它为 null - 我想返回 null,否则执行一些逻辑。我的版本看起来像:
fun calculateFoo(parameter: SomeObject?): ReturnObject? =
if (parameter == null) null
else performCalculation(parameter)
Run Code Online (Sandbox Code Playgroud)
字面意思是“如果 value 为 null,则返回自身,否则对其进行处理”。通常,如果 value 不为 null,我们会执行某些操作,例如 elvis 运算符,但这里是“反向”逻辑。对此有更好的解决方案吗?
我有两种不同的菜单布局.我想在单击按钮时更改运行时的膨胀菜单.
最初菜单设置为@menu/navigation
.我希望它更改@menu/navigation2
为单击按钮时.
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="th.ac.sd.sdschoolas.MainActivity">
<FrameLayout
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<WebView
android:id="@+id/web_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="152dp"
android:layout_height="158dp"
android:layout_x="23dp"
android:layout_y="46dp"
app:srcCompat="@mipmap/logo"
android:padding="20dp"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
菜单/ navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav1"
android:icon="@mipmap/homeicon"
android:title="@string/title_home" />
<item
android:id="@+id/nav2"
android:icon="@mipmap/newsicon01"
android:title="@string/title_news" />
<item
android:id="@+id/nav3"
android:icon="@drawable/ic_calendar_page_empty"
android:title="@string/title_cal" />
<item
android:id="@+id/nav4"
android:icon="@drawable/ic_chat_bubbles"
android:title="@string/title_sms" />
<item
android:id="@+id/nav5"
android:icon="@mipmap/more"
android:title="@string/title_more" />
</menu>
Run Code Online (Sandbox Code Playgroud)
菜单/ …
我是Pascal的新手,目前正在使用指针.我有2条记录,其中一条包含指向另一条记录类型的2条指针.
type
WaypointRef = ^Waypoint;
PathRef = ^Path;
Waypoint = record
id: integer;
Name: string;
pathRefs: array of PathRef;
end;
Path = record
distance: integer;
WaypointRefA, WaypointRefB: WaypointRef;
end;
Run Code Online (Sandbox Code Playgroud)
所有航路点都保存在一个阵列中.现在,当我试图读出路径的值时,我得到了神秘的结果:
writeln(waypoints[0].pathRefs[0]^.distance);
writeln(waypoints[1].pathRefs[0]^.distance);
Run Code Online (Sandbox Code Playgroud)
两者都应该打印相同的值,但它们不会.然而,更神秘的是,即使我尝试以下内容:
writeln(waypoints[0].pathRefs[0]^.distance);
writeln(waypoints[0].pathRefs[0]^.distance);
writeln(waypoints[0].pathRefs[0]^.distance);
Run Code Online (Sandbox Code Playgroud)
我得到2个不同的值.(正确的 - 先是173 - 然后是2次.)
waypoints[0].pathRefs[0]^
Run Code Online (Sandbox Code Playgroud)
总是指向同一个地址,因此我很困惑.我希望有人知道这个问题.
编辑:2似乎是默认值,因为如果我在路径创建时没有将任何值保存到"距离",它也会返回2.
EDIT2:这里是航点和路径创建的代码.我认为一定有失败.我现在可能会因设备内部的程序而混淆设计.我只是在试验.
procedure buildWaypoint(Name: string);
procedure addWaypoint(w: Waypoint);
var
lngth: integer;
begin
lngth := Length(waypoints);
SetLength(waypoints, lngth + 1);
waypoints[lngth] := w;
end;
var
w: Waypoint;
begin
w.id := id;
id := id + 1;
w.Name := …
Run Code Online (Sandbox Code Playgroud) 我现在正在编写一个小型 Kotlin 库,想知道是否有一种方法可以验证哪些类和函数是公开公开的。
我基本上想确保该库不会公开任何不属于公共 API 一部分的实现细节。
到目前为止,我将所有内容表示为internal
库用户不应该能够访问给定的类/函数。这种方法的问题是我可能会忘记将某些内容声明为internal
,从而向用户公开某些内容。
当我非故意公开类/方法时,我希望在构建时收到警告/错误。
任何有关如何实现这一目标或最佳实践的提示都将受到高度赞赏!
如果您在包含两个对象的集合上从kotlin-stdlibminBy
运行or ,并且最低/最高值并列,则该函数将返回哪一个?maxBy