本經驗將分別介紹Android裏面的四種AlertDialog分別是:顯示帶中立,取消,肯定的提示框、 獲取帶列表的對話框按鈕、獲取帶單選列表的對話框按、獲取顯示帶多選項的對話框。此經驗介紹獲取顯示帶多選項的對話框html
Android studio或者Eclipseapp
首先咱們在界面佈局上面佈局四個按鈕ide
而後咱們在代碼中關聯這四個按鈕佈局
Button button3=(Button)findViewById(R.id.button3);ui
Button button4=(Button)findViewById(R.id.button4);this
Button button5=(Button)findViewById(R.id.button5);spa
Button button6=(Button)findViewById(R.id.button6);code
而後分別加入觸發事件:htm
//獲取帶列表的對話框按鈕 button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String[] iterms=new String[]{"run","badminton","football","pingpang","gymnastics","tennis","more..."}; AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); //builder.setIcon(R.drawable.desert); builder.setTitle("Choose your favable sport project:"); builder.setItems(iterms,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"You choosed "+iterms[which],Toast.LENGTH_LONG).show(); } }); builder.create().show();//建立對話框並顯示 } }); //獲取帶單選列表的對話框按鈕 button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String[] iterms=new String[]{"standard","silent","meeting","outdoor","offline"}; AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); //builder.setIcon(R.drawable.desert); builder.setTitle("Choose your favable contextual model:"); builder.setSingleChoiceItems(iterms, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "You choosed " + iterms[which], Toast.LENGTH_LONG).show(); } }); builder.create().show();//建立對話框並顯示 } }); //獲取顯示帶多選項的對話框 button6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkedItems=new boolean[]{false,true,false,true,false}; items=new String[]{"Plants vs Zombies","Angry Birds","popodino","Happy Farm","Super Mario"}; //顯示帶單選列表的對話框 AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext()); //builder.setIcon(R.drawable.desert); builder.setTitle("Choose your favorable Game:"); builder.setMultiChoiceItems(items,checkedItems,new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which]=isChecked; } }); //爲對話框添加"肯定按鈕" builder.setPositiveButton("confirm",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result=""; for(int i=0;i<checkedItems.length;i++){ if(checkedItems[i]==true){ result+=items[i]+","; } } if(result.equals("")==false){ result=result.substring(0,result.length()-1); Toast.makeText(MainActivity.this,"You choosed "+result,Toast.LENGTH_LONG).show(); } } }); builder.create().show(); } });
運行結果以下blog
更多詳細的介紹你們能夠到這裏面看看,圖文並茂更清楚:
http://jingyan.baidu.com/article/0320e2c1ccc8031b87507b23.html