安卓控件使用系列20:ProgressBar进度条控件的使用方法

来源:互联网 发布:阿里云ecs搭建ss 编辑:程序博客网 时间:2024/05/17 04:20

安卓中的进度条是经常使用的控件之一,下面我们来一起分享一下各种类型的进度条的使用。

这个例子显示的是小中大圆形进度条和水平进度条,通过按下增加进度和减少进度来控制进度条上进度的增加和减少。

整体思路:通过设置ProgressBar的style属性来控制进度条的显示类型,是圆形还是水平,是大还是小,不进行设置的话默认为是中型圆形进度条的显示效果。在活动中设置进度条的初始属性和刻度,点击按钮的OnClick事件中,获取当前的进度进行增加或减少。

activity_main.xml文件:

 <LinearLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="小型圆形进度条" />    <ProgressBar         android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="?android:attr/progressBarStyleSmallTitle"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="中型圆形进度条"        />    <ProgressBar         android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="大型圆形进度条"        />    <ProgressBar         android:layout_width="wrap_content"        android:layout_height="wrap_content"         style="?android:attr/progressBarStyleLarge"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="水平进度条"        />    <ProgressBar         android:layout_width="fill_parent"        android:layout_height="wrap_content"        style="?android:attr/progressBarStyleHorizontal"        android:max="100"        android:progress="30"        />    <ProgressBar         android:id="@+id/progressbar"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        style="?android:attr/progressBarStyleHorizontal"        android:max="100"        android:progress="30"        android:secondaryProgress="60"        android:layout_marginTop="20dp"        /></LinearLayout><LinearLayout     android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    >    <Button         android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:text="增加进度"        />     <Button          android:id="@+id/button2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="20dp"         android:text="减少进度"         /></LinearLayout>

MainActivity.java文件:

   public class MainActivity extends Activity implements OnClickListener{     private ProgressBar progressBar;   private Button button1,button2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//如何设置窗口有刻度的效果requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.activity_main);//这句话必须在上面两句话的下面,否则报错。progressBar=(ProgressBar)findViewById(R.id.progressbar);setProgressBarVisibility(true);setProgressBarIndeterminate(true);setProgress(3500);//设置刻度为3500button1=(Button)findViewById(R.id.button1);button2=(Button)findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}@Overridepublic 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;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button1:progressBar.setProgress((int)(progressBar.getProgress()*1.2));progressBar.setSecondaryProgress((int)(progressBar.getSecondaryProgress()*1.2));break;case R.id.button2:progressBar.setProgress((int)(progressBar.getProgress()*0.8));progressBar.setSecondaryProgress((int)(progressBar.getSecondaryProgress()*0.8));break;}}}


0 0
原创粉丝点击