Android----------PopWindow

1.PopWindow的效果为:

 

 2.首先点击 PopWindow练习 进入   

 

 3. 点击最喜爱的科目 弹出 列表界面 

 

4.  再次点击消失  

 

 5.当点击数学时  弹出 

 

 

 6.以此类推 语文也一样。

 

 

 

 

 

 

JAVA:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
 
public class PopupActivity extends AppCompatActivity {
 
      private Button btn_popup;
      private PopupWindow mpopup;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup);
        btn_popup=findViewById(R.id.btn_popup);
        btn_popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View popview =getLayoutInflater().inflate(R.layout.layout_pop,null);
                TextView textView1=popview.findViewById(R.id.shuxue);
                textView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mpopup.dismiss();
                        Toast.makeText(PopupActivity.this,"数学课很好玩",Toast.LENGTH_SHORT).show();
                    }
                });
                TextView textView2=popview.findViewById(R.id.yuwen);
                textView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(PopupActivity.this,"语文课很好玩",Toast.LENGTH_SHORT).show();
                    }
                });
                TextView textView3=popview.findViewById(R.id.yingyu);
                textView3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(PopupActivity.this,"英语课很好玩",Toast.LENGTH_SHORT).show();
                    }
                });
                 mpopup=new PopupWindow(popview,btn_popup.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                 mpopup.setOutsideTouchable(true);
                 mpopup.setFocusable(true);
                 mpopup.showAsDropDown(btn_popup);
            }
        });
    }
}

  XML1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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"
    android:padding="10dp">
 
    <Button
        android:id="@+id/btn_popup"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="最喜爱的科目"
        android:layout_gravity="center"
        android:layout_marginTop="200dp"/>
 
</LinearLayout>

  XML2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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"
    android:padding="15dp"
    android:background="@drawable/bg_dropdown">
    <TextView
        android:id="@+id/shuxue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="数学"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#A9A9A9"/>
    <TextView
        android:id="@+id/yuwen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="语文"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#A9A9A9"/>
    <TextView
        android:id="@+id/yingyu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="英语"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"/>
 
</LinearLayout>

  

 

posted @   快乐阿星  阅读(131)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
点击右上角即可分享
微信分享提示