Android常用高级组件之滑块与进度条
1.ProgressBar类简介:该类同样位于android.widget包下,但它继承自View,主要用于显示一些操作的进度。
2.SeekBar类简介:该类继承自ProgressBar,是用来接收用户输入的控件。SeekBar类似于拖拉条,可以直观地先显示用户需要的数据,
常用于声音调节等场合。
3.案例:
1).布局文件的编写
main.xml文件布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/ProgressBar01" android:max="100" android:progress="20" style="@android:style/Widget.ProgressBar.Horizontal" android:padding="@dimen/padding_medium" /> <!-- 添加一个进度条控件 --> <SeekBar android:id="@+id/SeekBar01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="20"/> <!-- 添加一个滑块控件 --> </LinearLayout>
2).逻辑代码的编写
package com.example.sample_5_5; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.ProgressBar; import android.widget.SeekBar; public class Sample_5_5 extends Activity { final static double MAX = 100;//SeekBar、ProgressBar的最大值 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //普通拖拉条被拉动的处理代码 SeekBar sb = (SeekBar)this.findViewById(R.id.SeekBar01); sb.setOnSeekBarChangeListener( //添加监听 new SeekBar.OnSeekBarChangeListener()//创建一个监听类 { public void onStopTrackingTouch(SeekBar seekBar) { } public void onStartTrackingTouch(SeekBar seekBar) { } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { ProgressBar pb = (ProgressBar)findViewById(R.id.ProgressBar01); SeekBar sb = (SeekBar)findViewById(R.id.SeekBar01); pb.setProgress(sb.getProgress());//设置进度 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
3).运行结果略.