进度条简单demo

来源:互联网 发布:汪精卫为何叛变知乎 编辑:程序博客网 时间:2024/06/05 03:07

布局非常简单。

<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"    tools:context="com.example.loadprogressbardemo.MainActivity" >    <ProgressBar         android:id="@+id/bar"        android:layout_width="match_parent"        android:layout_height="wrap_content"         style="?android:attr/progressBarStyleHorizontal"        />        <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="加载" /></LinearLayout>

逻辑代码

package com.example.loadprogressbardemo;import android.app.Activity;import android.os.Bundle;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;import android.widget.Toast;public class MainActivity extends Activity {private Button btn;private ProgressBar bar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bar = (ProgressBar)findViewById(R.id.bar);btn = (Button)findViewById(R.id.btn);btn.setOnClickListener(new ButtonListener());}class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.getId() == R.id.btn){Thread t = new LoadThread();t.start();}}}class LoadThread extends Thread{public void run(){for(int i=0;i<100;i++){try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}bar.setProgress(bar.getProgress()+1);}}}}


效果图:




0 0
原创粉丝点击