小编Ber*_*hus的帖子

如何设置 AndroidX 库的 EditTextPreference 的最大长度?

最近,我将我的 android 项目迁移到了 AndroidX,并使用了 AndroidX 库中的 EditTextPreference。现在,我想将 EditTextPreference 的最大长度设置为 50。我尝试使用:

android:maxLength="50"
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

似乎所有 android 命名空间都不适用于 EditTextPreference 并且没有代码建议,所以我找不到任何相关代码来设置最大长度。如何设置最大长度?

android edittextpreference androidx

5
推荐指数
1
解决办法
810
查看次数

按下返回键始终使用喷气背包导航返回起始目的地

所以,我BottomNavigationView与 Jetpack Navigation 联系在一起。假设我有 4 个底部导航菜单,片段 A、B、C 和 D,A 作为起始目的地。从片段 A 转到片段 B,然后转到片段 C。然后,我按下硬件后退按钮。我期望它返回到片段 B,相反,它返回到片段 A(这是起始目的地)。

这是代码:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
navController = navHostFragment.navController

binding.navView.setupWithNavController(navController)
Run Code Online (Sandbox Code Playgroud)

我怎样才能改变这种行为?

谢谢~

编辑:我遵循 Zain 的答案,行为已经符合预期。但是,还有另一个问题。假设我有另一个片段 A1,它不是BottomNavView片段的一部分。我可以从片段 A 导航到片段 A1。下面是导航图:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_a">

    <fragment
        android:id="@+id/navigation_a"
        android:name="com.example.FragmentA"
        android:label=""
        tools:layout="@layout/fragment_a">
        <action
            android:id="@+id/action_navigation_a_to_navigation_a1"
            app:destination="@id/navigation_a1"
            app:launchSingleTop="true" />
    </fragment>
    <fragment
        android:id="@+id/navigation_a1"
        android:name="com.example.FragmentA1"
        android:label=""
        tools:layout="@layout/fragment_a1" />
    <fragment
        android:id="@+id/navigation_b"
        android:name="com.example.FragmentB"
        android:label=""
        tools:layout="@layout/fragment_b" />
    <fragment
        android:id="@+id/navigation_c"
        android:name="com.example.FragmentC"
        android:label=""
        tools:layout="@layout/fragment_c" />
    <fragment
        android:id="@+id/navigation_d"
        android:name="com.example.FragmentD"
        android:label=""
        tools:layout="@layout/fragment_d" …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-architecture-components android-jetpack-navigation

3
推荐指数
1
解决办法
1874
查看次数

如何在 Activity 中获取 navController?

我使用 Jetpack Navigation 在片段和活动之间导航。所以,我在布局中有一个MainActivity带有 a 的。FragmentContainerView我可以轻松地从片段导航到片段/活动。但是,我找不到一种方法如何使用navController.

例如,我从片段 FA 调用navController.navigate()活动 A。现在,我想从活动 A 导航到活动 B 或片段 FB。

我已经尝试过这个:

val host = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = host.navController
Run Code Online (Sandbox Code Playgroud)

但总是出现这个错误

null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment
Run Code Online (Sandbox Code Playgroud)

谢谢!

android android-jetpack android-jetpack-navigation

2
推荐指数
1
解决办法
6245
查看次数