Android基础-----进度条(ProgressBar手动实现)

来源:互联网 发布:网络中立规则 编辑:程序博客网 时间:2024/05/21 22:44

                                              ProgressBar

ProgressBar 是android的自带的进度条。


基本方法就不说了,直接贴代码,并加入点自己的理解。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="20dp"    tools:context="${relativePackage}.${activityClass}" >    <ProgressBar        android:id="@+id/pb"        style="@android:style/Widget.ProgressBar.Horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:progress="30"        android:max="100"        android:layout_marginBottom="50dp"     />    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/pb"        android:layout_marginLeft="65dp"        android:onClick="add"        android:text="增加" />    <Button        android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/btn1"        android:layout_alignBottom="@+id/btn1"        android:layout_toRightOf="@+id/btn1"        android:onClick="jianxiao"        android:text="减少" /></RelativeLayout>
/*@android:style/Widget.ProgressBar.Horizontal是系统自带的f*/


/*MainActivity.java*/import android.view.View;import android.widget.ProgressBar;public class MainActivity extends Activity {private int process = 0;private ProgressBar pb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//点击增加按钮进度条加5 public void add(View v) {pb = (ProgressBar) findViewById(R.id.pb);                  
               //防止加到100以外
 if (pb.getProgress() < 95) {process = pb.getProgress() + 5;pb.setProgress(process);} else {pb.setProgress(100);}}
//点击增加按钮进度条减5
 public void jianxiao(View v) {pb = (ProgressBar) findViewById(R.id.pb);
//防止减到负值出错
  if (pb.getProgress() > 5) {process -= 5;pb.setProgress(process);} else {pb.setProgress(0);}}}
 
      
android:onClick是安卓里面链接到方法的一个属性,比较常用,但是它有个规范就是

android:onClick里面的方法名一定要对得到且不用加“()”,定义时必须是public类型且方法里面的参数类型必须是(View xxxx)


结果截图:



















1 0