有没有一种简单的方法可以从Android Studio中的另一个Activity访问函数

问题描述 投票:0回答:1

这是一个简单的问题,我希望有人能帮助我解决。我对 AS 相当陌生,我希望我没有过度思考这一点,但我非常相信充分研究一个主题,并且刚刚花了几天时间学习一本书,以便找到一种完成任务的简单方法;

我正在阅读一本书,本质上,当我继续阅读时,在我的内心深处,我正在尝试执行从另一个活动调用函数的简单任务。我已经学会了两种很长的方法来做到这一点:

with the findViewById method: I include a snippet of the app, but in total there were pages and pages of code just to do one tiny task
'''


package com.example.exercise205


import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import com.example.exercise205.RainbowColourPickerActivity

const val PICK_RAINBOW_COLOUR_INTENT = 1
const val RAINBOW_COLOUR_NAME="RAINBOW_COLOUR_NAME"
const val RAINBOW_COLOUR="RAINBOW_COLOUR"
const val DEFAULT_COLOUR="#FFFFFF"
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.submit_button).setOnClickListener {

            Intent(this, RainbowColourPickerActivity::class.java)
                .also { rainbowColourPickerIntent ->
                    startActivityForResult(
                        rainbowColourPickerIntent,
                        PICK_RAINBOW_COLOUR_INTENT
                    )
                }
        }
    }

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

        if (requestCode == PICK_RAINBOW_COLOUR_INTENT && resultCode == Activity.RESULT_OK) {

            val backgroundColor =
                data?.getIntExtra(RAINBOW_COLOUR, Color.parseColor(DEFAULT_COLOUR))
                    ?: Color.parseColor(
                        DEFAULT_COLOUR
                    )
            val colorName = data?.getStringExtra(RAINBOW_COLOUR_NAME) ?: ""
            val colorMessage = getString(R.string.colour_chosen_message, colorName)
            val rainbowColor = findViewById<TextView>(R.id.rainbow_color)

            rainbowColor.setBackgroundColor(ContextCompat.getColor(this, backgroundColor))
            rainbowColor.text = colorMessage
            rainbowColor.isVisible = true
        }
    }
}
'''
with a view model, using the 'Factory' method by far this seems more effective with less code and I am glad its been implemented into AS, but I still cant help but feel there is a quicker route. Am I missing something?
'''
class MainActivityViewModelFactory(private val startingTotal : Int) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(MainActivityViewModel::class.java)){
            return MainActivityViewModel(startingTotal) as T
        }
        throw IllegalArgumentException("Unknown View Model Class")
    }
}
'''

我可以不使用与 React Native/Expo 类似的方法,只需导入要调用的 Activity 并调用该函数吗?

android-intent findviewbyid factory-method
1个回答
0
投票
I really hope I can help someone who is in the same boat as me,
It took me quite a while to find the answer to this: essentially it comes in three parts, I had some trouble as I was working with fragments.

In this case I have a text field that I want to update with bluetooth devices from another page.


Part one

At the top of the sending fragment you need a top level declaration as a variable, in this case I am using the term DEVICES to store my data.

const val DEVICES = "DEVICE"

Part two

At the bottom of the sending fragment(include the class definition)

class MainActivity : AppCompatActivity() {
                    override fun onCreate(savedInstanceState: Bundle?) {
                        super.onCreate(savedInstanceState)

                        Intent(this, StyleFragment::class.java)
                            .also { styleFragment ->
                                styleFragment.putExtra(DEVICES, device.name)
                                startActivity(styleFragment)

Part three
At the bottom of the receiving fragment (include the class definition)

class MainActivity : AppCompatActivity() {

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

        intent?.let {
            val devices = it.getStringExtra(DEVICES)
            findViewById<TextView>(R.id.tt_conf_label)
                .text = getString(R.string.tt_confirmation_label, devices)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.