自定义Toast
来源:互联网 发布:linux getdtablesize 编辑:程序博客网 时间:2024/06/05 13:26
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); }
0 0
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- Toast自定义
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- 分别嵌入 新浪微博、QQ、微信 做第三方授权登录 获取到头像 昵称等信息
- SpringMVC
- C#读取INI文件
- 静态方法何时使用
- java web一些概念区分
- 自定义Toast
- lalala
- 机器学习笔记(五)续——朴素贝叶斯算法的后验概率最大化含义
- GDOI'2016市选day1 —— 2. K叉树(ktree)
- 详解垃圾收集器
- 算法导论----<选择排序>及实例
- spring(4)
- NYOJ 207 && POJ 1182 食物链 (并查集)
- zjnu1822JEDAN(dp)