按按钮执行操作,而不是使用后退按钮

发布于 2025-01-16 00:43:25 字数 2450 浏览 0 评论 0原文

我正在从图库中选择一张图像。

按回button

当我按下后退按钮时,将调用 getResultCamera.launch(intent) 并发生操作。

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_selected_image)

        image = findViewById(R.id.the_selected_image);
        srButton = findViewById(R.id.super_resolution_button)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
                requestPermissions(permissions, PERMISSION_CODE)
            } else {
                chooseImageGallery()
            }
        } else {
            chooseImageGallery()
        }


    }


private fun chooseImageGallery() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        getResultCamera.launch(intent)
    }


 private val getResultCamera =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                imageUri = it.data?.data as Uri
                image.setImageURI(imageUri)
                intent.putExtra("imageuri", imageUri)
                

            }
        }

因此,它转到 MainActivity,加载图像并在那里进行操作。

现在,我想使用一个按钮供用户按下,而不是后退按钮。

所以,我正在尝试类似的方法:

private val getResultCamera =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                imageUri = it.data?.data as Uri
                image.setImageURI(imageUri)
                
                srButton.setOnClickListener { view: View ->
                    intent.putExtra("imageuri", imageUri)
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                }

            }
        }

with button

但是当我按下按钮时,它只是返回到 MainActivity 而不执行操作。

I am selecting an image from gallery.

press back button

When I press the back button, the getResultCamera.launch(intent) is called and an operation takes place.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_selected_image)

        image = findViewById(R.id.the_selected_image);
        srButton = findViewById(R.id.super_resolution_button)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
                requestPermissions(permissions, PERMISSION_CODE)
            } else {
                chooseImageGallery()
            }
        } else {
            chooseImageGallery()
        }


    }


private fun chooseImageGallery() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        getResultCamera.launch(intent)
    }


 private val getResultCamera =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                imageUri = it.data?.data as Uri
                image.setImageURI(imageUri)
                intent.putExtra("imageuri", imageUri)
                

            }
        }

So, it goes to the MainActivity, it loads the image and the operation takes place there.

Now, I want to use a button for the user to press instead of the back button.

So, I am trying something like:

private val getResultCamera =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                imageUri = it.data?.data as Uri
                image.setImageURI(imageUri)
                
                srButton.setOnClickListener { view: View ->
                    intent.putExtra("imageuri", imageUri)
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                }

            }
        }

with button

but when I press the button, it just returns to the MainActivity without executing the operation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2025-01-23 00:43:25

尝试将 finish() 放入按钮的 OnClickListener 中。这基本上就像用户按下后退按钮一样。

Try putting finish() in the button's OnClickListener. This basically acts as though the user pressed the back button.

清泪尽 2025-01-23 00:43:25

请尝试这个方法可能对你有帮助

private val pickImages = registerForActivityResult(ActivityResultContracts.GetContent()){ uri: Uri? ->
     uri?.let { it ->
         // The image was saved into the given Uri -> do something with it
             imageUri = it

          }
     }        
        
        
    pickImages.launch("image/*")
        

    srButton.setOnClickListener { view: View ->
         intent.putExtra("imageuri", imageUri)
         val intent = Intent(this, MainActivity::class.java)
         startActivity(intent)
    }


 @Override
 protected void onBackPressed(){
  //Do not call super.onbackpressed here
 } 

Please try this way may help you

private val pickImages = registerForActivityResult(ActivityResultContracts.GetContent()){ uri: Uri? ->
     uri?.let { it ->
         // The image was saved into the given Uri -> do something with it
             imageUri = it

          }
     }        
        
        
    pickImages.launch("image/*")
        

    srButton.setOnClickListener { view: View ->
         intent.putExtra("imageuri", imageUri)
         val intent = Intent(this, MainActivity::class.java)
         startActivity(intent)
    }


 @Override
 protected void onBackPressed(){
  //Do not call super.onbackpressed here
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文