如何获得给定密封类的所有子类?

最近,我们把枚举类的一个升级为了对象作为子类的密封类,所以我们可以做另一个抽象层来简化代码。 然而,我们不能再通过Enum.values()函数获得所有可能的子类,这是不好的,因为我们严重依赖于这个function。 有没有办法通过reflection或其他工具检索这些信息? PS:手动添加到arrays是不可接受的 。 目前有45个,并计划增加更多。 这就是我们的密封课程的样子: sealed class State object StateA: State() object StateB: State() object StateC: State() ….// 42 more 如果有一个值集合,它将是这样的形状: val VALUES = setOf(StateA, StateB, StateC, StateC, StateD, StateE, StateF, StateG, StateH, StateI, StateJ, StateK, StateL, …… 当然,没有人想要维护这样一个怪物。

如何禁用从/到EditText复制/粘贴

在我的应用程序中,有一个注册屏幕,我不希望用户能够将文本复制/粘贴到EditText字段中。 我已经在每个EditText上设置了onLongClickListener ,以便显示复制/粘贴/输入方法和其他选项的上下文菜单不显示。 所以用户将无法复制/粘贴到编辑字段。 OnLongClickListener mOnLongClickListener = new OnLongClickListener() { @Override public boolean onLongClick(View v) { // prevent context menu from being popped up, so that user // cannot copy/paste from/into any EditText fields. return true; } }; 但是,如果用户启用了Android默认键盘以外的其他第三方键盘,则可能会出现问题,该键盘可能有一个复制/粘贴按钮或可能显示相同的上下文菜单。 那么我如何禁用复制/粘贴在这种情况下? 请让我知道是否有其他方法来复制/粘贴。 (并可能如何禁用它们) 任何帮助,将不胜感激。

Kotlin – NoSuchMethodError实例化Throwable时

尝试实例化Throwable对象会引发以下错误 java.lang.NoSuchMethodError: No direct method (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V in class Ljava/lang/Throwable; or its super classes (declaration of ‘java.lang.Throwable’ appears in /system/framework/core-libart.jar) 我尝试的不同方式是: val s = Throwable() val t = Throwable(“This is a message”) 我观察到的一些事情(在Mac上使用Android Studio): 在Java类中,“Command + click”ing Throwable将我带到java.lang.Throwable而从Kotlin类中,我带到了Throwable中的Throwable定义 我尝试使用val s = java.lang.Throwable()但它甚至不会编译 我正在使用Kotlin版本1.0.0-beta-2423 我错过了一些非常明显的东西吗

在使用标准Gradle生命周期插件时声明自定义的“干净”任务是不允许的

我正在向jCenter添加一个库,所以我需要添加一些插件到我的项目的build.gradle文件。 但是,我收到错误 在使用标准Gradle生命周期插件时声明自定义的“干净”任务是不允许的。 我可以看到task clean块,当我删除它的错误消失。 我认为这是我所需要做的,但是之前它做了一些重要的事情吗? 如果我在某个时候删除了插件,并忘记添加clean块,那么存储的可怕后果是什么? buildscript { repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.3.1’ // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } plugins { id “com.jfrog.bintray” version “1.7.3” id “com.github.dcendents.android-maven” version “1.5” } allprojects { repositories { jcenter() } } […]

我可以覆盖我的应用程序中的“主页”按钮吗?

我想在我的android上创建我自己的“家”屏幕,我想从我的应用程序调用主屏幕。 我怎样才能覆盖“主页”按钮,以便当它被按下时,应用程序将被重定向到我的主屏幕,而不是默认的主屏幕? 是否有可能重写主页按钮?

为什么在一个mutableList上的.map实际上并没有改变Kotlin中List的值?

这就是我所拥有的,我想达到的目标: 我有一个类有一个mutableList作为一个字段。 我想find列表中的一个特定的元素,并改变它。 这是我迄今为止所尝试的: 这是我希望能够工作的function语句,在我将它放入扩展函数之后: fun Classroom.setNewParameters(id: String, modifiedName: String) { this.students.filter { l -> l.id == id } .map { l -> l.name = modifiedName } .toList() } 但是这个函数被调用后,列表中的students似乎没有变化。 我用下面的代码find了一个“丑陋的”解决方案: fun Classroom.setNewParameters(id: String, modifiedName: String) { for (l : Student in this.students) { if (l.id == id) { l.name = modifiedName break } } […]

如何通过自定义的webView取代webView?

我有我的活动只包含一个webView。 活动写在Kotlin上: class MyActivity : AppCompatActivity() { @SuppressLint(“NewApi”) override fun onCreate(savedInstanceState: Bundle?) { // some code here verticalLayout { webView {//replace this by custom webView (ObservableWebView) setOnLongClickListener(object : View.OnLongClickListener { override fun onLongClick(v: View): Boolean { return true } }) } } } } 另外我有自定义的webView – ObservableWebView.java: public class ObservableWebView extends WebView { // some […]

Kotlin:Java无法解析Kotlin Symbol?

我有一个如下所示的Kotlin代码, SingleKotlin.instance可以被其他Kotlin文件调用 class SingleKotlin private constructor(){ companion object { val instance by lazy { SingleKotlin() } } } 但是,当我尝试从java调用SingleKotlin.instance时,它显示无法解析符号“实例” 我不明白为什么,有人可以解释,我该如何解决这个问题?

如何在Kotlin中使用AndroidAnnotation @FragmentArg?

我如何使用Kotlin片段中的@FragmentArg的AndroidAnnotations ? @EFragment(R.layout.debug_empty_fragment) open class EmptyFragment : Fragment(){ @FragmentArg(“debugIndex”) var debugIndex:Int = 0 } 这给我下面的gradle错误: error: org.androidannotations.annotations.FragmentArg cannot be used on a private element 我试图使debugIndex open (公共) ,但它不工作。 有任何想法吗? 这甚至有可能吗? 我使用Android Annotations 4.3.1和kotlin 1.1.2-5

在Kotlin嵌套注释

在Java中,可以创建如下所示的嵌套注释: public @interface InnerInnotation { String value(); } public @interface OuterAnnotation { InnerAnnotation[] value() default { @InnerAnnotation(“Hello”), @InnerAnnotation(“World”) } } annotation class InnerAnnotation(val value: String) 但是当我试图在Kotlin中做同样的事情时,我得到一个编译错误: annotation class OuterAnnotation( // Next line doesn’t compile: “Annotation class cannot be instantiated” val value: Array = arrayOf(InnerAnnotation(“Test”)) ) 但是,单个实例注记字段正常工作: annotation class OuterAnnotation( val value: InnerAnnotation = InnerAnnotation(“Hello World”) […]