如何在Kotlin中调用带开关的活动?

enyaitl3  于 2023-01-31  发布在  Kotlin
关注(0)|答案(4)|浏览(123)

如何在Kotlin中使用switch-case调用activity?我想在Kotlin中使用switch-case结构,如下图所示。(Java代码)

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                break;
            case R.id.navigation_adminword:
                Intent a = new Intent(activity_callender.this,Words_List.class);
                startActivity(a);
                onStop();
                break;
            case R.id.navigation_myword:
                Intent b = new Intent(activity_callender.this,SaveWords.class);
                startActivity(b);
                onStop();
                break;
            case R.id.navigation_profil:
                Intent c = new Intent(activity_callender.this,UsersActivity.class);
                startActivity(c);
                onStop();
                break;
        }
        return false;
    }
niknxzdl

niknxzdl1#

由于Java switch在Kotlin中不可用,因此您不能使用when,甚至if案例...

fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.getItemId()) {
        R.id.navigation_home -> // do something...
        R.id.navigation_adminword -> {
            val a = Intent(this@activity_callender, Words_List::class.java)
            startActivity(a)
            onStop() // this method is called automatically, it is no necessary call it manually
        }
        R.id.navigation_myword -> {
            val b = Intent(this@activity_callender, SaveWords::class.java)
            startActivity(b)
            onStop() // this method is called automatically, it is no necessary call it manually
        }
        R.id.navigation_profil -> {
            val c = Intent(this@activity_callender, UsersActivity::class.java)
            startActivity(c)
            onStop() // this method is called automatically, it is no necessary call it manually
        }
        else -> {
            // else case would be the default switch case
        }
    }
    return false
}

实际上你可以用when做更多的事情,比如返回一个值,然后把它赋给一个变量,等等。
https://kotlinlang.org/docs/reference/control-flow.html#when-expression

dm7nw8vv

dm7nw8vv2#

When表达式(您的问题)

我将直接返回when表达式的结果(它返回每个分支的值),在这种情况下,您的乐趣变成了一个“单行程序”,它可以使用Kotlin表达式语法(=,而不是{}):

fun onNavigationItemSelected(item: MenuItem) = when (item.getItemId()) {
    R.id.navigation_home -> {
        // Do something
        true
    }
    R.id.navigation_adminword -> menuStartAndFinish(Words_List::class)
    R.id.navigation_myword -> menuStartAndFinish(SaveWords::class)
    R.id.navigation_profil -> menuStartAndFinish(UsersActivity::class)
    else -> false
}

帮助器扩展(跨项目重用)

我已经将intentstartActivity逻辑委托给这些扩展:

inline fun <reified T : Activity> Context.start(
    cls: KClass<T>, noinline init: (Intent.() -> Unit)? = null
) {
    Intent(this, cls.java).apply {
        init?.invoke(this)
        startActivity(this)
    }
}

inline fun <reified T : Activity> Context.menuStartAndFinish(
    cls: KClass<T>, noinline init: (Intent.() -> Unit)? = null
): Boolean {
    start(cls, init)
    finish()
    return true
}
ar7v8xwq

ar7v8xwq3#

我在下面的链接中使用了底栏。我无法调用Activity。我猜是因为onNavigationItemSelected属性不存在。enter link description here

56lgkhnf

56lgkhnf4#

1.如果你从Java迁移到Kotlin,那么你会发现语法部分也有一些变化。
1.在Java中,我们使用开关,但在Kotlin中,当时,开关转换为
1.when也用于条件表示,但它以一种非常聪明和容易的方式完成这些事情。
1.每当你有个可能性时,你就可以在代码中使用when
例如,在Java中使用switch编写的代码:-

switch(number) {
    case 1:
      System.out.println("1");
      break;
    case 2:
      System.out.println("2");
      break;
    case 3:
      System.out.println("3");
      break;
    default:
      System.out.println("Number is not between 1 and 3");
      break;
    }

例如,使用switch编写的代码在使用when后转换为如下所示的内容:

when(number) {
    1 -> println("1")
    2 -> println("2")
    3 -> println("3")
    else -> println("Number is not between 1 and 3")
}

因此,对于您的问题,您可以在Kotlin中将代码编写为:-

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.navigation_home -> {}
            R.id.navigation_adminword -> {
                startActivity(Intent(this, Words_List::class.java))
                onStop()
            }
            R.id.navigation_myword -> {
                startActivity(Intent(this, SaveWords::class.java))
                onStop()
            }
            R.id.navigation_myword -> {
                startActivity(Intent(this,  UsersActivity::class.java))
                onStop()
            }
        }
        return false
    }

相关问题