Android开发- 点击按钮旁弹出选择框

示例:

                                                    

 

一、代码部分:

1.弹出框页面布局(layout/pop_mesure_layout.xml)

<!--LinearLayout不能设置背景颜色-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <!--@color/arrowViewColor设置背景透明度为0-->
   <com.chy.tools.ArrowView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@color/arrowViewColor"
       android:padding="20dp">

     <ListView
         android:id="@+id/measureListView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:cacheColorHint="#00000000"/>


   </com.chy.tools.ArrowView>


</LinearLayout>

二、java代码: 

 /**
     * 测量方法
     * */
    public void measureMapOperation(View v){
        // 获取弹出框布局
        View measureView = LayoutInflater.from(context).inflate(R.layout.pop_measure_layout, null);
        // 创建数组,承接坐标值(x坐标值:location[0],y坐标值:location[1])
        int[] location = new int[2];
        // view相对于整个屏幕的左上角坐标
        v.getLocationOnScreen(location);
        // view相对于当前Activity的左上角坐标
        //v.getLocationInWindow(location);

        popWindow = new PopupWindow(measureView, 200, 220);
        // 设置这句话,点击pop以外区域回dismiss
        popWindow.setOutsideTouchable(true);
        int offsetX = location[0] - popWindow.getWidth();
        int offsetY = location[1] - popWindow.getHeight()/5;
        popWindow.showAtLocation(v,Gravity.NO_GRAVITY,offsetX,offsetY);
        // 测量数组
        final List<String> measureList =  Arrays.asList(new String[]{"测距","测面"});
        measureAdapter = new MeasureAdapter(context,measureList);
        ListView listView = measureView.findViewById(R.id.measureListView);
        listView.setAdapter(measureAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
                resertOperation();//重置
                String result = measureList.get(position);
                if ("测距".equals(result)){
                    // 测距方法
                    measureDistance();
                }
                if ("测面".equals(result)){
                    // 测面方法
                    measureArea();
                }
            }
        });
    }

三、ArrowView

请看另一篇文章:Android 使用popupWindow 模仿微信弹出框使用方法_破z晓的博客-CSDN博客

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Android 代码示例,演示如何在按钮点击弹出提示: 1. 在你的 XML 布局文件中添加一个按钮控件: ```xml <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击弹出提示" /> ``` 2. 在你的 Java 代码中获取按钮控件,然后为它设置点击事件监听器: ```java Button myButton = findViewById(R.id.my_button); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在这里编写弹出提示的代码 } }); ``` 3. 在点击事件监听器中,使用 `AlertDialog.Builder` 类创建一个提示,并设置标题、消息和按钮: ```java AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage("确定要执行这个操作吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 在这里编写“确定”按钮被点击时的代码 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 在这里编写“取消”按钮被点击时的代码 } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 这样,当用户点击按钮时,就会弹出一个提示,询问用户是否要执行某项操作。用户可以选择“确定”或“取消”按钮。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值