zoukankan      html  css  js  c++  java
  • PopupWindow弹出窗口的使用

    1.主布局文件activity_popup_window.xml:

    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.androidui.PopupWindowActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="14dp"
            android:text="Button" />
    
    </RelativeLayout>

    2.弹出窗口的布局文件view_popup_window.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="苏州" />
    
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上海" />
    
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="北京" />
        </RadioGroup>
    
    </LinearLayout>

    3.Activity代码:

    public class PopupWindowActivity extends Activity {
    
        private PopupWindow popupWindow;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_popup_window);
            View view = getLayoutInflater().inflate(R.layout.view_popup_window, null);
            popupWindow = new PopupWindow(view, 200, 400);
            // 加载对应layout文件中的组件
            RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    Toast.makeText(PopupWindowActivity.this, "click" + checkedId, Toast.LENGTH_LONG).show();
                }
            });
            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    } else {
                        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, -10);
                    }
                }
            });
        }
    
    }

    #遇到的问题:

    使用findViewById(R.id.radioGroup1)时导致程序崩溃。

    #原因:

    使用findViewById(R.id.radioGroup1)时默认调用当前的Layout文件生成的View对象对组件初始化,但在当前Layout布局文件中没有定义R.id.radioGroup1所以会导致程序崩溃。

    #解决办法:

    实例化R.id.radioGroup1所在Layout布局文件:

    LayoutInflater.from(context).inflate(resource, root).findViewById(id)

    用该Layout布局文件的实例加载R.id.radioGroup1组件。

    这也解释了问什么相同Layout文件中不可有相同id,不同Layout文件中可以有相同id的问题,因为不同Layout中相同id的组件是用与其组件相对应的Layout布局文件的实例加载的。

  • 相关阅读:
    win10 访问远程文件夹 此共享需要过时的SMB1协议 你不能访问此共享文件夹
    Navicat 1142 SELECT command denied to user 'sx'@'xxx' for table 'user'
    MySQL 密码参数配置与修改 validate_password
    MySQL 命令行下更好的显示查询结果
    MySQL 数据库的存储结构
    MySQL实验 内连接优化order by+limit 以及添加索引再次改进
    MySQL实验 子查询优化双参数limit
    MySQL 索引结构 hash 有序数组
    MySQL 树形索引结构 B树 B+树
    hbase2.1.9 centos7 完全分布式 搭建随记
  • 原文地址:https://www.cnblogs.com/mada0/p/4829894.html
Copyright © 2011-2022 走看看