按按钮执行操作,而不是使用后退按钮
我正在从图库中选择一张图像。
当我按下后退按钮时,将调用 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)
}
}
}
但是当我按下按钮时,它只是返回到 MainActivity
而不执行操作。
I am selecting an image from gallery.
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)
}
}
}
but when I press the button, it just returns to the MainActivity
without executing the operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
finish()
放入按钮的 OnClickListener 中。这基本上就像用户按下后退按钮一样。Try putting
finish()
in the button's OnClickListener. This basically acts as though the user pressed the back button.请尝试这个方法可能对你有帮助
Please try this way may help you