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布局文件的实例加载的。

  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/mada0/p/4829894.html
Copyright © 2011-2022 走看看