android之实现ProgressBar进度条组件:
(注意:横向那个进度条要android4.0以上版本支持,也就是最低(android:minSdkVersion="14")支持)
布局:layout/activity_main.xml
<LinearLayout 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:orientation="vertical"
>
<ProgressBar
android:id="@+id/pb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar"/>
<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"/>
<Button
android:layout_marginTop="50dp"
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="download file"
/>
</LinearLayout>
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);
}
}
效果:
本文出自 “追随心的彼岸” 博客,谢绝转载!
原文地址:http://qa962839575.blog.51cto.com/6819179/1587117