Android UI编程(3)——Popupwindow对话框

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

  • AlertDialog的位置固定,而PopupWindow的位置可以随意
  • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
PopupWindow在android.widget包下,弹出窗口的形式展示。官方文档对该控件的描述是:"一个弹出窗口控件,可用来显示任何视图(View),而且会浮动在当前活动(Activity)的顶部"。PopupWindow可以让我们实现多种自定义控件,例如:menu、alertdialog等弹窗似的View。
PopupWindow的位置按照有无偏移分为:有偏移和无偏移两种;按照参照物的不同分为:相对于某个控件(Anchor锚)和相对父控件。具体如下:
showAsDropDown(View anchor);->相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff);->相对某个控件的位置,偏移
showAtLocation(View parent, int gravity, int x, int y);->相对于父控件的位置(例如正中央Gravity.GENTER,下方Gravity.BOTTOM等),可以设置偏移或偏移
参考博客:
总结
1、RadioButton默认选择,只需要在对应的RadioButton中添加属性:android:checked="true"
2、PopupWindow需要先获取对应视图布局,然后调用PopupWindow构造方法去初始化一个PopupWindow实例化对象,如PopupWindow(popupWindowView,100,130);
参数1:对应的视图布局
参数2:指定PopupWindow的width
参数3:指定PopupWindow的height
AndroidManifest.xml——没有做任何修改,默认
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wxl.popupwindow"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.wxl.popupwindow.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
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:background="#00FF00"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/popup_window_textView"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="选择状态:" 
        android:textColor="@android:color/white" 
        android:textSize="20px" />
    
    <RadioGroup 
        android:id="@+id/popup_window_radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <RadioButton 
            android:id="@+id/popup_window_RadioButton1"
            android:text="在线" 
            android:checked="true"/> 
 
        <RadioButton 
            android:id="@+id/popup_window_RadioButton2"
            android:text="离线" /> 
 
        <RadioButton 
            android:id="@+id/popup_window_RadioButton3"
            android:text="隐身" /> 
    </RadioGroup>
    
    
</LinearLayout>
activity_main.xml
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/button01" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="以自己为Anchor,不偏移" /> 
 
    <Button 
        android:id="@+id/button02" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="以自己为Anchor,有偏移" /> 
 
    <Button 
        android:id="@+id/button03" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="以屏幕中心为参照,不偏移(正中间)" /> 
 
    <Button 
        android:id="@+id/button04" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="以屏幕下方为参照,下方中间" /> 

</LinearLayout>
MainActivity.java
package com.wxl.popupwindow;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener{
	PopupWindow popupWindow;
	View popupWindowView;//用来获取PopupWindow布局视图
	RadioGroup radioGroup;
	
	private Button mbutton01; 
    private Button mbutton02; 
    private Button mbutton03; 
    private Button mbutton04;
    
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mbutton01 = (Button) findViewById(R.id.button01); 
        mbutton02 = (Button) findViewById(R.id.button02); 
        mbutton03 = (Button) findViewById(R.id.button03); 
        mbutton04 = (Button) findViewById(R.id.button04); 
 
        mbutton01.setOnClickListener(this); 
        mbutton02.setOnClickListener(this); 
        mbutton03.setOnClickListener(this); 
        mbutton04.setOnClickListener(this); 
        
        initPopupWindow();
        
    }
	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		if (radioGroup == arg0 && R.id.popup_window_RadioButton1 == arg1)
		{
			Log.i("+++++++", "点击radioButton1");
		}
		if (radioGroup == arg0 && R.id.popup_window_RadioButton2 == arg1)
		{
			Log.i("+++++++", "点击radioButton2");
		}
		if (radioGroup == arg0 && R.id.popup_window_RadioButton3 == arg1)
		{
			Log.i("+++++++", "点击radioButton3");
		}
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) { 
        // 相对某个控件的位置(正左下方),无偏移 
        case R.id.button01: 
            if (popupWindow.isShowing())
            {
            	popupWindow.dismiss();
            }
            popupWindow.showAsDropDown(arg0); 
                      
            break; 
 
        // 相对某个控件的位置(正左下方),有偏移 
        case R.id.button02: 
        	if (popupWindow.isShowing())
            {
            	popupWindow.dismiss();
            }
            popupWindow.showAsDropDown(arg0, 50, 50);// X、Y方向各偏移50                     
            break; 
 
        // 相对于父控件的位置,无偏移 
        case R.id.button03: 
        	if (popupWindow.isShowing())
            {
            	popupWindow.dismiss();
            }
            popupWindow.showAtLocation(arg0, Gravity.CENTER, 0, 0); 
            break; 
 
        // 相对于父控件的位置,有偏移 
        case R.id.button04: 
        	if (popupWindow.isShowing())
            {
            	popupWindow.dismiss();
            }
            popupWindow.showAtLocation(arg0, Gravity.BOTTOM, 0, 50);
            break; 
 
        default: 
            break; 
        } 
	}
	
	public void initPopupWindow()
	{
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		popupWindowView = layoutInflater.inflate(R.layout.popup_window, null);
		radioGroup = (RadioGroup) popupWindowView.findViewById(R.id.popup_window_radioGroup); 
        radioGroup.setOnCheckedChangeListener(this); //单选钮监听事件
        
        // 创建一个PopupWindow 
        // 参数1:contentView 指定PopupWindow的内容 
        // 参数2:width 指定PopupWindow的width 
        // 参数3:height 指定PopupWindow的height 
        popupWindow = new PopupWindow(popupWindowView, 100, 130); 
 
	}
    
}
点击RadioButton的打印信息,测试点击事件



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值