1、普通对话框一
public void show(View v){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher).setTitle("haha");
builder.show();
}
}
2、普通对话框二
public void show2(View v){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle("明天去哪玩?")
.setMessage("好好学习,天天向上");
OnClickListener clickListener=new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(MainActivity.this,
"the chioce is no", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(MainActivity.this,
"the chioce is yes", Toast.LENGTH_SHORT).show();
break;
}
}
};
builder.setNegativeButton("no",clickListener);
builder.setPositiveButton("yes",clickListener);
builder.show();
}
3、简单列表对话框
public void show3(View v){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle("what is your favorite?")
.setItems(str, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
"what your chioce is "+str[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
4、单选列表对话框
public void show4(View v){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle("learnHard")
.setSingleChoiceItems(str, index, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
"what your chioce is"+str[which],
Toast.LENGTH_SHORT).show();
index=which;
dialog.dismiss();
}
});
builder.show();
}
5、自定义view
public void show6(View v){
List<Map<String, Object>> data=new ArrayList<>();
String[] names={"hah","xix","heh"};
int[] images={R.drawable.q1,R.drawable.q2,R.drawable.q3};
for (int i = 0; i < 3; i++) {
Map<String, Object> map=new HashMap<>();
map.put("name", names[i]);
map.put("image", images[i]);
data.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this, data,
R.layout.item2, new String[]{"image","name"},
new int[]{R.id.iv,R.id.tv});
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle("just do it")
.setAdapter(adapter, null);
builder.show();
}
6、多选列表对话框
public void show5(View v){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle("show your chioce:")
.setMultiChoiceItems(str, flag, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if(isChecked){
flag[which]=true;
}else{
flag[which]=false;
}
}
}).setPositiveButton("yes", new OnClickListener() {
String s="";
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < flag.length; i++) {
if(flag[i]){
s=s+str[i]+",";
}
}
s=s.substring(0,s.length()-1);
Toast.makeText(MainActivity.this,
"what your chioce is"+s, Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
7、简单登录程序
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//登录
public void show7(View v){
final View v2=View.inflate(this, R.layout.item3, null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher)
.setTitle("chioce")
.setView(v2)
.setPositiveButton("login", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText text1=(EditText) v2.findViewById(R.id.username);
EditText text2=(EditText) v2.findViewById(R.id.password);
String username=text1.getText().toString();
String passward=text2.getText().toString();
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(passward)){
Toast.makeText(MainActivity.this, "null", Toast.LENGTH_SHORT).show();
mShow(dialog,false);
return;
}
if("zhang".equals(username)&&"123".equals(passward)){
Toast.makeText(MainActivity.this, "access", Toast.LENGTH_SHORT).show();
mShow(dialog,true);
}else{
Toast.makeText(MainActivity.this, "failure", Toast.LENGTH_SHORT).show();
mShow(dialog,false);
}
}
})
.setNegativeButton("dismiss", null);
builder.show();
}
public static void mShow(DialogInterface dialog,boolean flag){
Field field;
try {
field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, flag);
} catch (Exception e) {
e.printStackTrace();
}
}
String[] str={"sleeping","playing","learning","doing"};
int index=0;
boolean[] flag={false,false,false,false};