对话框
普通对话框
代码实现
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("警告");
b.setMessage("你好牛啊!");
b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了ok",Toast.LENGTH_SHORT).show();
}
});
b.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了cancel",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
自定义对话框
代码实现
View view = View.inflate(MainActivity.this, R.layout.view_layout, null);
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setIcon(R.drawable.b);
b.setTitle("008号技师");
b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了ok",Toast.LENGTH_SHORT).show();
}
});
b.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了cancel",Toast.LENGTH_SHORT).show();
}
});
b.setView(view);
AlertDialog alertDialog = b.create();
alertDialog.show();
日期对话框
Calendar calendar =Calendar.getInstance();
new DatePickerDialog(MainActivity.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();
时间对话框
Calendar calendar = Calendar.getInstance();
new TimePickerDialog(MainActivity.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();
水平进度条对话框
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setTitle("正在下载");
dialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress =0;
@Override
public void run() {
if (progress==100){
dialog.dismiss();
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);
圆形进度条对话框
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMax(100);
dialog.setTitle("正在下载");
dialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress =0;
@Override
public void run() {
if (progress==100){
dialog.dismiss();
timer.cancel();
}
dialog.setProgress(progress+=20);
}
},0,1000);
单选对话框
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setIcon(R.drawable.b);
b.setTitle("你最喜欢什么");
b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了ok",Toast.LENGTH_SHORT).show();
}
});
b.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 = {"女人","金钱","游戏","房子","车子"};
b.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选中的是"+items[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
多选对话框
final String[] items = {"女人","金钱","游戏","房子","车子"};
final boolean[] flags = {false,false,false,false,false};
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setIcon(R.drawable.b);
b.setTitle("你最喜欢什么");
b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer stringBuffer = new StringBuffer();
for (int i =0;i<flags.length;i++){
if (flags[i]){
stringBuffer.append(items[i]+">");
}
}
String s = stringBuffer.toString();
Toast.makeText(MainActivity.this,"你选择了ok>"+s,Toast.LENGTH_SHORT).show();
}
});
b.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了cancel",Toast.LENGTH_SHORT).show();
}
});
b.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
底部导航栏切换selector选择器的使用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/pic_checked"></item>
<item android:state_checked="false" android:drawable="@drawable/pic_unchecked"></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#2196F3"></item>
<item android:state_checked="false" android:color="#7C7A7A"></item>
</selector>
//我们给RadioGroup设置监听 RadioButton必须要设置id属性
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:drawableTop="@drawable/pic_selc"
android:id="@+id/rab1"
android:text="消息"
android:textSize="20dp"
android:checked="true"
android:textColor="@drawable/text_selc"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<RadioButton
android:drawableTop="@drawable/pic_selc"
android:text="联系人"
android:textColor="@drawable/text_selc"
android:gravity="center"
android:textSize="20dp"
android:id="@+id/rab2"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<RadioButton
android:drawableTop="@drawable/pic_selc"
android:text="动态"
android:textColor="@drawable/text_selc"
android:textSize="20dp"
android:gravity="center"
android:id="@+id/rab3"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</RadioGroup>