SeekBar control
(1), overview
drag bar control, class name: SeekBar , similar in appearance to the progress bar, and allows the user to change the value of the slider by dragging the slider. The SeekBar is used to adjust the screen brightness and volume in the mobile phone.
(2) Common properties:
SeekBar is an indirect subclass of ProgressBar , so the properties of SeekBar refer to the property list of ProgressBar .
(3), OnSeekBarChangeListener interface
1. Overview
This interface is responsible for monitoring the slider movement of the SeekBar control.
2. Common methods:
Three methods are defined in this interface:
1) void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
Function: When the slider progress value changes, this method is triggered to execute.
seekBar parameter: the current SeekBar object
progress parameter: the progress value of the slider
fromUser parameter: whether the user has dragged the slider, true: yes, false: no
2) void onStartTrackingTouch(SeekBar seekBar);
Function: When the slider starts to move, this method is triggered to execute, and this method is no longer triggered during the slider movement.
3) void onStopTrackingTouch(SeekBar seekBar);
Function: This method is triggered when the slider is finished moving.
Example:
Code in MainActivity.java
package com.jxust.day03_08; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends ActionBarActivity implements OnSeekBarChangeListener { int mProgress; //represents the current progress value of the SeekBar @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar); // Save the address of MainActivity to OnSeekBarChangeListener seekBar.setOnSeekBarChangeListener(this); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mProgress = progress; Log.i("main", "progress="+progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Log.i("main", "Start dragging seekBar"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Log.i("main", "End dragging seekBar"); } }
Code in activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" /> </RelativeLayout>