【android开发】手机应用管理器的实现之实现popupWindow类对话框(二)

2 篇文章 0 订阅
1 篇文章 0 订阅

上一篇我们实现获取手机里面的所有应用程序的信息,并显示在列表中,今天我们主要利用popupWindow类实现一个对话框,对话框包括运行、分享、卸载、加锁。我们先看一下效果:


一、android的对话框的实现方式:

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
AlertDialog的位置固定,而PopupWindow的位置可以随意
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下:
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移。

二、PopupWindow对话框的实现:

首先我们要先建一个视图文件,用于存放四个控件,将这个view放到PopupWindow对象中

View popupView = View.inflate(MainActivity.this, R.layout.popup_item, null);
将view放到PopupWindow对象中:

popupWindow = new PopupWindow(popupView, 615, 150);
设置相关属性并将popupwindow显示出来:

Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
    popupWindow.setBackgroundDrawable(drawable);
    int x = location[0]+60;
    int y = location[1];
    //把PopupWindow显示出来 
    popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
写一个方法用于判断popupwindow是否显示:

//判断PopupWindow是不是存在,存在就把它dismiss掉  
    private void dismissPopupWindow()  
    {  
        if(popupWindow != null)  
        {  
            popupWindow.dismiss();  
            popupWindow = null;  
        }  
    }
三、项目具体实现过程及代码:

新建一个视图文件存放控件popup_item.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_app_popup"
    android:layout_width="230dip"
    android:layout_height="70dip"
    android:background="@drawable/local_popup_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <LinearLayout
        android:id="@+id/ll_app_start"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:scaleType="fitXY"
            android:src="@drawable/img2"
            android:contentDescription="@string/hello_world" />

        <TextView
            android:layout_width="35dip"
            android:layout_height="20dip"
            android:gravity="center_horizontal"
            android:text="运行" />
    </LinearLayout>

    <View
        android:layout_width="1dip"
        android:layout_height="50dip"
        android:background="#ff00ff66" />
    <LinearLayout
        android:id="@+id/ll_app_share"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:scaleType="fitXY"
            android:src="@drawable/img3"
            android:contentDescription="@string/hello_world" />
        <TextView
            android:layout_width="35dip"
            android:layout_height="20dip"
            android:gravity="center_horizontal"
            android:text="分享"/>
    </LinearLayout>
     <View
        android:layout_width="1dip"
        android:layout_height="50dip"
        android:background="#ff00ff66" />
    <LinearLayout
        android:id="@+id/ll_app_lock"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/iv_app_lock"
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:scaleType="fitXY"
            android:src="@drawable/img4_lock"
            android:contentDescription="@string/hello_world" />
        <TextView
            android:id="@+id/tv_app_lock"
            android:layout_width="35dip"
            android:layout_height="20dip"
            android:gravity="center_horizontal"
            android:text="加锁"/>
    </LinearLayout>
    
     <View
        android:layout_width="1dip"
        android:layout_height="50dip"
        android:background="#ff00ff66" />
    <LinearLayout
        android:id="@+id/ll_app_uninstall"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:layout_marginLeft="8dip"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="35dip"
            android:layout_height="35dip"
            android:scaleType="fitXY"
            android:src="@drawable/img1"
            android:contentDescription="@string/hello_world" />
        <TextView
            android:layout_width="35dip"
            android:layout_height="20dip"
            android:text="卸载" />
     </LinearLayout>

   
</LinearLayout>

在MainActivity类中添加代码,如下:

package com.xh.ui;


import java.util.List;
import com.example.appmanager.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.ScaleAnimation;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
/**
 * 类名称:MainActivity 
 * 类描述:系统主页面
 * 创建人:LXH 
 * 创建时间:2013-11-5 下午3:31:00 
 */
public class MainActivity extends Activity implements OnClickListener{

	private static final int GET_ALL_APP_FINISH = 1;  
    
    private ListView lv_app_manager;//应用信息列表  
    private LinearLayout ll_app_manager_progress; //进度条 
    private AppInfoProvider provider;  
    private AppManagerAdapter adapter;  
    private List<AppInfo> list;  
    private PopupWindow popupWindow;  
    @SuppressLint("HandlerLeak")  
    private Handler handler = new Handler()  
    {  
        public void handleMessage(Message msg)   
        {  
            switch(msg.what)  
            {  
                case GET_ALL_APP_FINISH :   
                    //进度条设置为不可见  
                    ll_app_manager_progress.setVisibility(View.GONE);  
                    adapter = new AppManagerAdapter();  
                    lv_app_manager.setAdapter(adapter);  
                    break;  
                      
                default :   
                    break;  
            }  
        };  
    };  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main_layout);  
          
        lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);  
        ll_app_manager_progress = (LinearLayout) findViewById(R.id.ll_app_manager_progress);  
        ll_app_manager_progress.setVisibility(View.VISIBLE);  
          
        /**
         * 开一个线程用于完成对所有应用程序信息的搜索  
         * 当搜索完成之后,就把一个成功的消息发送给Handler,
         * 然后handler把搜索到的数据设置进入listview里面  .
         * */
        new Thread()  
        {  
            public void run()   
            {  
                provider = new AppInfoProvider(MainActivity.this);  
                list = provider.getAllApps();  
                Message msg = new Message();  
                msg.what = GET_ALL_APP_FINISH;  
                handler.sendMessage(msg);  
            };  
        }.start();
        
        
        //实现点击listview触发事件
        lv_app_manager.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				dismissPopupWindow();
				//用来存放当前的item的坐标值,第一个是x的坐标,第二个是y的坐标 
				int[] location = new int[2];
				//把当前的item的坐标值放到int数组里面
				view.getLocationInWindow(location);
				View popupView = View.inflate(MainActivity.this, R.layout.popup_item, null);
				//new 一个PopupWindow出来
				popupWindow = new PopupWindow(popupView, 615, 150);
				//一定要给PopupWindow设置一个背景图片,不然的话,会有很多未知的问题的  
				//如没办法给它加上动画,还有显示会有问题等,  
				//如果我们没有要设置的图片,那么我们就给它加上了一个透明的背景图片
				Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
				popupWindow.setBackgroundDrawable(drawable);
	           
				int x = location[0]+60;
				int y = location[1];
				//把PopupWindow显示出来 
				popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
				//增加一个动画效果
				LinearLayout ll_app_popup = (LinearLayout) popupView.findViewById(R.id.ll_app_popup);
				ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
	            scaleAnimation.setDuration(300);
	            ll_app_popup.startAnimation(scaleAnimation);
			}
		});
        //listview滚动时判断一下对话框,防止对话框一直存在
        lv_app_manager.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				// TODO Auto-generated method stub
				dismissPopupWindow();
			}
			
			@Override
			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				dismissPopupWindow();
			}
		});
    }  
  //判断PopupWindow是不是存在,存在就把它dismiss掉  
    private void dismissPopupWindow()  
    {  
        if(popupWindow != null)  
        {  
            popupWindow.dismiss();  
            popupWindow = null;  
        }  
    } 
    //======================================================================  
      
    private class AppManagerAdapter extends BaseAdapter  
    {  
  
        @Override  
        public int getCount()  
        {  
            return list.size();  
        }  
  
        @Override  
        public Object getItem(int position)  
        {  
            return list.get(position);  
        }  
  
        @Override  
        public long getItemId(int position)  
        {  
            return position;  
        }  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent)  
        {  
            AppInfo info = list.get(position);  
            if(convertView == null)  
            {  
                View view = View.inflate(MainActivity.this, R.layout.app_manager_item, null);  
                AppManagerViews views = new AppManagerViews();  
                views.iv_app_icon = (ImageView) view.findViewById(R.id.iv_app_manager_icon);  
                views.tv_app_name = (TextView) view.findViewById(R.id.tv_app_manager_name);  
                views.iv_app_icon.setImageDrawable(info.getIcon());  
                views.tv_app_name.setText(info.getAppName());  
                view.setTag(views);  
                return view;  
            }  
            else  
            {  
                AppManagerViews views = (AppManagerViews) convertView.getTag();  
                views.iv_app_icon.setImageDrawable(info.getIcon());  
                views.tv_app_name.setText(info.getAppName());  
                return convertView;  
            }  
        }  
          
    }  
    /**
     * 用来优化listview的类  
     * */
    private class AppManagerViews  
    {  
        ImageView iv_app_icon;  
        TextView tv_app_name;  
    }
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}  
}

运行后效果就会出来了!到此,利用popupwindow类实现的对话框就结束了,过程应该说是相当的简单,需要说明在代码中也加了注释。大家可以下载源码详细参考一下,实例代码本人运行都是正常的。有什么问题,大家请在下面留言说明,大家在一起交流。下一篇我们将实现运行、分享、卸载功能,欢迎大家继续关注!





  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值