当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程实现长按弹出选项框View进行操作的方法

Android编程实现长按弹出选项框View进行操作的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

胡晓阳 陈小蒙,海盐车震门,换装大战存档

本文实例讲述了android编程实现长按弹出选项框view进行操作的方法。分享给大家供大家参考,具体如下:

长按弹出选项框view进行操作

主要代码解释

private void showpopwindows(view v) {
    /** pop view */
    view mpopview = layoutinflater.from(this).inflate(r.layout.popup, null);
    final popupwindow mpopwindow = new popupwindow(mpopview, viewgroup.layoutparams.wrap_content,
        viewgroup.layoutparams.wrap_content, true);
    /** set */
    mpopwindow.setbackgrounddrawable(new colordrawable(color.transparent));
    /** 这个很重要 ,获取弹窗的长宽度 */
    mpopview.measure(measurespec.unspecified, measurespec.unspecified);
    int popupwidth = mpopview.getmeasuredwidth();
    int popupheight = mpopview.getmeasuredheight();
    /** 获取父控件的位置 */
    int[] location = new int[2];
    v.getlocationonscreen(location);
    /** 显示位置 */
    mpopwindow.showatlocation(v, gravity.no_gravity, (location[0] + v.getwidth() / 2) - popupwidth / 2, location[1]
        - popupheight);
    mpopwindow.update();
    final string copytxt = (string) v.gettag();
    mpopview.findviewbyid(r.id.tv_copy_txt).setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        copytoclip(copytxt);
        if (mpopwindow != null) {
          mpopwindow.dismiss();
        }
      }
    });
}

layout

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/pop_bg" >
  <textview
    android:id="@+id/tv_copy_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:text="复制邀请码"
    android:textcolor="@android:color/white"
    android:textsize="12sp" />
</linearlayout>

效果图:

根据上面可以自行调整位置。

完整实例代码点击此处。

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网