【问题标题】:How to change the text of a progress bar如何更改进度条的文本
【发布时间】:2021-10-17 03:19:10
【问题描述】:

// 伙计们,我是一名初学者,我正在尝试将对话文本从“请稍候”更改为其他字符串文本,下面是我的 XML 标题

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="horizontal"
    android:padding="23dp">


    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="64dp"
        android:layout_height="64dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tvProgressBar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.hardextech.caramel.utils.MyCustomFont
        android:id="@+id/tvProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Wait......"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@+id/progressBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/progressBar"
        app:layout_constraintTop_toTopOf="@+id/progressBar" />

</androidx.constraintlayout.widget.ConstraintLayout>

// 下面是我在 Kotlin 中的代码

package com.hardextech.caramel.activities

import android.app.Dialog
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
import com.hardextech.caramel.R



open class BaseActivity : AppCompatActivity() {

    private lateinit var progressBar: Dialog

// Method to show the progressBar
        fun showProgressDialogue(text:String){
        progressBar = Dialog(this)
        progressBar.setContentView(R.layout.progress_dialogue)
        progressBar.tvProgressBar.text = text
        progressBar.setCancelable(false)
        progressBar.setCanceledOnTouchOutside(false)
        progressBar.show()

    }

    fun dismissProgressDialogue(){
        progressBar.dismiss()

    }
}

// 我希望代码行 progressBar.tvProgressBar.text = text 将对话文本更改为构造函数 text: String 但未实现。我不知道该怎么做。

【问题讨论】:

  • 我对你的代码有一个解决方案,但它是用java编写的,我稍后会发送给你
  • 谢谢。使用 java 解决方案将为使用 Kotlin 解决它提供指导或想法

标签: android kotlin methods progressdialog


【解决方案1】:

为了能够更改文本,您必须将布局放在视图对象中,以便在将视图对象设置为进度对话框的上下文之前可以引用TextView

    fun showProgressDialogue(text:String){
        progressBar = Dialog(this)
        val inflater = (context as Activity).layoutInflater 
        val view = inflater.inflate(R.layout.progress_dialogue, null)
        view.findViewById(R.id.tvProgressBar).text = text
        progressBar.setContentView(view)
        progressBar.setCancelable(false)
        progressBar.setCanceledOnTouchOutside(false)
        progressBar.show()

    }

【讨论】:

  • 谢谢。遵循您的指南和代码后它可以工作
猜你喜欢
  • 2023-03-16
  • 2016-11-02
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
相关资源
最近更新 更多