Activity多功能按钮――基础编

 

 

 

 

 

  
  
  
  
  1. package com.smart; 
  2.  
  3. import android.app.Activity; 
  4. import android.app.AlertDialog; 
  5. import android.app.Dialog; 
  6. import android.content.DialogInterface; 
  7. import android.os.Bundle; 
  8. import android.util.Log; 
  9. import android.view.View; 
  10. import android.view.View.OnClickListener; 
  11. import android.widget.Button; 
  12. import android.widget.ListView; 
  13. //继承OnClickListener 
  14. public class Main extends Activity implements OnClickListener { 
  15. //固定的静态变量, 
  16.     private final int DELETE_FILE = 1
  17.     private final int SIMPLE_LIST = 2
  18.     private final int SINGLE_CHOICE_LIST = 3
  19.     private final int MULTI_LIST = 4
  20.     private ListView lv = null
  21.     //定义一个字符串数组 
  22.     private String[] provinces = new String[] { "中国", "泰国", "美国", "法国", 
  23.             "新加坡", "英国" }; 
  24.     //按钮 
  25.     private ButtonOnClick buttonOnClick = new ButtonOnClick(1); 
  26.     @Override//事件点击 
  27.     public void onClick(View v) { 
  28.  
  29.         //开关选择 
  30.         switch(v.getId()){ 
  31.         case R.id.btnDeleteFile: 
  32.             showDialog(DELETE_FILE); 
  33.             break; 
  34.         case R.id.btnSimpleList: 
  35.             showDialog(SIMPLE_LIST); 
  36.             break; 
  37.         case R.id.btnChoiceList: 
  38.             showDialog(SINGLE_CHOICE_LIST); 
  39.             break; 
  40.         case R.id.btnMultiChoiceList: 
  41.             showDialog(MULTI_LIST); 
  42.             break; 
  43.         case R.id.btnRemoveDialog: 
  44.             removeDialog(DELETE_FILE); 
  45.             removeDialog(SIMPLE_LIST); 
  46.             removeDialog(SINGLE_CHOICE_LIST); 
  47.             removeDialog(MULTI_LIST); 
  48.             break; 
  49.              
  50.         } 
  51.     } 
  52.     @Override 
  53.     protected Dialog onCreateDialog(int id) { 
  54.         Log.d("dialog",String.valueOf(id)); 
  55.         //用来判断选择的目标。如果没有选择,并提示出来,选择中了,也显示结果。详看代码 
  56.         switch(id){ 
  57.         case DELETE_FILE: 
  58.             return new AlertDialog.Builder(this).setIcon(R.drawable.question).setTitle("是否删除文件").setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  59.                  
  60.                 @Override 
  61.                 public void onClick(DialogInterface dialog, int which) { 
  62.                     new AlertDialog.Builder(Main.this).setMessage("文件已经被删除").create().show(); 
  63.                 } 
  64.             }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 
  65.                  
  66.                 @Override 
  67.                 public void onClick(DialogInterface dialog, int which) { 
  68.                     new AlertDialog.Builder(Main.this).setMessage("您已经选择了取消,文件未删除").create().show(); 
  69.                 } 
  70.             }).create(); 
  71.              
  72.              
  73.         case SIMPLE_LIST: 
  74.         return new AlertDialog.Builder(this).setTitle("选择国家").setItems(provinces, new DialogInterface.OnClickListener() { 
  75.              
  76.             @Override 
  77.             public void onClick(DialogInterface dialog, int which) { 
  78.                 final AlertDialog ad=new AlertDialog.Builder(Main.this).setMessage("您已经选择了"+which+":"+provinces[which]).show(); 
  79.              
  80.             android.os.Handler hander=new android.os.Handler(); 
  81.             hander.postDelayed(new Runnable() { 
  82.                 //线程管理 
  83.                 @Override 
  84.                 public void run() { 
  85.                     ad.dismiss(); 
  86.                 } 
  87.             },  5*1000); 
  88.             } 
  89.         }).create(); 
  90.         case SINGLE_CHOICE_LIST: 
  91.             return new AlertDialog.Builder(this).setTitle("选择国家").setSingleChoiceItems(provinces, 1, buttonOnClick).setPositiveButton("确定", buttonOnClick).setNegativeButton("取消", buttonOnClick).create(); 
  92.          
  93.         case    MULTI_LIST: 
  94.             AlertDialog ad=new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("选择国家").setMultiChoiceItems(provinces, new boolean[]{false,true,false,true,false,false} , new DialogInterface.OnMultiChoiceClickListener() { 
  95.                  
  96.                 @Override 
  97.                 public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
  98.                      
  99.                 } 
  100.             }).setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  101.                  
  102.                 @Override 
  103.                 public void onClick(DialogInterface dialog, int which) { 
  104.                     int count=lv.getCount(); 
  105.                     String s="您选择了:"
  106.                     for (int i = 0; i < provinces.length; i++) { 
  107.                         if(lv.getCheckedItemPositions().get(i)){ 
  108.                             s+=i+":"+lv.getAdapter().getItem(i)+"  "; 
  109.                         } 
  110.                     } 
  111.                     if(lv.getCheckedItemPositions().size()>0){ 
  112.                         new AlertDialog.Builder(Main.this).setMessage(s).show(); 
  113.                     }else{ 
  114.                         new AlertDialog.Builder(Main.this).setMessage("您未选择任何国家").show(); 
  115.                     } 
  116.                 } 
  117.             }).setNegativeButton("取消", null).create(); 
  118.             lv=ad.getListView(); 
  119.             return ad; 
  120.         } 
  121.          
  122.              
  123.          
  124.          
  125.          
  126.          
  127.         return null; 
  128.     } 
  129.  
  130.      
  131.     @Override 
  132.     public void onCreate(Bundle savedInstanceState) { 
  133.         super.onCreate(savedInstanceState); 
  134.         setContentView(R.layout.main); 
  135.         //得到选中的哪一个按钮 
  136.         Button btnDeletefile=(Button)findViewById(R.id.btnDeleteFile); 
  137.         Button btnSimpleList=(Button)findViewById(R.id.btnChoiceList); 
  138.         Button btnSimngleChoiceList=(Button)findViewById(R.id.btnSimpleList); 
  139.         Button btnMultiChoiceList=(Button)findViewById(R.id.btnMultiChoiceList); 
  140.         Button btnRemoveDialog=(Button)findViewById(R.id.btnRemoveDialog); 
  141.  
  142.         //绑定监听器 
  143.         btnDeletefile.setOnClickListener(this); 
  144.         btnSimpleList.setOnClickListener(this); 
  145.         btnSimngleChoiceList.setOnClickListener(this); 
  146.         btnMultiChoiceList.setOnClickListener(this); 
  147.         btnRemoveDialog.setOnClickListener(this); 
  148.          
  149.     } 
  150.     //定义一个类,用来判断选择中的。 
  151.     private class ButtonOnClick implements DialogInterface.OnClickListener { 
  152.         private int index; 
  153.  
  154.         public ButtonOnClick(int index) { 
  155.             this.index = index; 
  156.         } 
  157.  
  158.         @Override 
  159.         public void onClick(DialogInterface dialog, int whichButton) { 
  160.                 if(whichButton>=0){ 
  161.                     index=whichButton
  162.                 }else{ 
  163.                     if(whichButton==DialogInterface.BUTTON_POSITIVE){ 
  164.                         new AlertDialog.Builder(Main.this).setMessage("您已经选择了:"+index+":"+provinces[index]).show(); 
  165.                          
  166.                     }else if(whichButton==DialogInterface.BUTTON_NEGATIVE){ 
  167.                         new AlertDialog.Builder(Main.this).setMessage("你未选择,请选择").show(); 
  168.                          
  169.                     } 
  170.                 } 
  171.              
  172.              
  173.         } 
  174.  
  175.     } 
  176. protected void onPrepareDialog(int id,Dialog dialog){ 
  177.     super.onPrepareDialog(id, dialog); 
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.      

另外很多朋友对Android系统构架图还不是很了解,我附上一张中文的图给大家观看

 

 

 

你可能感兴趣的:(职场,休闲)