一:Test确认和取消Dialog:
1,在layout目录下创建一个布局,布局中创建Button按钮控件:
<Button
android:id="@+id/btn1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="确认和取消dialog"/>
2,java文件代码如下:
//1,继承Activity
public class MainActivity extends Activity {
Button btn1;
@Override
//2,创建一个容器,加载布局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//3,加载layout中布局
setContentView(R.layout.activity_main);
//4,声明并初始化Button控件
btn1=(Button) this.findViewById(R.id.btn1);
//5,设置监听事件(有三种方式)
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* 通过AlertDialog.Builder来获得AlertDialog对象,因为研究AlertDialog的源码发现AlertDialog
* 所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog = new AlertDialog();来得到。
* AlertDialog为Dialog的子类
*/
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//对话框标题
builder.setTitle("是否退出");
//对话框标题图片
builder.setIcon(R.drawable.ic_launcher);
//生成对话框输入框
builder.setView(new EditText(MainActivity.this));
//设置确认,取消按钮
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确认", 0).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
});
}
}
二:Test单选框Dialog:
1,在layout目录下创建一个布局,布局中创建Button按钮控件:
<Button
android:id="@+id/btn1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="确认和取消单选框dialog"/>
2,java文件代码如下:
//1,继承Activity
public class MainActivity extends Activity {
Button btn1;
@Override
//2,创建一个容器,加载布局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//3,加载layout中布局
setContentView(R.layout.activity_main);
//4,声明并初始化Button控件
btn1=(Button) this.findViewById(R.id.btn1);
//5,设置监听事件(有三种方式)
btn1.setOnClickListener(new View.OnClickListener() {
String[] chengJiDan={"语文:110","数学:130","英语:70","物理:100","化学:80","生物:70"};
@Override
public void onClick(View v) {
/**
* 通过AlertDialog.Builder来获得AlertDialog对象,因为研究AlertDialog的源码发现AlertDialog
* 所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog = new AlertDialog();来得到。
* AlertDialog为Dialog的子类
*/
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//对话框标题
builder.setTitle("是否退出");
//对话框标题图片
builder.setIcon(R.drawable.ic_launcher);
//对话框items
builder.setItems(chengJiDan, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, chengJiDan[which], 0).show();
}
});
builder.create().show();
}
});
}
三:Test复选框Dialog:
//1,继承Activity
public class MainActivity extends Activity {
Button btn1;
@Override
//2,创建一个容器,加载布局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//3,加载layout中布局
setContentView(R.layout.activity_main);
//4,声明并初始化Button控件
btn1=(Button) this.findViewById(R.id.btn1);
//5,设置监听事件(有三种方式)
btn1.setOnClickListener(new View.OnClickListener() {
String[] shopping={"手机","电脑","Adidas","Nick","美女"};
ArrayList<String> arrayList=new ArrayList<String>();
public void onClick(View v) {
/**
* 通过AlertDialog.Builder来获得AlertDialog对象,因为研究AlertDialog的源码发现AlertDialog
* 所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog = new AlertDialog();来得到。
* AlertDialog为Dialog的子类
*/
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//对话框标题
builder.setTitle("是否退出");
//对话框标题图片
builder.setIcon(R.drawable.ic_launcher);
builder.setMultiChoiceItems(shopping, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
arrayList.add(shopping[which]);
}else {
arrayList.remove(shopping[which]);
}
if (arrayList.size()>0) {
Toast.makeText(getApplicationContext(), arrayList.toString(), 0).show();
}
}
});
builder.create().show();
}
});
}