android之实现ProgressBar进度条组件

android之实现ProgressBar进度条组件:

(注意:横向那个进度条要android4.0以上版本支持,也就是最低(android:minSdkVersion="14")支持)

布局:layout/activity_main.xml

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>


android:id="@+id/pb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar"/>
android:id="@+id/pb2"
android:layout_width="match_parent"
android:layout_height="50dp"
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100"
android:progress="30"/>
android:layout_marginTop="50dp"
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="download file"
/>

java代码:

package com.litsoft.main;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Process;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;


public class MainActivity extends ActionBarActivity {
private ProgressBar gressBar;
private Button bt ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setListener();
}


private void setListener() {
// TODO Auto-generated method stub
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
for (int i=0;i<100;i++){
gressBar.setProgress(i);
SystemClock.sleep(2000);
}
}
}.start();
}
});
}


private void init() {
// TODO Auto-generated method stub
gressBar = (ProgressBar) findViewById(R.id.pb2);
gressBar.setProgress(0);
bt = (Button) findViewById(R.id.bt1);
}


}

效果:

android之实现ProgressBar进度条组件_第1张图片

你可能感兴趣的:(android)