Android中ProgressBar用法简单实例
本文实例讲述了Android中ProgressBar用法。分享给大家供大家参考,具体如下:
在android中会经常用到ProgressBar,下面通过举例来说明如何使用ProgressBar。
importandroid.app.Activity; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.ProgressBar; publicclassA03ActivityextendsActivity{ privateProgressBarrectangle,circle; privateButtonshowProgressBar; privatefinalstaticintSTOP=0x10000; privatefinalstaticintNEXT=0x10001; privateintcount=0; /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); rectangle=(ProgressBar)findViewById(R.id.rectangle); circle=(ProgressBar)findViewById(R.id.circle); showProgressBar=(Button)findViewById(R.id.showProgressBar); rectangle.setIndeterminate(false); circle.setIndeterminate(false); showProgressBar.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub rectangle.setVisibility(View.VISIBLE); circle.setVisibility(View.VISIBLE); rectangle.setMax(100); rectangle.setProgress(0); circle.setProgress(0); Threadt=newThread(newRunnable(){ @Override publicvoidrun(){ //TODOAuto-generatedmethodstub for(inti=0;i<20;i++){ try{ count=(i+1)*5; Thread.sleep(1000); if(count==19){ Messagemsg=newMessage(); msg.what=STOP; handler.sendMessage(msg); break; } else{ Messagemsg=newMessage(); msg.what=NEXT; handler.sendMessage(msg); } }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } }); t.start(); } }); } privateHandlerhandler=newHandler(){ @SuppressWarnings("static-access") publicvoidhandleMessage(Messagemsg){ switch(msg.what){ caseSTOP: rectangle.setVisibility(View.GONE); circle.setVisibility(View.GONE); Thread.currentThread().interrupt(); break; caseNEXT: if(!Thread.currentThread().interrupted()){ rectangle.setProgress(count); circle.setProgress(count); } break; } } }; }
res/layout/main.xml如下所示:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> <ProgressBar android:id="@+id/rectangle" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal" android:visibility="gone" /> <ProgressBar android:id="@+id/circle" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge" android:visibility="gone" /> <Button android:id="@+id/showProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="showProgressBar" /> </LinearLayout>
更多关于Android控件相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。