AndroidGUI07:SeekBar的常用技巧

来源:互联网 发布:mysql 修改复合主键 编辑:程序博客网 时间:2024/05/01 05:08

SeekBar其实也是一种ProgressBar,它是ProgressBar的间接派生类,因此ProgressBar可以用的方法,SeekBar都可以用。

 

1.    在布局文件(main.xml)中,增加界面元素声明如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="horizontal"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   >

 

         <SeekBar

                   android:id="@+id/seekbar"

                   android:layout_width="240px"

                   android:layout_height="wrap_content"

                   android:max="500"

         />

                 

         <TextView

                   android:id="@+id/seekbar_tv"

                   android:layout_width="80px"

                   android:layout_height="wrap_content"

                   android:text=""

         />

        

</LinearLayout>

 

2.    Activity所对应的代码:

public class ControlSeekBar extends Activity

implements

OnSeekBarChangeListener

{

         private SeekBar sb;

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        sb =(SeekBar)findViewById(R.id.seekbar);

        sb.setOnSeekBarChangeListener(this);

       

        sb.setProgress(100);

    }

   

         public voidonProgressChanged(SeekBar sb, int progress, boolean fromUser)

         {

                   ((TextView)findViewById(R.id.seekbar_tv)).setText(" " +progress);

         }

        

         public voidonStartTrackingTouch(SeekBar seekBar)

         {

                   //Notification that the user has started a touch gesture.

                   // Clientsmay want to use this to disable advancing the seekbar.             

         }

        

         public voidonStopTrackingTouch(SeekBar seekBar)

         {

                   //Notification that the user has finished a touch gesture.

                   // Clientsmay want to use this to re-enable advancing the seekbar.

         }

}

 

运行结果

拖动thumb之前


拖动thumb400处:


原创粉丝点击