Android基础控件之SeekBar(拖动条)

基础控件SeekBar用于触摸拖动,eg:手机中的音量的调节、亮度调节等,可用到SeekBar实现。

此处以调节音量大小为例:

<-------------------XML文件------------------>

<?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"
    android:orientation="vertical"
    tools:context="com.example.example20.MainActivity">

   <TextView
       android:id="@+id/text_view"
       android:textSize="20dp"
       android:text="音量大小: 0"
       android:paddingTop="10dp"
       android:paddingLeft="10dp"
       android:paddingRight="10dp"
       android:paddingBottom="10dp"
       android:textAlignment="center"
       android:textColor="#2d4a87"
       android:background="#72b3cf"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
    </TextView>
<!-----android:max用于设置拖动条的最大值
android:progress用于设置拖动条的当前进度
android:thumb该属性指定一个图片作为滑动条的滑块----->
    <SeekBar					//定义SeekBar控件
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<-----------------------------MainActivity文件---------------------------->

package com.example.example20;

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

public class MainActivity extends AppCompatActivity {
    final static double MAX=100;
    SeekBar seekBar;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar=(SeekBar)findViewById(R.id.seek_bar);
        textView=(TextView)findViewById(R.id.text_view);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {		//设置监听
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView.setText("音量大小: "+(int)seekBar.getProgress());   //根据拖动位置设置textView的值
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值