在Android开发过程中,经常会使用到弹出对话框,Eclipse为我们的开发提供了系统默认的对话框样式,同时也允许我们设置自己的对话框
下面以一个简单的例子实现这个功能
首先,在主界面添加三个按钮,用于操作
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" android:orientation="vertical" android:gravity="center_horizontal" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_gravity="right" android:text="@string/main_btn1" android:textColor="#fff" android:textSize="18sp" android:background="#0a0" android:onClick="main_btn1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_gravity="right" android:text="@string/main_btn2" android:textColor="#fff" android:textSize="18sp" android:background="#0a0" android:onClick="main_btn2" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_gravity="right" android:text="@string/main_btn3" android:textColor="#fff" android:textSize="18sp" android:background="#0a0" android:onClick="main_btn3" /> </LinearLayout>
dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ff0"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:text="@string/hello_world" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:text="自定义对话框效果" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void main_btn1(View view) {//弹出系统对话框 Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("标题")//设置标题 .setMessage("内容:系统对话框效果")//设置内容 .setPositiveButton("确定", //设置确定按钮 new DialogInterface.OnClickListener()//设置事件监听 { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您点击了确定按钮", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //点击"取消"按钮之后此对话框消失 dialog.cancel(); } }) .create(); dialog.show();// 显示对话框 } public void main_btn2(View view) {//弹出自定义对话框 LayoutInflater factory = LayoutInflater.from(MainActivity.this); //得到自定义对话框 final View dialogView = factory.inflate(R.layout.dialog, null); //创建对话框 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("标题")//设置标题 //.setMessage("内容")//设置内容 .setView(dialogView)//设置视图 .setPositiveButton("确定", //设置确定按钮 new DialogInterface.OnClickListener()//设置事件监听 { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您点击了确定按钮", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //点击"取消"按钮之后此对话框消失 dialog.cancel(); } }) .create(); dialog.show();// 显示对话框 } public void main_btn3(View view) { final ProgressDialog dialog = ProgressDialog.show(MainActivity.this,"请等待...","正在测试...",true); new Thread() { public void run() { try { sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { dialog.dismiss(); } } }.start(); } }
主界面
点击第一个按钮
点击第二个按钮
点击第三个按钮
OK,这样就完成啦!
源码下载