Learn android<seekbar slide assembly from scratch. 22 .>

Source: Internet
Author: User

The drag bar can be manually adjusted by the user, for example, when the user needs to adjust the volume of the player or the progress of the movie, a drag bar is used, and the definition structure of the Seekbar class is as follows:Java.lang.Object   ? Android.view.View      ? Android.widget.ProgressBar        ? Android.widget.AbsSeekBar          ? Android.widget.SeekBar Common Methods
Public SeekBar (Context context) Structure To create an object of the Seekbar class
Public void Setonseekbarchangelistener (seekbar.onseekbarchangelistener L) Ordinary Setting Change listener operation
Public synchronized void Setmax (int max) Ordinary Set the maximum value for growth

Public static interface seekbar.onseekbarchangelistener{/*** trigger action when starting drag * @param seekBar seekBar Component Object that triggers the action  */Public abstract void Onstarttrackingtouch (SeekBar SeekBar);/** * @param seekBar seekBar Component Object that triggers the action * @param Progress Current progress value * @param Whether the fromuser is triggered by the user itself  */Public abstract void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser);/*** trigger action when dragging is stopped * @param seekBar seekBar Component Object that triggers the action  */Public abstract void Onstoptrackingtouch (SeekBar SeekBar);}

Basic Use

XML File
<span style= "FONT-SIZE:18PX;" ><relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".        Mainactivity "> <seekbar android:max=" android:progress= "" android:id= "@+id/seekbar1" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentle        Ft= "true" android:layout_alignparenttop= "true" android:layout_margintop= "60DP"/> <textview        Android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignleft= "@+id/seekbar1" android:layout_below= "@+id/seekbar1" android:layout_marginleft= "28DP" Androi d:layout_margintop= "32DP" android:text= "Seek1" android:textappearance= "? Android:attr/textappearancemedium"/        > <seekbar android:id= "@+id/seekbar2" android:max= "android:secondaryprogress=" 60 " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_alignparentleft= "t Rue "android:layout_below=" @+id/textview1 "android:layout_margintop=" 67DP "/> <textview Android Oid:id= "@+id/textview2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android        oid:layout_alignleft= "@+id/textview1" android:layout_below= "@+id/seekbar2" android:layout_margintop= "28DP" android:text= "Seek2" android:textappearance= "Android:attr/textappearancemedium"/></relativelayout> </span>

Java files
<span style= "FONT-SIZE:18PX;" >package com.example.seekbar;import android.app.activity;import Android.os.bundle;import Android.widget.SeekBar ; Import Android.widget.seekbar.onseekbarchangelistener;import Android.widget.textview;public class MainActivity Extends Activity implements Onseekbarchangelistener{private TextView textview1,textview2;private SeekBar seekBar1,    SEEKBAR2;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        textview1= (TextView) This.findviewbyid (R.ID.TEXTVIEW1);        Textview2= (TextView) This.findviewbyid (R.ID.TEXTVIEW2);        seekbar1= (SeekBar) This.findviewbyid (R.ID.SEEKBAR1);        Seekbar2= (SeekBar) This.findviewbyid (R.ID.SEEKBAR2);        Seekbar1.setonseekbarchangelistener (this);    Seekbar2.setonseekbarchangelistener (this); } @Overridepublic void Onprogresschanged (SeekBar SeekBar, int position, Boolean flag) {//TODO auto-generated MethoD stubif (Seekbar.getid () ==seekbar1.getid ()) {Textview1.settext ("Current seekbar1 scale" +position);} else {textview2.settext ("Current SEEKBAR2 scale" +position);}} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {//TODO auto-generated method Stubif (seekbar.getid () = = Seekbar1.getid ()) {Textview1.settext ("Start sliding Seek1");} else {textview2.settext ("Start sliding Seek2");}} @Overridepublic void Onstoptrackingtouch (SeekBar SeekBar) {//TODO auto-generated method Stubif (seekbar.getid () = = Seekbar1.getid ()) {Textview1.settext ("Stop sliding Seek1");}    else {textview2.settext ("Stop sliding Seek2");}} }</span>




use Seekbar to control the brightness of the screen

XML File
<span style= "FONT-SIZE:18PX;" ><relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Fill_parent "and roid:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true        "Android:layout_margintop=" 24DP "android:text=" Adjust the phone brightness "/> <seekbar android:max=" 100 "        Android:id= "@+id/seekbar1" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_alignleft= "@+id/textview1" android:layout_below= "@+id/textview1" android:layout_margintop= "117DP" Android Oid:progress= "/></relativelayout></span>"

Java files

<span style= "FONT-SIZE:18PX;" >package Com.example.seekbardemo;import Android.app.activity;import Android.os.bundle;import Android.view.windowmanager;import Android.widget.seekbar;import Android.widget.SeekBar.OnSeekBarChangeListener; public class Mainactivity extends Activity implements Onseekbarchangelistener {private SeekBar myseekbar;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); myseekbar= (SeekBar) This.findviewbyid (R.ID.SEEKBAR1); Myseekbar.setonseekbarchangelistener (this);} @Overridepublic void onprogresschanged (SeekBar SeekBar, int position, Boolean flag) {//TODO auto-generated method stub}// Method of adjusting brightness private void setscreenbrightness (float num) {windowmanager.layoutparams layoutparams = GetWindow (). GetAttributes ();//Get window Property layoutparams.screenbrightness = num;//num has been divided by 100super.getwindow (). SetAttributes ( Layoutparams); Between 0~1} @Overridepublic void Onstarttrackingtouch (SeekBar arg0) {//TODO auto-generated method stub}//at the end of the drag is to use getprogress to get the current progress value to set the brightness @overridepublic void Onstoptrackingtouch ( SeekBar SeekBar) {//TODO auto-generated method Stubif (Seekbar.getid () ==myseekbar.getid ()) {// Divide progress by 100 and convert to float type setscreenbrightness ((float) seekbar.getprogress ()/100);}}} </span>



after the change



of course, the use of Seekbar components can also be used to control the volume, you can query the relevant API self-attempt





Next forecast: Ratingbar scoring Components

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.