关于弹窗:自定义PopuWindow和Toast
2016-05-20 17:34
295 查看
最近做项目要求有弹窗,效果如下
我想到popuWindow 但是有bug,就是弹出次数比较多的时候,它就不消失了。项目组iOS的小伙伴也被同样问题困扰,经讨论有两种解决思路:
1,如果pop.isShowing让其他控件不可选,
2,铺满屏幕,中心黑色,其他都透明
我还没来得及试试。
想到另一种就是吐司,一样一样说:
先把popuWindow代码贴出来:(文中所有源码下载链接都会在文末提供)
PopuUtils.java
MainActivity.java
main_activity.xml
drawable中新建一个根目录为shape的xml文件:my_corner_shape.xml
popuWindow的布局文件
再说说Toast方法:
注意代码有公用的:
1,popuWindow.xml
2,my_corner_shape.xml
CenterToast.java代码
MainActivity.java
主界面布局文件代码
关于Toast代码的文字参考可以点击这个链接:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/945.html 另外推荐一篇科普文《几种Toast的基本用法》http://jileniao.net/android-toast-displaytime.html
另外有个关于Toast的工具类:
点点点时候,避免了界面都关了,吐司还在自嗨中,一直弹弹弹
本文中源码下载链接:
1,PopuWindow的:http://download.csdn.net/detail/iblade/9526533
2,ToastDemo的:http://download.csdn.net/detail/iblade/9526538
我想到popuWindow 但是有bug,就是弹出次数比较多的时候,它就不消失了。项目组iOS的小伙伴也被同样问题困扰,经讨论有两种解决思路:
1,如果pop.isShowing让其他控件不可选,
2,铺满屏幕,中心黑色,其他都透明
我还没来得及试试。
想到另一种就是吐司,一样一样说:
先把popuWindow代码贴出来:(文中所有源码下载链接都会在文末提供)
PopuUtils.java
package com.example.ylwang.popuwindowdemo; import android.content.Context; import android.os.Handler; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.PopupWindow; import android.widget.TextView; /** * Created by ylwang on 2016/5/20. */ public class PopuUtils { private Context context; private View anchor;//锚,就是为了弹窗的定位 private PopupWindow pop; private Handler handler = new Handler(); private Runnable runnable = new Runnable() { @Override public void run() { pop.dismiss(); } }; public PopuUtils(View anchor, Context context) { this.anchor = anchor; this.context = context; } /** * @param tips 输入提示框的内容 * @param duration 显示时间,单位:ms */ public void show(String tips, int duration) { View contentView = LayoutInflater.from(context).inflate(R.layout.popuwindow, null); TextView textView = (TextView) contentView.findViewById(R.id.tv_popu); textView.setText(tips); pop = new PopupWindow(contentView, 320, 110); pop.showAtLocation((View) anchor.getParent(), Gravity.CENTER, 0, 0); pop.update();//必须添加 否则不显示 handler.postDelayed(runnable, duration); } }
MainActivity.java
package com.example.ylwang.popuwindowdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { private Button btn_show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private <T> T f(int id) { return (T) findViewById(id); } private void init() { btn_show = f(R.id.btn_show); btn_show.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_show: new PopuUtils(btn_show, this).show("随机播放", 1000); break; default: break; } } }
main_activity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出popuWindow" /> </RelativeLayout>
drawable中新建一个根目录为shape的xml文件:my_corner_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#b3000000" /> <corners android:radius="20px" /> <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" /> </shape>
popuWindow的布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="160dp" android:layout_height="55dp" android:background="@drawable/my_corner_shape" android:orientation="vertical"> <TextView android:id="@+id/tv_popu" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#ffffff" android:textSize="20sp" /> </LinearLayout>
再说说Toast方法:
注意代码有公用的:
1,popuWindow.xml
2,my_corner_shape.xml
CenterToast.java代码
package com.example.ylwang.toastdemo; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.util.Timer; import java.util.TimerTask; /** * Created by ylwang on 2016/5/20. */ public class CenterToast { private Toast toast; private TextView textView; public CenterToast(Context context) { View convertView = LayoutInflater.from(context).inflate(R.layout.popuwindow, null); textView = (TextView) convertView.findViewById(R.id.tv_popu); toast = new Toast(context); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(convertView); } /** * @param tips 要显示的内容 * @param duration 显示时间 单位:ms */ public void showMyToast(String tips, final int duration) { textView.setText(tips); final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { toast.show(); } }, 0, 3000); new Timer().schedule(new TimerTask() { @Override public void run() { toast.cancel(); timer.cancel(); } }, duration); } }
MainActivity.java
package com.example.ylwang.toastdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { private CenterToast centerToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { centerToast = new CenterToast(this); Button btnShow = (Button) findViewById(R.id.btn_toShow); btnShow.setOnClickListener(this); } @Override public void onClick(View v) { centerToast.showMyToast("我的自定义Toast", 1000); } }
主界面布局文件代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_toShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出我的自定义土司" /> </RelativeLayout>
关于Toast代码的文字参考可以点击这个链接:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/945.html 另外推荐一篇科普文《几种Toast的基本用法》http://jileniao.net/android-toast-displaytime.html
另外有个关于Toast的工具类:
点点点时候,避免了界面都关了,吐司还在自嗨中,一直弹弹弹
import android.content.Context; import android.os.Handler; import android.widget.Toast; /** * Toast 每次创建Toast时先做一下判断, * 如果前面有Toast在显示,只需调用Toast中的setText()方法将要显示的信息替换即可 * 避免了界面都关了,吐司还在自嗨中,一直弹弹弹 */ public class CustomToast { private static Toast mToast; private static Handler mHandler = new Handler(); private static Runnable r = new Runnable() { public void run() { mToast.cancel(); } }; public static void showToast(Context mContext, String text, int duration) { mHandler.removeCallbacks(r); if (mToast != null) mToast.setText(text); else mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT); mHandler.postDelayed(r, duration); mToast.show(); } public static void showToast(Context mContext, int resId, int duration) { showToast(mContext, mContext.getResources().getString(resId), duration); } }
本文中源码下载链接:
1,PopuWindow的:http://download.csdn.net/detail/iblade/9526533
2,ToastDemo的:http://download.csdn.net/detail/iblade/9526538
相关文章推荐
- 升级centos内核到最新版本
- XenServer的架构之XenAPI(3)
- Linux的二号功臣-Alan Cox
- VOOC还真算是OPPO的核心技术
- XenServer的架构(2)
- flask apache wsgi windows
- OpenCL学习之介绍
- Centos7 关闭防火墙
- Citrix XenServer体系架构解析
- 大型网站技术架构 学习总结一(基础了解)
- Apache Cordova——APP (HTML5, CSS3, and JavaScript)
- OpenGL学习脚印: 二维纹理映射(2D textures)
- Caffe代码与架构介绍
- Linux下的文件查找命令--find
- Linux gpio 按键中断学习
- 高性能服务器架构思路【不仅是思路】
- linux top命令查看内存及多核CPU的使用讲述
- 关于SDN和OpenFlow 它们的特点和区别
- ubuntu编译android6.0错误recipe for target 'out/host/linux-x86/obj/lib/libart.so' failed
- Tomcat debug 模式