seekBar 的用法

<         大家可以粘贴代码进自己的Androidstudio观看 图片不好显示         >

SeekBar :允许用户拖动滑块来改变值,因此通常用于对系统的某种数值进行调节,比如音量。

、、布局:添加一个SeekBar拖动条;再添加一个TextView文本来显示拖动条进度。

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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.myapplication.MainActivity">

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="800"


     />
    <TextView
        android:id="@+id/tv_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_gravity="center_horizontal"
        />

LinearLayout>
、、将拖动滑块以图标形式呈现
 
  
 
  

、、添加监听事件:实现拖动滑块计数
 
  
package com.example.administrator.myapplication;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    SeekBar myseekBar;
    TextView seekBarView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBarView=(TextView)findViewById(R.id.tv_text_view);
        myseekBar=(SeekBar)findViewById(R.id.seekBar2);
        myseekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                seekBarView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

 
  

你可能感兴趣的:(android)