常见对话框

public class MainActivity extends Activity { 

 

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

 }

 

 public void click1(View view) {

  AlertDialog.Builder builder = new Builder(this);

  builder.setTitle("普通对话框").setIcon(R.drawable.ic_launcher)

    .setPositiveButton("确定", new OnClickListener() {

     @Override

     public void onClick(DialogInterface arg0, int arg1) {

      Toast.makeText(MainActivity.this, "确定", 0).show();

     }

    }).setNegativeButton("取消", new OnClickListener() {

     @Override

     public void onClick(DialogInterface dialog, int which) {

      Toast.makeText(MainActivity.this, "取消", 0).show();

     }

    }).show();

 }

 

 public void click2(View view) {

  final String[] items = new String[] { "条目1", "条目2", "条目3" };

  int checkedItem = 1;

  AlertDialog.Builder builder = new Builder(this);

  builder.setTitle("单选对话框")

    .setIcon(R.drawable.ic_launcher)

    .setSingleChoiceItems(items, checkedItem,

      new OnClickListener() {

 

       @Override

       public void onClick(DialogInterface dialog,

         int which) {

        Toast.makeText(MainActivity.this,

          items[which] + "被选择", 0).show();

        dialog.dismiss();

       }

      }).setPositiveButton("确定", new OnClickListener() {

     @Override

     public void onClick(DialogInterface arg0, int arg1) {

      Toast.makeText(MainActivity.this, "确定", 0).show();

     }

    }).setNegativeButton("取消", new OnClickListener() {

     @Override

     public void onClick(DialogInterface dialog, int which) {

      Toast.makeText(MainActivity.this, "取消", 0).show();

     }

    }).show();

 

 }

 

 public void click3(View view) {

  final String[] items = new String[] { "条目1", "条目2", "条目3" };

  boolean[] checkedItems = { true, false, false };

  AlertDialog.Builder builder = new Builder(this);

  builder.setTitle("多选对话框")

    .setIcon(R.drawable.ic_launcher)

    .setMultiChoiceItems(items, checkedItems,

      new OnMultiChoiceClickListener() {

       @Override

       public void onClick(DialogInterface dialog,

         int which, boolean isChecked) {

        Toast.makeText(MainActivity.this,

          items[which] + ":" + isChecked, 0)

          .show();

 

       }

      })

 

    .setPositiveButton("确定", new OnClickListener() {

     @Override

     public void onClick(DialogInterface arg0, int arg1) {

      Toast.makeText(MainActivity.this, "确定", 0).show();

     }

    }).setNegativeButton("取消", new OnClickListener() {

     @Override

     public void onClick(DialogInterface dialog, int which) {

      Toast.makeText(MainActivity.this, "取消", 0).show();

     }

    }).show();

 

 }

 

 public void click4(View view) {

  ProgressDialog pd = new ProgressDialog(this);

  pd.setTitle("提示");

  pd.setMessage("正在玩命加载中.......");

  // 设置进度条样式,默认为圆形进度对话框 STYLE_SPINNER

  pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);

  // 设置返回键取消对话框,默认为true

  pd.setCancelable(true);

  pd.show();

 

 }

 

 public void click5(View view) {

  final ProgressDialog pd = new ProgressDialog(this);

  pd.setTitle("提示");

  pd.setMessage("正在玩命加载中.......");

  // 设置进度条样式

  pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

  // 设置返回键取消对话框

  pd.setCancelable(false);

  pd.setMax(100);

  pd.show();

  new Thread() {

   public void run() {

    for (int i = 0; i <= 100; i++) {

     pd.setProgress(i);

     try {

      sleep(100);

     } catch (InterruptedException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

     }

    }

    pd.dismiss();

   

   };

 

  }.start();

 

 }

 

}

 

<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" >

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click1"

        android:text="普通对话框" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click2"

        android:text="单选对话框" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click3"

        android:text="多选对话框" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click4"

        android:text="进度对话框" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="click5"

        android:text="进度条对话框" />

 

</LinearLayout>

posted on 2013-07-03 10:02  Freedom000  阅读(121)  评论(0编辑  收藏  举报

导航