前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[android] 练习PopupWindow实现对话框

[android] 练习PopupWindow实现对话框

作者头像
唯一Chat
发布2019-09-10 16:35:21
4670
发布2019-09-10 16:35:21
举报
文章被收录于专栏:陶士涵的菜地

练习使用Dialog实习对话框

代码语言:javascript
复制
package com.example.tsh;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Dialog实现对话框
        Dialog dialog=new Dialog(this,R.style.Theme_Tsh_Dialog);
        dialog.setContentView(R.layout.activity_two);
        dialog.show();
        
        
    }
}

styles.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    
    <!-- 自定义对话框主题 -->
    <style name="Theme.Tsh.Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@null</item>
    </style>
</resources>

练习使用PopupWindow实现对话框

代码语言:javascript
复制
package com.example.tsh;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Dialog实现对话框
//        Dialog dialog=new Dialog(this,R.style.Theme_Tsh_Dialog);
//        dialog.setContentView(R.layout.activity_two);
//        dialog.show();
        
        //使用PopupWindow实现对话框
        Button dropDown=(Button) findViewById(R.id.bt_dropdown);
        dropDown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                View anchor=View.inflate(MainActivity.this, R.layout.activity_two, null);
                final PopupWindow popupWindow=new PopupWindow(anchor, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
                
                //点击空白隐藏
                popupWindow.setTouchable(true);
                popupWindow.setTouchInterceptor(new OnTouchListener() {
                    
                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        popupWindow.dismiss();
                        return false;
                    }
                });
                popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                popupWindow.showAsDropDown(arg0);
                
            }
        });
        
    }
}

activity_main.xml

代码语言:javascript
复制
<?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" >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一个界面"/>
    <Button 
        android:id="@+id/bt_dropdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示下拉"
        />
</LinearLayout>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-07-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档