一手遮天 Android - view(进度类): SeekBar 基础

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(进度类): SeekBar 基础

示例如下:

/view/progress/SeekBarDemo1.java

/**
 * SeekBar - 拖动条
 *     setOnSeekBarChangeListener(OnSeekBarChangeListener l) - 拖动条发生改变时的回调
 * OnSeekBarChangeListener
 *     onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) - 进度值发生变化
 *         progress - 当前进度值
 *         fromUser - 是否是用户操作导致的进度变化(比如用户拖动了进度则此值为 true;程序修改了进度则此值为 false)
 *     onStartTrackingTouch(SeekBar seekBar) - 拖动开始
 *     onStopTrackingTouch(SeekBar seekBar) - 拖动结束
 */

package com.webabcd.androiddemo.view.progress;

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

import com.webabcd.androiddemo.R;

public class SeekBarDemo1 extends AppCompatActivity {

    private TextView _textView1;
    private SeekBar _seekBar1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_progress_seekbardemo1);

        _textView1 = (TextView)findViewById(R.id.textView1);
        _seekBar1 = (SeekBar)findViewById(R.id.seekBar1);

        sample();
    }

    private void sample() {
        _seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                _textView1.setText(String.format("progress:%d, fromUser:%b", progress, fromUser));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(getApplicationContext(), "start tracking touch", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(getApplicationContext(), "stop tracking touch", Toast.LENGTH_SHORT).show();
            }
        });

        // 在 java 中设置拖动条的进度
        _seekBar1.setProgress(30);
    }
}

/layout/activity_view_progress_seekbardemo1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        SeekBar - 拖动条
            min - 最小值
            max - 最大值
            progress - 进度值(相当于播放器的当前播放位置)
            secondaryProgress - 第二进度的进度值(相当于播放器的当前已缓冲位置)
    -->

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="300dp"
        android:layout_height="30dp"
        android:layout_marginTop="10dp"
        android:min="0"
        android:max="100"
        android:progress="10"
        android:secondaryProgress="70" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 12:55  webabcd  阅读(91)  评论(0编辑  收藏  举报