小编Cod*_*ist的帖子

如何格式化保留两位小数的数字?

这就是我正在做的以显示两位小数的金额。它工作正常,但我想知道这是否正确或者这样做有什么缺点以及更好的方法是什么

\n

我想格式化“您的价格”和“MRP”的金额。我怎样才能做到这一点?

\n
        holder.itemView.tv_dashboard_item_title.text = model.title\n        holder.itemView.tv_dashboard_item_price.text = "Your Price \xe2\x82\xb9${model.price}"\n        holder.itemView.tv_dashboard_item_mrp.text = "MRP \xe2\x82\xb9${model.mrp}"\n        val mrp:Double? = model.mrp\n        val price: Double? = model.price\n        val save=DecimalFormat("#,##0.00")\n        val save2: Double = mrp!! - price!!\n        val saves=save.format(save2)\n\n        holder.itemView.tv_dashboard_item_you_save.text = "You Save \xe2\x82\xb9  $saves"\n
Run Code Online (Sandbox Code Playgroud)\n

谢谢。

\n

编辑

\n

修改后的代码。

\n
    val decFormat = DecimalFormat("#,##0.00")\n    holder.itemView.tv_dashboard_item_title.text = model.title\n    val mrp: BigDecimal = model.mrp.toBigDecimal()\n    val price: BigDecimal = model.price.toBigDecimal()\n\n    val save: BigDecimal = mrp - price\n    val saveAmount = decFormat.format(save)\n\n    holder.itemView.tv_dashboard_item_price.text …
Run Code Online (Sandbox Code Playgroud)

number-formatting decimalformat kotlin

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

在 Android Kotlin 上检查网络连接的问题

我调用以下函数onCreate,当我在进行此活动时打开和关闭设备上的网络连接时,它会完美地显示该消息。但是,当我在设备上的网络关闭时打开此活动时,它不会显示网络已关闭的消息,而当我在网络打开时打开此活动时,它会显示我正在运行的消息连接到网络。

       private fun checkConnection() {
        connectivity = CheckConnectivity(application)
        connectivity.observe(this, { isConnected ->
            if (isConnected) {
          Toast.makeText(this,"You are connected to the internet",Toast.LENGTH_SHORT).show()
            } else {
           Toast.makeText(this,"You are NOT connected to the internet",Toast.LENGTH_SHORT).show()
            }
        })
    }
Run Code Online (Sandbox Code Playgroud)

以下是CheckConnectivity.kt

    class CheckConnectivity(private val connectivityManager: ConnectivityManager) :
    LiveData<Boolean>() {

    constructor(application: Application) : this(
        application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    )

    private val networkCallback = @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    object: ConnectivityManager.NetworkCallback(){

        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            postValue(true)
        }

        override fun onLost(network: Network) {
            super.onLost(network)
            postValue(false)
        }
    } …
Run Code Online (Sandbox Code Playgroud)

android kotlin

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

如何在 Kotlin 的 Fragment 中隐藏作为 Activity 一部分的 FloatingActionButton?

在我的“HomeActivity”中,我有 aFloatingActionButton和 4 Fragments。我希望FloatingActionButton仅在“fragment1”和“fragment2”中显示,将其隐藏在“fragment3”和“fragment4”中。我尝试过,但没有成功。

以下是我在xml“HomeActivity”文件中的内容

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:menu="@menu/bottom_nav_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_hide_category"
            app:layout_anchor="@id/bottom_appbar"/>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation"
        app:layout_anchor="@id/bottom_appbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

我在“HomeActivity”中添加了以下代码

fun hideFabOne(){
    binding.fabOne.hide()
}
Run Code Online (Sandbox Code Playgroud)

下面的代码位于onCreateViewfragment想要隐藏 FAB 的地方。但是,这没有用。

HomeActivity().hideFabOne()
Run Code Online (Sandbox Code Playgroud)

android-fragments kotlin floating-action-button

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