效果图:(可自由滑动)
<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:paddingBottom="@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:id="@+id/firstSeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> </RelativeLayout>
package com.jkxqj.helloandroid; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity { private SeekBar seekBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar)findViewById(R.id.firstSeekBar); seekBar.setProgress(30); seekBar.setSecondaryProgress(50); SeekBarListener listener = new SeekBarListener(); seekBar.setOnSeekBarChangeListener(listener); } class SeekBarListener implements OnSeekBarChangeListener{ /** * seekBar 该对象指的是触发了监听器的SeekBar对象 * progress 指的是当前SeekBar的进度 */ @Override public void onProgressChanged(SeekBar SeekBar, int progress, boolean fromUser) { System.out.println("progress:" + progress + ",fromUser:" + fromUser); } @Override public void onStartTrackingTouch(SeekBar seekBar) { System.out.println("onStart"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { System.out.println("onStop"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }