首页 > 代码库 > AlertDialog Demo
AlertDialog Demo
AlertDialod like java awt dialog , when a event actived the activity will display a dialog , it can contain many views not only text or buttons ,here is a demo show us how to use AlertDialog to creat multi choices ,single choice .
1 package com.example.alertdialog_01; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.support.v7.app.ActionBar; 5 import android.support.v4.app.Fragment; 6 import android.app.AlertDialog; 7 import android.app.AlertDialog.Builder; 8 import android.content.DialogInterface; 9 import android.content.DialogInterface.OnMultiChoiceClickListener; 10 import android.os.Bundle; 11 import android.view.LayoutInflater; 12 import android.view.Menu; 13 import android.view.MenuItem; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.view.ViewGroup; 17 import android.widget.Button; 18 import android.widget.Toast; 19 import android.os.Build; 20 21 public class MainActivity extends ActionBarActivity { 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 if (savedInstanceState == null) { 29 getSupportFragmentManager().beginTransaction() 30 .add(R.id.container, new PlaceholderFragment()).commit(); 31 } 32 Button b1 = (Button) findViewById(R.id.button1); 33 Button b2 = (Button) findViewById(R.id.button2); 34 Button b3 = (Button) findViewById(R.id.button3); 35 Button b4 = (Button) findViewById(R.id.button4); 36 Button b5 = (Button) findViewById(R.id.button5); 37 b1.setOnClickListener(new OnClickListener() { 38 39 @Override 40 public void onClick(View arg0) { 41 AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this); 42 43 ab.setNegativeButton("Negative", new DialogInterface.OnClickListener() { 44 45 @Override 46 public void onClick(DialogInterface arg0, int arg1) { 47 Toast.makeText(MainActivity.this, "Negative", Toast.LENGTH_SHORT).show(); 48 49 } 50 }); 51 52 ab.setPositiveButton("Positive", new DialogInterface.OnClickListener() { 53 54 @Override 55 public void onClick(DialogInterface arg0, int arg1) { 56 Toast.makeText(MainActivity.this, "Positive", Toast.LENGTH_SHORT).show(); 57 58 } 59 }); 60 61 62 63 64 65 /** 66 * set Attribute of dialog must before create it 67 * eg: title message button and so on 68 * 69 * */ 70 71 ab.setTitle("MyFirstAlertDialog!"); 72 ab.setMessage("HelloJava"); 73 AlertDialog ad = ab.create(); 74 ad.show(); 75 } 76 }); 77 78 79 final String[] sc = { 80 "NanJing", 81 "BeiJing", 82 "DongJing" 83 }; 84 b2.setOnClickListener(new View.OnClickListener() { 85 86 @Override 87 public void onClick(View arg0) { 88 AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this); 89 90 91 adb.setTitle("Second"); 92 adb.setItems(sc, new DialogInterface.OnClickListener() { 93 94 @Override 95 public void onClick(DialogInterface arg0, int arg1) { 96 Toast.makeText(MainActivity.this, sc[arg1], Toast.LENGTH_SHORT).show(); 97 98 } 99 });100 101 AlertDialog ad = adb.create() ;102 ad.show();103 104 }105 });106 107 b4.setOnClickListener(new OnClickListener() {108 109 @Override110 public void onClick(View arg0) {111 AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);112 ab.setTitle("SelectCity");113 114 115 ab.setSingleChoiceItems(sc, 0, new DialogInterface.OnClickListener() {116 117 @Override118 public void onClick(DialogInterface arg0, int arg1) {119 Toast.makeText(MainActivity.this, "wu", Toast.LENGTH_SHORT).show();120 121 }122 });123 AlertDialog ad = ab.create() ;124 ad.show();125 }126 });127 b5.setOnClickListener(new OnClickListener() {128 129 @Override130 public void onClick(View arg0) {131 AlertDialog.Builder db = new AlertDialog.Builder(MainActivity.this);132 133 134 db.setTitle("Muti");135 db.setMultiChoiceItems(sc,null, new OnMultiChoiceClickListener() {136 137 @Override138 public void onClick(DialogInterface arg0, int arg1, boolean arg2) {139 Toast.makeText(MainActivity.this, sc[arg1], Toast.LENGTH_SHORT).show();;140 141 }142 });143 144 AlertDialog ad = db.create() ;145 ad.show();146 147 }148 });149 150 }151 152 @Override153 public boolean onCreateOptionsMenu(Menu menu) {154 155 // Inflate the menu; this adds items to the action bar if it is present.156 getMenuInflater().inflate(R.menu.main, menu);157 return true;158 }159 160 @Override161 public boolean onOptionsItemSelected(MenuItem item) {162 // Handle action bar item clicks here. The action bar will163 // automatically handle clicks on the Home/Up button, so long164 // as you specify a parent activity in AndroidManifest.xml.165 int id = item.getItemId();166 if (id == R.id.action_settings) {167 return true;168 }169 return super.onOptionsItemSelected(item);170 }171 172 /**173 * A placeholder fragment containing a simple view.174 */175 public static class PlaceholderFragment extends Fragment {176 177 public PlaceholderFragment() {178 }179 180 @Override181 public View onCreateView(LayoutInflater inflater, ViewGroup container,182 Bundle savedInstanceState) {183 View rootView = inflater.inflate(R.layout.fragment_main, container,184 false);185 return rootView;186 }187 }188 189 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。