Android studio :进度类控件

ProgressBar
Layout部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <TextView
            android:layout_width="80dp"
            android:textColor="#FF0000"
            android:id="@+id/tv_main_desc"
            android:textSize="30dp"
            android:layout_height="match_parent" />
        <ProgressBar
            android:layout_width="match_parent"
            style="?android:attr/progressBarStyleHorizontal"
            android:id="@+id/pb_main_download"
            android:max="100"
            android:layout_height="match_parent" />
    </FrameLayout>
    <Button
        android:layout_width="match_parent"
        android:text="启动下载"
        android:onClick="doClick"
        android:layout_height="wrap_content" />
</LinearLayout>

Java部分:

import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private int p=0;//当前进度
    private ProgressBar pb;//进度条
    private TextView tv;//显示文本的控件
    private MyHandler myHandler=new MyHandler();//新写的Handler类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件对象并与控件进行绑定
        pb=findViewById(R.id.pb_main_download);
        tv=findViewById(R.id.tv_main_desc);
    }
    public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int code=msg.what;//接受处理码
            switch (code){//这里使用switch便于扩充其它消息
                case 1:
                    p++;//计数器累加
                    pb.setProgress(p);//给进度条的当前进度赋值
                    tv.setText(p+"%");//显示当前进度为多少
                    break;
            }
        }
    }
    //定义一个线程类继承自Thread类
    public class myThread extends Thread{
        @Override
        public void run() {//重写run方法
            super.run();
            while(true){
                try {
                    Thread.sleep(100);//使线程休眠0.1秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(p==100){//当前进度等于总进度时退出循环
                    p=0;
                    break;
                }
                Message msg=new Message();//创建消息对象
                msg.what=1;
                myHandler.sendMessage(msg);//发送处理码
            }
        }
    }
    public void doClick(View v){
        if(0==p){//如果当前进度为0
            new myThread().start();//开启线程
        }
    }
}

在这里插入图片描述
SeekBar
Layout部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="5.0dp"
        android:minHeight="5.0dp"/>
    <TextView
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拖动试试"/>
</LinearLayout>

Java部分:

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private SeekBar sb;//定义拖动控件对象
    private TextView txt;//定义文本控件对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件对象并与之绑定
        sb =  findViewById(R.id.sb);
        txt = findViewById(R.id.txt);
        //设置拖动控件改变监听事件
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txt.setText("当前进度值:" + progress + "  / 100 ");
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this, "触碰SeekBar", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this, "放开SeekBar", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

在这里插入图片描述
在这里插入图片描述
RatingBar
Layout部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <RatingBar
        android:id="@+id/rb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="1.5"/>
</LinearLayout>

Java部分:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private RatingBar rb;//定义评分控件对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rb = (RatingBar) findViewById(R.id.rb);//初始化评分控件
        //设置评分控件改变监听事件
        rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                //当评分发生改变时做出提示
                Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating),
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值