基本Dialog的使用
-
布局
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="click1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定取消对话框" /> <Button android:onClick="click2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选对话框" /> <Button android:onClick="click3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选对话框" /> <Button android:onClick="click4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="进度对话框" /> <Button android:onClick="click5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="进度条对话框" /> </LinearLayout>
-
代码
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //确定取消对话框 public void click1(View view) { //1.创建Buidler对象 AlertDialog.Builder builder = new AlertDialog.Builder(this); //2.设置标题 builder.setTitle("警告"); //3.设置提示信息 builder.setMessage("欲练此功,必先自宫"); //4.设置确定按钮 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "啊。。。", 0).show(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "容我三思", 0).show(); } }); AlertDialog dialog = builder.create(); //记得显示对话框 dialog.show(); } int checkedItem = 0;//默认选择条目下标,If -1 no items are checked //单选对话框 public void click2(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择性别"); checkedItem = -1; final String[] items = {"男","女","保密"};//单选的范围相应数组 builder.setSingleChoiceItems(items, checkedItem, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { checkedItem = which;//which就是选择项目下标 } }); //4.设置确定按钮 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "性别选择了: " + items[checkedItem], 0).show(); } }); builder.show();//直接用builder对象调用show()方法 } //进度对话框 public void click4(View v) { final ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setMessage("正在加载"); dialog.show(); new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(3000); dialog.dismiss(); } }).start(); } //进度条对话框 public void click5(View v) { final ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setMax(10);//设置最大值 //设置样式STYLE_HORIZONTAL,默认STYLE_SPINNER 圆环 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); new Thread(new Runnable() { @Override public void run() { for(int i = 0;i <= 10;i++){ dialog.setProgress(i); SystemClock.sleep(500); } //当任务完成需要消除对话框 dialog.dismiss(); } }).start(); dialog.show(); } //多选对话框 public void click3(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择兴趣"); final String[] items = {"吃饭","睡觉","写代码","打游戏"};//可多选的范围数组 final boolean [] checkedItems = {true, true,false,false};//选择了哪个项目 builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { //参数2 选择哪一项 //参数3 这个项目是选上还是没选上 @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which] = isChecked; } }); //4.设置确定按钮 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for(int i = 0;i < checkedItems.length;i++){ //如果某一项被选择 if(checkedItems[i]){ result += items[i] + " "; } } Toast.makeText(MainActivity.this, "兴趣选择了: " + result, 0).show(); } }); builder.show();//直接用builder对象调用show()方法 } }