Dialog 的6中提示方式

1.在res/ layout 包先创建 main.xml

  内容如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical">
        <ImageView
         android:src="@drawable/jay"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
        />
        <Button android:id="@+id/button0"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="确定取消信息框"/>
        <Button android:id="@+id/button1"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="多个按钮信息框"/>
        <Button android:id="@+id/button2"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="列表框"/>
        <Button android:id="@+id/button3"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="进度条框"/>
        <Button android:id="@+id/button4"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="单项选择列表框"/>
        <Button android:id="@+id/button5"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="多项选择列表框"/>
        <Button android:id="@+id/button6"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="自定义布局"/>
        <Button android:id="@+id/button7"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:text="读取进度框"/>
    </LinearLayout>
</ScrollView>

2. 在res/layout包先  在增加test.xml文件

内容如下:

   

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:id="@+id/dialog"> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:id="@+id/dialogname">

<TextView android:layout_height="wrap_content"    android:layout_width="wrap_content"   android:id="@+id/tvUserName"   android:text="姓名:" /> <EditText android:layout_height="wrap_content"   android:layout_width="wrap_content"   android:id="@+id/etUserName"   android:minWidth="200dip"/> </LinearLayout>  <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:id="@+id/dialognum"  android:layout_below="@+id/dialogname" >   <TextView android:layout_height="wrap_content"    android:layout_width="wrap_content"   android:id="@+id/tvPassWord"   android:text="密码:" /> <EditText android:layout_height="wrap_content"   android:layout_width="wrap_content"   android:id="@+id/etPassWord"   android:minWidth="200dip"/>  </LinearLayout>    </RelativeLayout>

3. 在res/values包下在创建string.xml

   内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainDialog!</string>
    <string name="app_name">dialog窗口大合集</string>
</resources>

4. 在创建类 

  内容如下:

package com.hy;

import java.util.ArrayList;

import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

/**  * dialog 大集合代码. author : lhy  */

public class HyActivity extends Activity implements Runnable {

 /** 确定取消信息框 **/  private static final int DIALOG_0 = 1;

 /** 多个按钮信息框 **/  private static final int DIALOG_1 = 2;

 /** 列表框 **/  private static final int DIALOG_2 = 3;

 /** 进度条框 **/  private static final int DIALOG_3 = 4;

 /** 单项选择列表框 **/  private static final int DIALOG_4 = 5;

 /** 多项选择列表框 **/  private static final int DIALOG_5 = 6;

 /**自定义布局 **/     private static final int DIALOG_6 = 7;    

/**读取进度框 **/     private static final int DIALOG_7 = 8;  final String[] mItems = { "item0", "item1", "itme2", "item3", "itme4",    "item5", "item6" };  

private static final int MAX_PROGRESS = 100;  

private ProgressDialog mProgressDialog = null;

 int mSingleChoiceID = -1;

 ArrayList<Integer> MultiChoiceID = new ArrayList<Integer>();

 public void onCreate(Bundle savedInstanceState) {   

super.onCreate(savedInstanceState);

  setContentView(R.layout.main);   

Button button0 = (Button) findViewById(R.id.button0);  

 button0.setOnClickListener(new OnClickListener() {   

 public void onClick(View v) {     

CreatDialog(DIALOG_0);    }   

});   

Button button1 = (Button) findViewById(R.id.button1);   

button1.setOnClickListener(new OnClickListener() {   

 public void onClick(View v) {  

  CreatDialog(DIALOG_1);    }

  });

  Button button2 = (Button) findViewById(R.id.button2);   

button2.setOnClickListener(new OnClickListener() {   

 public void onClick(View v) {    

 CreatDialog(DIALOG_2);    }

  });   

Button button3 = (Button) findViewById(R.id.button3);   

button3.setOnClickListener(new OnClickListener() {  

  public void onClick(View v) {

    CreatDialog(DIALOG_3);     

mProgressDialog.setProgress(0);    }  

 });  

 Button button4 = (Button) findViewById(R.id.button4);

  button4.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {     

CreatDialog(DIALOG_4);    }  

 });   

Button button5 = (Button) findViewById(R.id.button5);  

 button5.setOnClickListener(new OnClickListener() {   

 public void onClick(View v) {    

 CreatDialog(DIALOG_5);    }

  });

        Button button6 = (Button) findViewById(R.id.button6);     

    button6.setOnClickListener(new OnClickListener() {          

   public void onClick(View v) {       

   CreatDialog(DIALOG_6);             }      

   });        

Button button7 = (Button) findViewById(R.id.button7);       

  button7.setOnClickListener(new OnClickListener() {        

     public void onClick(View v) {          CreatDialog(DIALOG_7);             }         });     }

 // 根据判断值,执行.  

public void CreatDialog(int id) {  

 AlertDialog.Builder builder = new AlertDialog.Builder(HyActivity.this);  

 switch (id) {  

 case DIALOG_0:   

 builder.setIcon(R.drawable.icon);  

  builder.setTitle("你确定要离开吗?");   

 builder.setPositiveButton("确定",      new DialogInterface.OnClickListener() {       public void onClick(DialogInterface dialog, int which) {        showDialog("你选择了确定");       }      });   

 builder.setNegativeButton("取消",      new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int which) {       

 // TODO Auto-generated method stub     

   showDialog("你选择了取消");       }     

 });    break;  

 case DIALOG_1:    

builder.setTitle("投票");    

builder.setMessage("你认为什么样的内容能吸引你!");   

 builder.setPositiveButton("有趣的",      new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int which) {        // TODO Auto-generated method stub        showDialog("你选择了有趣的");       }      });    

builder.setNeutralButton("有思想的",      new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int which) {      

  // TODO Auto-generated method stub       

 showDialog("你选择了有思想的");       }      

});   

 builder.setNegativeButton("主题强的",      new DialogInterface.OnClickListener() {       public void onClick(DialogInterface dialog, int which) {       

 // TODO Auto-generated method stub    

    showDialog("你选择了主题强的");       }    

  });   

 builder.setNegativeButton("主题强的",      new DialogInterface.OnClickListener() {       public void onClick(DialogInterface dialog,         int whichButton) {     

   showDialog("你选择了主题强的");       }     

 });    break;   

case DIALOG_2:  

  builder.setTitle("列表选择框");   

 builder.setItems(mItems, new DialogInterface.OnClickListener() {    

 public void onClick(DialogInterface dialog, int which) {  

    // 点击后弹出窗口选择了第几项  

    showDialog("你选择的id为" + which + " , " + mItems[which]);     }    });

   break;   case DIALOG_3:   

 mProgressDialog = new ProgressDialog(HyActivity.this);    

mProgressDialog.setIcon(R.drawable.icon);   

 mProgressDialog.setTitle("进度条窗口");  

  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  

  mProgressDialog.setMax(MAX_PROGRESS);   

 mProgressDialog.setButton("确定",      new DialogInterface.OnClickListener() {       public void onClick(DialogInterface dialog, int which) {

      }      });    

mProgressDialog.setButton("取消",      new DialogInterface.OnClickListener() {       public void onClick(DialogInterface dialog, int which) {

      }      });   

 mProgressDialog.show();    

new Thread(this).start();    break;

  case DIALOG_4:    

mSingleChoiceID = -1;    

builder.setIcon(R.drawable.icon);   

 builder.setTitle("单项选择");    builder.setSingleChoiceItems(mItems, 0,      new DialogInterface.OnClickListener() {     

  public void onClick(DialogInterface dialog,         int whichButton) {    

    mSingleChoiceID = whichButton;      

  showDialog("你选择的id为" + whichButton + " , "          + mItems[whichButton]);       }    

  });  

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

  public void onClick(DialogInterface dialog,         int whichButton) {     

   if (mSingleChoiceID > 0) {         

showDialog("你选择的是" + mSingleChoiceID);        }    

   }      });   

 builder.setNegativeButton("取消",      new DialogInterface.OnClickListener() {      

 public void onClick(DialogInterface dialog,         int whichButton) {

      }    

  });    break;   

case DIALOG_5:      

MultiChoiceID.clear();     

  builder.setIcon(R.drawable.icon);             

builder.setTitle("多项选择");             

builder.setMultiChoiceItems(mItems,                     new boolean[]{false, false, false, false, false, false, false},                      new DialogInterface.OnMultiChoiceClickListener() {            

              public void onClick(DialogInterface dialog, int whichButton,                                  boolean isChecked) {   

                          if(isChecked) {                                

                            MultiChoiceID.add(whichButton);                                

                           showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);              

               }else {                                 MultiChoiceID.remove(whichButton);                             }                                                       }                      });             

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

       public void onClick(DialogInterface dialog, int whichButton) {                

      String str = "";                      int size = MultiChoiceID.size();                  

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

   str+= mItems[MultiChoiceID.get(i)] + ", ";                      }          

            showDialog("你选择的是" + str);                  }              });       

       builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   

              public void onClick(DialogInterface dialog, int whichButton) {

                 }          

    });       break;  

 case DIALOG_6:            

LayoutInflater factory = LayoutInflater.from(this);          

   final View textEntryView = factory.inflate(R.layout.test, null);           

   builder.setIcon(R.drawable.icon);          

       builder.setTitle("自定义输入框");         

        builder.setView(textEntryView);     

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

          public void onClick(DialogInterface dialog, int whichButton) {              

    EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);         

         EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);           

       showDialog("姓名 :"  + userName.getText().toString()  + "密码:" + password.getText().toString() );                     }                 });                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {                  

   public void onClick(DialogInterface dialog, int whichButton) {

                    }               

  });      break;  

 case DIALOG_7:      

mProgressDialog = new ProgressDialog(this);    

   mProgressDialog.setTitle("读取ing");      

mProgressDialog.setMessage("正在读取中请稍候");    

   mProgressDialog.setIndeterminate(true);    

   mProgressDialog.setCancelable(true);     

  mProgressDialog.show();     

  return;   }  

 builder.create().show();  }

 // 获取button  public void showDialog(String str) {

  new AlertDialog.Builder(HyActivity.this).setMessage(str).show();  }

 public void run() {

  int progress = 0;   

while (progress < MAX_PROGRESS) {   

 try {     Thread.sleep(1000);     progress++;     mProgressDialog.incrementProgressBy(1);// 每一秒后,进度条就会增加1    }

catch (InterruptedException e) {     e.printStackTrace();    }   }

 } }

原文地址:https://www.cnblogs.com/yongwuqing/p/3933919.html