Android第一天
一 . 常用的对话框
1.普通对话框
public void bt1(View view) {
AlertDialog.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(MainActivity.this, "确认", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
2.单选对话框
private void single_dialog() {
//TODO 1:构建者
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("杨成果的成分是");//设置标题
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
final String[] items={"男人","女人","不想"};
//TODO 设置单选列表
//TODO 参数一 列表 参数二 默认选中下表 参数三 事件监听
builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中烦恼的"+items[which], Toast.LENGTH_SHORT).show();
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
3.多选对话框
private void mulite_dialog() {
final String[] items={"凤姐","小赵","如花","陈琳"};
final boolean[] flags={true,true,true,false};
//TODO 1:构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//TODO 2:画画
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("杨成果的女朋友们");//设置标题
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0;i<flags.length;i++){
if(flags[i]){
Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();
}
}
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
//TODO 设置多选列表
//TODO 参数一 列表 参数二 默认选中下表 参数三 事件监听
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
4.自定义对话框(基础)
1.Java代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:textSize="30sp"
android:text="大美女"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="+@id/iv"
android:src="@drawable/tp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.Java代码:
//TODO 0:获得视图
View view=LayoutInflater.from(this).inflate(R.layout.customer_dialog,null);
ImageView imageView=view.findViewById(R.id.iv);
imageView.setOnclickListener(new OnclickListsner(){
//点击图片就可以吐司或者关闭对话框了
});
//TODO 1:构建着
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置自定义布局
builder.setView(view);
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
5.日期选择对话框
//日期选择对话框DatePickerDialog
private void date_dialog() {
/**
*
*
* @param context 上下文
* @param listener 监听器
* @param year 默认的年
* @param month 默认的月
* @param dayOfMonth 默认的日
*/
Calendar calendar=Calendar.getInstance();//日历对象
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
6.时间选择对话框
//时间对话框:TimePickerDialog
private void time_dialog() {
/**
* @param context 上下文
* @param listener 监听器
* @param hourOfDay 默认的时
* @param minute 默认的分钟
* @param is24HourView 是否采用24时禁止
*/
Calendar calendar=Calendar.getInstance();//日历对象
//TODO 1:实例化对象 TODO 2:show
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
}