package com.jz;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.jz.adapter.MyBaseAdapter;
import com.jz.model.Person;
import java.util.ArrayList;
import java.util.List;
封装权限找github=https://github.com/lovedise/PermissionGen
lovedise/PermissionGen
// android api easy to use permission for android m
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "AlertDialogActivity";
private Button btn01,btn02,btn03;
private MyBaseAdapter adapter;
private String[] names; //代表人的姓名
private int[] pics = { //代表人的图片信息
R.drawable.diaochan,R.drawable.huatuo,
R.drawable.lvbu,R.drawable.sunquan,
R.drawable.sunshangxiang,R.drawable.zhouyu
};
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
initView();
initData();
}
//初始化数据
private void initData() {
//得到资源文件arrays.xml中的names字符串数组
names = getResources().getStringArray(R.array.names);
persons = new ArrayList<Person>();
//向集合添加数据
for(int i=0;i<names.length;i++){
persons.add(new Person(pics[i],names[i]));
}
//构造适配器对象
adapter = new MyBaseAdapter(this,R.layout.list_item,persons);
}
//初始化视图
private void initView() {
btn01 = (Button) findViewById(R.id.btn01);
btn02 = (Button) findViewById(R.id.btn02);
btn03 = (Button) findViewById(R.id.btn03);
//为按钮添加事件监听处理
btn01.setOnClickListener(this);
btn02.setOnClickListener(this);
btn03.setOnClickListener(this);
}
//按钮的事件处理函数
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn01: //警告框用法一[使用setItems加载列表项并添加监听处理]
dialog1();
break;
case R.id.btn02: //警告框用法二[使用adapter加载列表项]
dialog2();
break;
case R.id.btn03: //警告框用法三[使用multiChoiceItems加载多选框列表]
dialog3();
break;
case R.id.button04: //普通对话框
dialog4();
break;
case R.id.button05: //高级对话框
dialog5();
break;
}
}
//高级对话框(动态加载listview布局作为dialog的视图)
private void dialog5() {
//1.动态加载listview控件的布局并得到此listview控件
View view = View.inflate(this, R.layout.activity_main, null);
ListView listview = (ListView) view.findViewById(R.id.listView1);
listview.setAdapter(new MyBaseAdapter(this, R.layout.list_item03, persons));
//对选择listview中的某一项进行事件处理
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//形参中的parent就代表listview对象
ListView listview = (ListView) parent;
//通过listview控件的适配器对象 得到所选中的某一项的对象
//person = (Person) listview.getAdapter().getItem(position);
//或者
person = persons.get(position);
}
});
//2.定义对话框
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("温馨提示")
.setView(view) //为对话框动态指定布局视图
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LinearLayout ll = new LinearLayout(DialogActivity.this);
ImageView imageview = new ImageView(DialogActivity.this);
TextView textview = new TextView(DialogActivity.this);
imageview.setImageResource(person.picId);
textview.setText(person.name);
ll.addView(imageview);
ll.addView(textview);
//定义一个toast对象
Toast toast = new Toast(DialogActivity.this);
toast.setView(ll);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogActivity.this.finish();
}
})
.show();
}
//普通对话框
private void dialog4() {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("温馨提示");
builder.setMessage("大家好,这是一个普通的对话框的示例,请大家认真学习!");
//设置“确定”按钮及其事件监听
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "确定", Toast.LENGTH_LONG).show();
}
});
//设置“取消”按钮及其事件监听
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogActivity.this.finish();
}
});
//显示对话框
builder.show();
}
//警告框用法三[使用multiChoiceItems加载多选框列表]
private void dialog3() {
final List<String> list = new ArrayList<String>();
final String items [] = {"三国演义","水浒传","西游记","红楼梦"};
//①构造一个builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//②设置对话框的各个属性
builder.setTitle("请选择你爱好的图书:")
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//取得列表中的某一项
String info = items[which];
if(isChecked){
list.add(info);
}else{
list.remove(info);
}
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this,list.toString(),Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", null)
.show();
}
//警告框用法二
private void dialog2() {
MyBaseAdapter adapter = new MyBaseAdapter(this,R.layout.list_item,persons);
//①构造一个builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//②设置对话框的各个属性
builder.setTitle("请选择你喜爱的人物:")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//代表当前选择的哪个人物
Person person = persons.get(which);
//使用toast打印信息
Toast.makeText(AlertDialogActivity.this,person.name,Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.show();
}
//警告框用法一
private void dialog1() {
final String noves [] = {"三国演义","水浒传","西游记","红楼梦"};
//①构造一个builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//②设置对话框的各个属性
builder.setTitle("请选择你喜爱的小说:")
.setItems(noves, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//得到你选择的哪本书
String name = noves[which];
Log.i(TAG,name);
}
})
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show(); //③显示对话框
}
}
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.jz.adapter.MyBaseAdapter;
import com.jz.model.Person;
import java.util.ArrayList;
import java.util.List;
封装权限找github=https://github.com/lovedise/PermissionGen
lovedise/PermissionGen
// android api easy to use permission for android m
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "AlertDialogActivity";
private Button btn01,btn02,btn03;
private MyBaseAdapter adapter;
private String[] names; //代表人的姓名
private int[] pics = { //代表人的图片信息
R.drawable.diaochan,R.drawable.huatuo,
R.drawable.lvbu,R.drawable.sunquan,
R.drawable.sunshangxiang,R.drawable.zhouyu
};
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
initView();
initData();
}
//初始化数据
private void initData() {
//得到资源文件arrays.xml中的names字符串数组
names = getResources().getStringArray(R.array.names);
persons = new ArrayList<Person>();
//向集合添加数据
for(int i=0;i<names.length;i++){
persons.add(new Person(pics[i],names[i]));
}
//构造适配器对象
adapter = new MyBaseAdapter(this,R.layout.list_item,persons);
}
//初始化视图
private void initView() {
btn01 = (Button) findViewById(R.id.btn01);
btn02 = (Button) findViewById(R.id.btn02);
btn03 = (Button) findViewById(R.id.btn03);
//为按钮添加事件监听处理
btn01.setOnClickListener(this);
btn02.setOnClickListener(this);
btn03.setOnClickListener(this);
}
//按钮的事件处理函数
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn01: //警告框用法一[使用setItems加载列表项并添加监听处理]
dialog1();
break;
case R.id.btn02: //警告框用法二[使用adapter加载列表项]
dialog2();
break;
case R.id.btn03: //警告框用法三[使用multiChoiceItems加载多选框列表]
dialog3();
break;
case R.id.button04: //普通对话框
dialog4();
break;
case R.id.button05: //高级对话框
dialog5();
break;
}
}
//高级对话框(动态加载listview布局作为dialog的视图)
private void dialog5() {
//1.动态加载listview控件的布局并得到此listview控件
View view = View.inflate(this, R.layout.activity_main, null);
ListView listview = (ListView) view.findViewById(R.id.listView1);
listview.setAdapter(new MyBaseAdapter(this, R.layout.list_item03, persons));
//对选择listview中的某一项进行事件处理
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//形参中的parent就代表listview对象
ListView listview = (ListView) parent;
//通过listview控件的适配器对象 得到所选中的某一项的对象
//person = (Person) listview.getAdapter().getItem(position);
//或者
person = persons.get(position);
}
});
//2.定义对话框
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("温馨提示")
.setView(view) //为对话框动态指定布局视图
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LinearLayout ll = new LinearLayout(DialogActivity.this);
ImageView imageview = new ImageView(DialogActivity.this);
TextView textview = new TextView(DialogActivity.this);
imageview.setImageResource(person.picId);
textview.setText(person.name);
ll.addView(imageview);
ll.addView(textview);
//定义一个toast对象
Toast toast = new Toast(DialogActivity.this);
toast.setView(ll);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogActivity.this.finish();
}
})
.show();
}
//普通对话框
private void dialog4() {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("温馨提示");
builder.setMessage("大家好,这是一个普通的对话框的示例,请大家认真学习!");
//设置“确定”按钮及其事件监听
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "确定", Toast.LENGTH_LONG).show();
}
});
//设置“取消”按钮及其事件监听
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogActivity.this.finish();
}
});
//显示对话框
builder.show();
}
//警告框用法三[使用multiChoiceItems加载多选框列表]
private void dialog3() {
final List<String> list = new ArrayList<String>();
final String items [] = {"三国演义","水浒传","西游记","红楼梦"};
//①构造一个builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//②设置对话框的各个属性
builder.setTitle("请选择你爱好的图书:")
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//取得列表中的某一项
String info = items[which];
if(isChecked){
list.add(info);
}else{
list.remove(info);
}
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this,list.toString(),Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", null)
.show();
}
//警告框用法二
private void dialog2() {
MyBaseAdapter adapter = new MyBaseAdapter(this,R.layout.list_item,persons);
//①构造一个builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//②设置对话框的各个属性
builder.setTitle("请选择你喜爱的人物:")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//代表当前选择的哪个人物
Person person = persons.get(which);
//使用toast打印信息
Toast.makeText(AlertDialogActivity.this,person.name,Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.show();
}
//警告框用法一
private void dialog1() {
final String noves [] = {"三国演义","水浒传","西游记","红楼梦"};
//①构造一个builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//②设置对话框的各个属性
builder.setTitle("请选择你喜爱的小说:")
.setItems(noves, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//得到你选择的哪本书
String name = noves[which];
Log.i(TAG,name);
}
})
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show(); //③显示对话框
}
}