Kotlin单击打开文件 [英] Kotlin Open file on button click

查看:88
本文介绍了Kotlin单击打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kotlin的新手.有人可以告诉我如何在单击按钮时打开文件吗?是否有一些命令,如:

I am new to Kotlin. Can someone please tell me how to open a file on click of a button? Is there some command like:

Button.setOnAction {
File.Open(*//specified file path*)  
        }

我正在使用 Kotlin进行Java开发,而不是用于Andriod.我有一个 .fxml 文件,在其中定义了此按钮,我需要在kotlin(.kt)文件中定义以上功能.谢谢你.

I am using Kotlin for Java development and not for andriod. I have a .fxml file where I have defined this button and I need to define the above feature in a kotlin (.kt) file. Thank you.

推荐答案

活动中

btnBack.setOnClickListener {

    val intent = Intent()
            .setType("*/*")
            .setAction(Intent.ACTION_GET_CONTENT)

    startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)

}

仍在您的活动中,插入该行以获取结果

still in your activity, insert this to catch the result

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == 111 && resultCode == RESULT_OK) {
        val selectedFile = data?.data //The uri with the location of the file
    }
}

参考

这篇关于Kotlin单击打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆