標籤:android
一.等待視窗實現:
記錄一下,有時候我們下載資料的時候為了提高使用者的體驗,加一個等待視窗還是有必要的,有兩種等待視窗:
第一種旋轉的ProgressBar:
主介面:
public class MainActivity extends Activity {private Button button1;private TextView textview1;public AlertDialog selfdialog;private MainActivity main; public Handler mhandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {//顯示TextViewcase 1:Toast.makeText(main, "成功", Toast.LENGTH_SHORT).show();textview1.setVisibility(View.VISIBLE);break;}super.handleMessage(msg);}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);main = this;button1 = (Button) findViewById(R.id.button1);textview1 = (TextView) findViewById(R.id.textview1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {showDialogLoading();}});} //等待dialogprivate void showDialogLoading() {LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);//viewView view = inflater.inflate(R.layout.footer, null);AlertDialog.Builder ad = new AlertDialog.Builder(this);selfdialog = ad.create();selfdialog.setView(view);//屏蔽掉點擊selfdialog.setCancelable(false);selfdialog.show();new Thread() {@Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}Message message = new Message();message.what = 1;main.mhandler.sendMessage(message);//關閉dialogselfdialog.dismiss();}}.start();}}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊我" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="載入完成後顯示我" android:visibility="gone" /></LinearLayout>
footer.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊我" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="載入完成後顯示我" android:visibility="gone" /></LinearLayout>
這樣第一種的等待窗就實現了。
第二種進度條式的等待窗:
主介面:
public class MainActivity extends Activity {private Button button1;private TextView textview1;private TextView textview2;public AlertDialog selfdialog;private MainActivity main; public Handler mhandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {//顯示TextViewcase 1:textview2.setText("正在下載("+msg.obj.toString()+"/10)");break;}super.handleMessage(msg);}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);main = this;button1 = (Button) findViewById(R.id.button1);textview1 = (TextView) findViewById(R.id.textview1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {showDialogLoading();}});} //等待dialogprivate void showDialogLoading() {LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);//viewView view = inflater.inflate(R.layout.footer, null);final ProgressBar progressbar1 = (ProgressBar)view.findViewById(R.id.progressbar1);textview2 = (TextView)view.findViewById(R.id.textview2);AlertDialog.Builder ad = new AlertDialog.Builder(this);selfdialog = ad.create();selfdialog.setView(view);//屏蔽掉點擊selfdialog.setCancelable(false);selfdialog.show();new Thread() {@Overridepublic void run() {//getProgress()獲得當前的進度值//incrementProgressBy()每次增加的量//getMax()獲得這個進度條的範圍的上限int max = progressbar1.getMax();int i = 0;while(true){ if(i > max){break;}try {Thread.sleep(1000);progressbar1.incrementProgressBy(1);Message message = new Message();message.what = 1;message.obj = progressbar1.getProgress();main.mhandler.sendMessage(message);i++;} catch (InterruptedException e) {e.printStackTrace();}}//關閉dialogselfdialog.dismiss();}}.start();}}
footer.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <ProgressBar android:id="@+id/progressbar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dip" android:layout_height="wrap_content" android:max="10" /> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在下載(0/10)" android:textColor="@android:color/black" android:textSize="9sp" /></LinearLayout>
activity_main與上個例子布局一樣。
android(5) 等待視窗