[Android Studio 30天自我挑战] SeekBar元件介绍

这次介绍的SeekBar类似前面介绍的Progress Bar,但SeekBar有多一个拖曳的滑动杆,SeekBar继承自ProgressBar,因此SeekBar类别中的很多方法均继承自ProgressBar。

SeekBar 常用语法

android:max ="数值"	//设定范围的最大值
android:progress="数值"  //设定范围的目前数值
android:secondaryProgress="数值"	//次条SeekBar的数值
android:thumb = "" // 设定SeekBar的外观

SeekBar 事件常用语法

onProgressChanged //进度发生改变时会触发
onStartTrackingTouch //按住时会触发
onStopTrackingTouch //停止拖曳时触发事件

范例:用SeekBar显示目前的数值,按下和放开时会跳出Toast
xml范例如下:

<?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" >
    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:max="100"
        android:progress="0"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="目前数值: 0/100" />
</LinearLayout>

MainActivy.java范例如下:

package com.example.itseekbarr;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private SeekBar seekBar;
    private TextView textView;
    private Context Context;

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

        Context = MainActivity.this;
        bindViews();
    }

    private void bindViews() {
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        textView = (TextView) findViewById(R.id.textView);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView.setText("目前数值:" + progress + "  / 100 ");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(Context, "按住SeekBar", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(Context, "放开SeekBar", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

显示画面如下:
https://ithelp.ithome.com.tw/upload/images/20210929/20139258XqqNXhoKAq.png


<<:  Cotex-M55 首篇

>>:  Android学习笔记20

Day 09 Summary

Introduction to embedded system Components and app...

[Day 5] 餐前浓汤 pt.2-BeautifulSoup meets Stocks

上一篇我们设定完了vagrant,安装完了BeautifulSoup 这一篇我们就要来让Beutif...

Halloween Kills线上看2021

Halloween Kills线上看2021 《月光光心慌慌:杀戮》(英语:Halloween Ki...

Day-4: SSR 与 SPA

SSR — Server Side Rendering 服务器渲染 服务器端渲染 — 在服务器(Se...

DAY02 初探资料分析

一、何谓资料分析 资料分析是一种统计方法,其主要特点是多维性和描述性,有些几何方法有助於揭示不同的资...