zoukankan      html  css  js  c++  java
  • 常见对话框

    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>

  • 相关阅读:
    Python之路(第四十二篇)线程相关的其他方法、join()、Thread类的start()和run()方法的区别、守护线程
    Python之路(第四十一篇)线程概念、线程背景、线程特点、threading模块、开启线程的方式
    Python之路(第四十篇)进程池
    Python之路(第三十九篇)管道、进程间数据共享Manager
    树莓派系列(第一篇):用python代码查看树莓派的温度、cpu使用率、内存占用情况
    Python之路(第三十八篇) 并发编程:进程同步锁/互斥锁、信号量、事件、队列、生产者消费者模型
    Python之路(第三十七篇)并发编程:进程、multiprocess模块、创建进程方式、join()、守护进程
    Python之路(第三十六篇)并发编程:进程、同步异步、阻塞非阻塞
    Python之路(第三十五篇) 并发编程:操作系统的发展史、操作系统的作用
    Netty入门(二)之PC聊天室
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469810.html
Copyright © 2011-2022 走看看