欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-27 16:27:22
本文实例讲述了android编程实现长按弹出选项框view进行操作的方法。分享给大家供大家参考,具体如下: 长按弹出选项框view进行操作 主要代码解释 pr...

本文实例讲述了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编程实现长按弹出选项框View进行操作的方法

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

完整实例代码点击此处本站下载

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

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