摆脱活动中重复的代码部分

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

我是Android的新手,所以在某个时候,我意识到我有20个活动,其中有相同的部分。

应用的整体结构如下:Activity1-> Activity2-> TaskActivity1-> Activity3-> Activity4-> TaskActivity2-> ...

因此,有两种重复代码类型:用于简单活动和用于任务活动。

提供简单活动的代码:

class Activity2 : AppCompatActivity() {

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

fun nextClick(view: View, ID: Int) {
    val intent3 = Intent(this, Activity3::class.java)
    startActivity(intent3)
    this.finish()
}

fun backClick(view: View){
    val intent1 = Intent(this, Activity1::class.java)
    startActivity(intent1)
    this.finish()
}

}

对于任务,仅添加了另外3个按钮(用于答案)(因此,其中有5个按钮:下一个,ans1,ans2,ans3,返回)。另外,某些简单的活动在布局中没有后退按钮。

因此,显然我有大约15个活动使用相同的代码(看起来很糟糕)。

我尝试过:

1)为活动创建一个类:

open class everyActivity(imageId: Int, nextActivity: Activity, prevActivity: Activity): AppCompatActivity() {

val imageId = imageId
val nextActivity = nextActivity
val prevActivity = prevActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(imageId)
}

fun nextClick(view: View) {
    val intentNext = Intent(this, nextActivity::class.java)
    startActivity(intentNext)
    this.finish()
}

fun backClick(view: View){
    val intentPrev = Intent(this, prevActivity::class.java)
    startActivity(intentPrev)
    this.finish()
}

}

然后在活动中:

class Kadr1Activity : KadrActivity(R.layout.activity_kadr01, Kadr2Activity(), Kadr0InfoActivity()) {
}

它不起作用,应用程序显示白屏。

2)提取点击的功能:

无法在onClick中传递Activity变量,因此提取函数没有帮助。

3)坚持1个活动:

我不想每次单击都更改ImageView图片。正如我已经提到的,某些活动的外观有所不同(没有后退按钮),因此根据当前图像跟踪更改按钮并不方便。

4)单一活动架构

我想在活动之间进行动画处理,并带有片段,这太复杂了(根据我发现的信息)。

所以,有可能摆脱我的应用中的重复代码吗?

非常感谢您提供任何可能的帮助,不知道如何解决此问题

android android-studio kotlin
1个回答
0
投票

如果不能只交换片段和had来进行单独的Activity,我将制作一个Activity类并打开它的多个实例,使用Intent Extras确定行为上的细微差别,如下所示:] >

class MyActivity: AppCompatActivity()

    private val screen by lazy { 
        Screen.valueOf(intent.getStringExtra(SCREEN_KEY) ?: Screen.SCREEN_ONE.name)
        // The Elvis operator is to specifiy the default for when the activity
        // is the first instance from when the app was launched.
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_activity)
        imageView.setImageResource(screen.imageID)

        prepareButton(previousButton, Screen[screen.prevScreen])
        prepareButton(nextButton, Screen[screen.nextScreen])
    }

    private fun prepareButton(button: Button, targetScreen: Screen?) {
        if (targetScreen == null) {
            button.visibility = View.INVISIBLE
        } else {
            button.setOnClickListener { 
                val intent = Intent(this, MyActivity::class.java)
                intent.putExtra(SCREEN_KEY, targetScreen.name)
                startActivity(intent)
                finish()
           }
        }
    }
}

private const SCREEN_KEY = "SCREEN"

enum class Screen(val imageID: Int, val prevScreen: String?, val nextScreen: String?) {
    SCREEN_ONE(R.drawable.screenOneImg, null, "SCREEN_TWO"),
    SCREEN_TWO(R.drawable.screenTwoImg, "SCREEN_ONE", "SCREEN_THREE"),
    SCREEN_THREE(R.drawable.screenThreeImg, "SCREEN_TWO", null)

    companion object {
        operator fun get(name: String?) = name?.let { Screen.valueOf(it) }
    }
}

为简单起见,请注意,上面的示例代码的编写就像使用合成视图属性一样。如果不是,则将使用findViewById或视图绑定来获取视图引用。


编辑:

这是没有枚举或惰性属性的更适合初学者的方法:

class MyActivity: AppCompatActivity()

    companion object {
        private const SCREEN_KEY = "SCREEN"
        private const lastScreenIndex = 2 // if there are three screens (counting starts at 0)
    }

    private var screen: Int = 0

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

        screen = intent.getStringExtra(SCREEN_KEY) ?: 0 // 0 is default screen number
        val screenImageResource = when (screen) {
            0 -> R.drawable.screen_0_image
            1 -> R.drawable.screen_1_image
            else -> R.drawable.screen_2_image
        }
        imageView.setImageResource(screenImageResource )

        prepareScreen(previousButton, screen - 1)
        prepareScreen(nextButton, screen + 1)
    }

    private fun prepareButton(button: Button, targetScreen: Int) {
        if ((0..lastScreenIndex).contains(targetScreen)) {
            button.setOnClickListener { 
                val intent = Intent(this, MyActivity::class.java)
                intent.putExtra(SCREEN_KEY, targetScreen)
                startActivity(intent)
                finish()
           }
        } else {
            button.visibility = View.INVISIBLE
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.