b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

了解|在Android中实现进度条按钮功能(kotlin)(2)

电脑杂谈  发布时间:2020-04-02 12:06:20  来源:网络整理

自定义属性attr

我们在Android中的res--values下创建了一个attr.xml,名称无关紧要.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ProgressButton">  
        <attr name="backgroundcolor" format="color"  /> 
        <attr name="foreground" />  
        <attr name="textcolor" format="color" />  
        <attr name="max" />  
        <attr name="progress" />  
        <attr name="textSize" />  
        <attr name="text" format="string" />
    </declare-styleable>
</resources>

通过以上两个步骤,实现了我们的ProgressButton自定义控件. 接下来,我们将了解如何使用它.

android进度条显示进度_android 页面加载进度_android进度条的使用

使用ProgressButton

微信智慧

activity_main.xml

在主窗体的布局文件中,您可以直接添加我们刚刚生成的ProgressButton.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <dem.vac.progressbutton.ProgressButton
        android:layout_gravity="center"
        android:layout_width="100dp"
        android:layout_height="50dp"
        app:backgroundcolor="@color/colorLightSkyBlue"
        app:foreground="@color/colorRoyalBlue"
        app:textcolor="@color/colorWhite"
        app:text="点击开始"
        android:id="@+id/progressbtn" />
</LinearLayout>

MainActivity.kt

android进度条显示进度_android 页面加载进度_android进度条的使用

接下来看一下我们主程序的代码

package dem.vac.progressbutton
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlin.concurrent.thread
class MainActivity : AppCompatActivity() {
    lateinit var probtn: ProgressButton
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        probtn = findViewById(R.id.progressbtn)
        probtn.setOnProgressButtonClickListener(object :
            ProgressButton.OnProgressButtonClickListener {
            override fun onClickListener() {
                thread {
                    for (i in 1..100) {
                        probtn.setProgress(i)
                        probtn.setText("当前$i")
                        Thread.sleep(50)
                    }
                }
            }
        })
    }
}

在代码中android进度条的使用,我们直接调用ProgressButton,然后重写onProgressButtonClickListerner事件. 在这种情况下,我们启动一个线程并执行1到100的循环. 该调用的效果是本文开头的效果.

此分享来自公开微信的号码-智享卡(VaccaeShare),作者: Vaccae

有关原始来源和转载信息的详细信息android进度条的使用,请访问yunjia_community@tencent.com删除任何侵权行为.

最初发表于: 2019-12-09


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-161213-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 信乐团
      信乐团

      你一来正好給我们一个接口把人造岛军事化

    热点图片
    拼命载入中...