下边展示了如何调用系统的dialog
首先在layout添加三种按钮, 分辨显示3种不同的activity
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="@+id/send_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_wait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="openDialog"
android:text="@string/btn_wait" />
<Button
android:id="@+id/btn_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_wait"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:onClick="openDialog"
android:text="@string/btn_dialog" />
<Button
android:id="@+id/btn_dprogress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_wait"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/btn_dprogress"
android:onClick="openDialog"/>
</RelativeLayout>
下边是具体实现
//DisplayMessageActivity.java
package com.example.hello;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayMessageActivity extends Activity {
CharSequence[] items = { "Google", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean[items.length];
private ProgressDialog PresDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
String msg = this.getIntent()
.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView TxtSendMsg = (TextView) this.findViewById(R.id.send_msg);
TxtSendMsg.setText(msg);
this.getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
public void openDialog(View view) {
this.showDialog(view.getId());
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case R.id.btn_dprogress:
showProgressDialog();
break;
case R.id.btn_wait:
final ProgressDialog dialog = ProgressDialog.show(this,
"Doing something", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
try {
// ---simulate doing something lengthy---
Thread.sleep(5000);
// ---dismiss the dialog---
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.btn_dialog:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle(R.string.app_name)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(),
"Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked!"
: " unchecked!"),
Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
private ProgressDialog getProgressDialog() {
// TODO Auto-generated method stub
if (null != this.PresDialog) {
return this.PresDialog;
}
ProgressDialog Dialog = new ProgressDialog(this);
Dialog.setTitle("Downloading files...");
Dialog.setIcon(R.drawable.ic_launcher);
Dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int btnInt) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "OK clicked!",
Toast.LENGTH_SHORT).show();
}
});
Dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "cancel clicked",
Toast.LENGTH_SHORT).show();
}
});
this.PresDialog = Dialog;
return Dialog;
}
private void showProgressDialog() {
final ProgressDialog PresDialog = this.getProgressDialog();
PresDialog.setProgress(0);
PresDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
for (int i = 1; i <= 15; i++) {
Thread.sleep(1000);
PresDialog.incrementProgressBy((int) (100 / 15));
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PresDialog.dismiss();
}
}).start();
}
}