下面我们就以ProgressDialog实现来写如下代码 [java] package comexampleprogressbar; import javaioIOException; import androidosBundle; import androidappActivity; import androidappProgressDialog; import androidviewMenu; import androidviewView; import androidwidgetButton; public class MainActivity extends Activity { private Button btn; private ProgressDialog pg; @Override protected void onCreate(Bundle savedInstanceState) { superonCreate(savedInstanceState) setContentView(Rlayoutmain) btn=(Button)findViewById(Ridb) btnsetOnClickListener(new ButtonOnClickListener() { public void onClick(View view) { final CharSequence Body=Please wait for a moment; final CharSequence Title=Progress Bar; pg=ProgressDialogshow(MainActivitythis Title Body) new Thread() { public void run() { try { sleep() } catch(Exception e) { eprintStackTrace() } finally { pgdismiss() } } }start() } }) } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present getMenuInflater()inflate(numain menu) return true; } } |