main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SeekBar
android:thumb="@drawable/my_thumb"
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
my_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/select" android:state_pressed="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/select" android:state_focused="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/select" android:state_selected="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/normal"/>
</selector>
MainActivity.java
package com.imooc.android_seekbar;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private SeekBar seekBar;
private TextView tv1;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seekBar=(SeekBar) findViewById(R.id.seekBar);
tv1=(TextView) findViewById(R.id.tv1);
tv2=(TextView) findViewById(R.id.tv2);
seekBar.setOnSeekBarChangeListener(this);
}
//数值改变
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
tv1.setText("正在拖动");
tv2.setText("当前数值:"+progress);
}
//开始拖动
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
tv1.setText("开始拖动");
}
//停止拖动
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
tv1.setText("停止拖动");
}
}
演示效果:
seekbar.gif
网友评论