SeekBar 组件 类似于拖动设置音量大小

文章目录


####1、功能介绍
类似于拖动显示 音量 或 亮度大小
这里写图片描述

####2、代码架构
这里写图片描述

####3、activity_main.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="com.example.lum.seekbar.MainActivity">

    <TextView
        android:id="@+id/text_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="音量:0"
        android:textSize="30dp" />

    <SeekBar
        android:id="@+id/seekbar_id"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginTop="30dp" />
</LinearLayout>

####4、功能文件 ManiActivity.java

package com.example.lum.seekbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private TextView textView;
    private SeekBar seekBar;

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

        textView = (TextView) findViewById(R.id.text_id);
        seekBar = (SeekBar) findViewById(R.id.seekbar_id);
        seekBar.setOnSeekBarChangeListener(this);   //seekbar  监听
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textView.setText("音量:" + (int) seekBar.getProgress());    //滑动时设置 text view 显示大小
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/82724971