效果:
layout中的xml文件:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="当前进度:" />
- <SeekBar
- android:id="@+id/mySeekBar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="30"
- />
- </LinearLayout>
Activaty.java文件:
- package com.cheng.seekbarproject;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.SeekBar;
- import android.widget.Toast;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.TextView;
- public class SeekBarActivity extends Activity {
- //定义组件
- private TextView mTextView;
- private SeekBar mSeekBar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
- //获得组件
- mTextView = (TextView)findViewById(R.id.myTextView);
- mSeekBar = (SeekBar)findViewById(R.id.mySeekBar);
- //设置进度条的监听器
- OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- //当鼠标抬起的时候触发迟事件
- Toast.makeText(getApplicationContext(), "onStopTrackingTouch", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- //当鼠标点击下的时候触发该事件
- Toast.makeText(getApplicationContext(), "onStartTrackingTouch", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
- //当进度条的值发生改变的时候触发
- mTextView.setText("当前进度:" + progress);
- Toast.makeText(getApplicationContext(), "当前进度:" + progress + "%", Toast.LENGTH_LONG).show();
- }
- };
- //绑定监听
- mSeekBar.setOnSeekBarChangeListener(osbcl);
- }
- @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;
- }
- }