您的位置:首页 > 移动开发 > Android开发

Android学习笔记----关于各种AlertDialog的总结

2012-03-25 22:32 253 查看
1.AlertDialog

最简单的Dialog,包含title,message,若干个button;

package com.test.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class TestAlertDialogActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Builder builder = new AlertDialog.Builder(this);
ImageView img = (ImageView)findViewById(R.id.img);
Button bn = (Button)findViewById(R.id.bn);

bn.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
builder.setTitle("最简单的Dialog");
builder.setMessage("是否要退出程序?");
builder.setPositiveButton("YES",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
finish();
}
});

builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});

builder.create().show();
}});

}
}


  



2.带有列表的AlertDialog

----此对话框是以列表的形势展现给大家



package com.test.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ListAlertDialog  extends Activity{
int[] imageIds = new int[]{R.drawable.a4,R.drawable.a5,R.drawable.a6};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
final Builder builder = new AlertDialog.Builder(this);
final ImageView img = (ImageView)findViewById(R.id.img);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
builder.setTitle("带有列表的AlertDialog");
builder.setItems(new String[]{"NBA_1","NBA_2","NBA_3"},
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

switch(which) {
case 0:
img.setImageResource(imageIds[which]);
break;
case 1:
img.setImageResource(imageIds[which]);
break;
case 2:
img.setImageResource(imageIds[which]);
break;
}
}
});

builder.create().show();
}});

}

}


  

3.带有单选列表的AlertDialog

-----实现一个可以单选选择的列表,效果图如下:



package com.test.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SingleChoiceAlertDialog extends Activity {
int[] imageIds = new int[]{R.drawable.a4,R.drawable.a5,R.drawable.a6};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.singlechoice);
final Builder builder = new AlertDialog.Builder(this);
final ImageView img = (ImageView)findViewById(R.id.img);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
builder.setTitle("带有单选列表的AlertDialog");
builder.setSingleChoiceItems(new String[]{"NBA_1","NBA_2","NBA_3"},
1,new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which){
case 0:
img.setImageResource(imageIds[which]);
break;
case 1:
img.setImageResource(imageIds[which]);
break;
case 2:
img.setImageResource(imageIds[which]);
break;
}
}
});
builder.setPositiveButton("OK", null);

builder.create().show();
}});
}

}


  

4.带有多选列表的AlertDialog

------可以实现多选功能,效果图如下:



package com.test.alertdialog;

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

public class MutiChoiceAlertDialog extends Activity{
final  boolean[] checkStatus = new boolean[]{true,false,true};
final String[] players = new String[]{"Kobe","James","Wade"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mutichoice);
final Builder builder = new AlertDialog.Builder(this);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
builder.setTitle("带有多选列表的AlertDialog");
builder.setMultiChoiceItems(players, checkStatus,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
EditText et = (EditText)findViewById(R.id.et);
String ss = "你选择的球员是:";
for(int i = 0;i < checkStatus.length;i++)
{
if(checkStatus[i])
{
ss += players[i]+",";
}

}
et.setText(ss);
}
});
builder.setPositiveButton("OK", null);
builder.create().show();
}});
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: