1.自定义一个带图片带文字的消息提示框:在按钮上写监听方法,在方法中实现如下:
public void customListener(View v){
//将静态的布局文件转换成view对象
View view = getLayoutInflater().inflate(R.layout.toast_layout, null);
Toast toast = Toast.makeText(getBaseContext(), "", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(view);
toast.setDuration(100);
toast.show();
}
其中toast_layout是写好的消息模板,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义Toast" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/toast_layout_img" android:background="@mipmap/ic_launcher" />
</LinearLayout>
2.自定义对话框Toast:
public void dialogListener(View v){
View view=getLayoutInflater().inflate(R.layout.dialog_layout, null);
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setCancelable(true);
builder.setIcon(R.mipmap.left_menu_activity_normal);
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "您点击了确定", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "您点击了取消", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
3.自定义单选框Toast:
public void choiceListener(View v){
final String[] city={"烟台","青岛","威海"};
et=new EditText(this);
View view=getLayoutInflater().inflate(R.layout.dialog_layout,null);
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setCancelable(true);
builder.setIcon(R.mipmap.left_menu_activity_normal);
builder.setView(et);
builder.setSingleChoiceItems(city, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), city[which], Toast.LENGTH_SHORT).show();
key = which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String str = et.getText().toString();
Toast.makeText(getBaseContext(), "你选择了" + str + city[key], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
4.自定义多选框Toast:
public void choicesListener(View v){
final String[] group={"巴西","荷兰","德国","阿根廷"};
final boolean[] b={true,false,true,false};
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("多选");
builder.setMultiChoiceItems(group, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(getBaseContext(), group[which], Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
5.自定义popupWindow:
onCreate里面代码如下:
View view=getLayoutInflater().inflate(R.layout.popup_layout,null);
popupWindow=new PopupWindow(view);
popupWindow.setFocusable(true);
ColorDrawable cd=new ColorDrawable();
popupWindow.setBackgroundDrawable(cd);
其中popup_layout是写好的弹出框模板,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="#0fed04" android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:text="选项1" android:gravity="center" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:text="选项2" android:gravity="center" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:text="选项3" android:gravity="center" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:text="选项4" android:gravity="center" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:text="选项5" android:gravity="center" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:text="选项6" android:gravity="center" />
</LinearLayout>
监听函数代码如下:
public void popuWindowShow(View view){
popupWindow.setWidth(view.getWidth());
int height=getResources().getDisplayMetrics().heightPixels/3;
popupWindow.setHeight(height);
popupWindow.showAsDropDown(view,0,0);
popupWindow.setAnimationStyle(R.style.popAnim);
}