用ProgressDialog来缓冲Activity的切换

先来看看主要的Activity部分:

package com.misoo.kx03;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class ac01 extends Activity implements OnClickListener {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private Button btn, btn2;
private ProgressDialog progressDialog = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this);
btn.setText("load map");
btn.setOnClickListener(this);
layout.addView(btn, new LinearLayout.LayoutParams(WC, WC));
btn2 = new Button(this);
btn2.setText("Exit");btn2.setOnClickListener(this);
layout.addView(btn2, new LinearLayout.LayoutParams(WC, WC));
setContentView(layout);
}
public void onClick(View v){
if (v == btn){//主要的缓冲过程
progressDialog=ProgressDialog.show(this,"please wait…","Loading YourMap…",true);
new Thread(){
public void run(){
try{
sleep(8000);//最好这里用一个同步线程,监听返回结果,更灵活
Intent intent = new Intent();
intent.setClass(ac01.this, DispActivity.class);
startActivity(intent);
}
catch(Exception e)
{ e.printStackTrace(); }
progressDialog.dismiss();
}
}.start();
setTitle("this main thread");
}
if(v.equals(btn2)) this.finish();
}}

撰寫Activity 的子類別:DispActivity

package com.misoo.kx03;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DispActivity extends Activity implements OnClickListener {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
public static final int EXIT_ID = Menu.FIRST + 2;
private TextView tx;@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Second Screen");
tx = new TextView(this);
tx.setText("Here Is Your Map.");
setContentView(tx, new LinearLayout.LayoutParams(WC, WC));
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, EXIT_ID, 0, "Exit");
return true;
}
public void onClick(View v)
{ setTitle("yes"); }
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EXIT_ID:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}}
posted @ 2012-08-10 16:17  Tammie-锴  阅读(1012)  评论(0编辑  收藏  举报