弹出窗口是不是在我工作的活动 [英] Popup Window is not work in my activity

查看:432
本文介绍了弹出窗口是不是在我工作的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在一个应用程序中实现弹出窗口。我有<样本code href=\"http://javabeginnerstutorial.com/android-tutorial/how-to-create-popups-in-android-2/#comment-2236\"相对=nofollow>这。如果我实现我的这个活动code比它不工作。所以我不能发现我的错误。任何一个可以帮助我。

Unlock_hud.java:

  = INF(LayoutInflater)getApplicationContext()。getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    MyView的= inf.inflate(R.layout.unlock_launcher,NULL);
    收到=(按钮)myview.findViewById(R.id.receive1);
    拒绝=(按钮)myview.findViewById(R.id.reject12);
    slidehandlebutton =(按钮)myview.findViewById(R.id.slidehandlebutton);
    slidehandlebutton.setOnClickListener(本);
    处理器公顷=新的处理程序();
    ha.postDelayed(新的Runnable(){        @覆盖
        公共无效的run(){
            ActivityManager mActivityManager =(ActivityManager)getSystemService(活动);
            清单&LT; RunningTaskInfo&GT; list_running = mActivityManager
                    .getRunningTasks(2147483647);
            清单&LT; RecentTaskInfo&GT; list_recent = mActivityManager
                    .getRecentTasks(2147483647,1);            Log.d(主,跑+ list_running.get(0).topActivity);
            Log.d(主,最近+ list_recent.get(0).baseIntent);
        / *意图homeIntent =新意图(Intent.ACTION_MAIN);
            homeIntent.addCategory(Intent.CATEGORY_HOME);
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_ preVIOUS_IS_TOP);
            startActivity(homeIntent); * /
            // Log.d(主,自由记忆:+ getUsedMemorySize());
            preFS = preferenceManager
                    .getDefaultShared preferences(getApplicationContext());        }    },500)
}@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    Log.d(主,的OnDestroy);
    // lock.reenableKeyguard();
    如果(call_flag&安培;!&安培;!WM2 = NULL){
        如果(receive_flag){
            如果(isaddView){
                wm2.removeView(MyView的);
            }        }其他{
            wm2.removeView(new_view);
        }        edit.putBoolean(旗帜,FALSE);
        edit.commit();
        call_flag = TRUE;
        如果(CheckMissCall.bmp!= NULL){
            CheckMissCall.bmp.recycle();
            CheckMissCall.bmp = NULL;
        }    }}
@覆盖
公共无效的onClick(查看为arg0){
    开关(arg0.getId()){    案例R.id.slidehandlebutton:
         如果(P!= NULL)
               showPopup(Unlock_hud.this页);
        打破;
    }
}公共无效onWindowFocusChanged(布尔hasFocus){       INT [] =位置INT新[2];
       Button按钮=(按钮)MyView的。 findViewById(R.id.slidehandlebutton);       //获取x,y位置,并将其存储位置[]数组中
       //位置[0] = X,位置[1] = Y。
       button.getLocationOnScreen(位置);       //初始化为x点,和y位置
       P =新点();
       p.x =位置[0];
       p.y =位置[1];
    }私人无效showPopup(上下文的背景下,P2点){
    // TODO自动生成方法存根
    INT popupWidth = 180;
       INT popupHeight = 80;       //充气popup_layout.xml
       LinearLayout中的ViewGroup =(的LinearLayout)((活动)上下文).findViewById(R.id.popup);
       LayoutInflater layoutInflater =(LayoutInflater)上下文
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       查看布局= layoutInflater.inflate(R.layout.popup_layout,ViewGroup中);       //创建PopupWindow
       最后PopupWindow弹出=新PopupWindow(C);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(真);       //有些偏移对齐弹出有点权,有点下降,相对于按钮的位置。
       INT OFFSET_X = -200;
       INT OFFSET_Y = 20;       //清除默认的半透明背景
       popup.setBackgroundDrawable(新BitmapDrawable());       //显示弹出指定位置,+偏移。
       popup.showAtLocation(布局,Gravity.NO_GRAVITY,p.x + OFFSET_X,p.y + OFFSET_Y);       //获取一个参考关闭按钮,点击后关闭弹出。
       按钮关闭=(按钮)layout.findViewById(R.id.stop);
       close.setOnClickListener(新OnClickListener(){         @覆盖
         公共无效的onClick(视图v){
           popup.dismiss();
         }
       });
    }

现在我发现错误在(上下文)

 的LinearLayout的ViewGroup =(的LinearLayout)context.findViewById(R.id.popup);
       LayoutInflater layoutInflater =(LayoutInflater)上下文
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


解决方案

我已经用你的code。但似乎ok了。

活动的完整code段

 公共类TestPopupActivity扩展活动实现OnClickListener {    //屏幕上的x和查看按钮的Y的位置。
    点p;
    私人的LinearLayout持有人;
    私人LayoutInflater吹气;
    私有视图MyView的;
    私人按钮btn_show;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
       super.onCreate(savedInstanceState);
       的setContentView(R.layout.main);       支架=(的LinearLayout)findViewById(R.id.holder);
       吹气=(LayoutInflater)getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);       MyView的= inflater.inflate(R.layout.inflated_layout,NULL);
       btn_show =(按钮)myView.findViewById(R.id.show_popup);
       btn_show.setOnClickListener(本);
       holder.addView(MyView的);
    }    //获取x和y位置后,按钮在屏幕上绘制
    需要注意的是,我们无法获得位置在OnCreate()//(这一点很重要,
    //因为在那个阶段最有可能的视图尚未绘制,所以它会返回(0,0))
    @覆盖
    公共无效onWindowFocusChanged(布尔hasFocus){       INT [] =位置INT新[2];
      / * Button按钮=(按钮)myView.findViewById(R.id.show_popup); * /       //获取x,y位置,并将其存储位置[]数组中
       //位置[0] = X,位置[1] = Y。
       btn_show.getLocationOnScreen(位置);       //初始化为x点,和y位置
       P =新点();
       p.x =位置[0];
       p.y =位置[1];
    }    //显示弹出的方法。
    私人无效showPopup(Activity上下文,点p){
       INT popupWidth = 200;
       INT popupHeight = 150;       //充气popup_layout.xml
       LinearLayout中的ViewGroup =(的LinearLayout)context.findViewById(R.id.popup);
       LayoutInflater layoutInflater =(LayoutInflater)上下文
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       查看布局= layoutInflater.inflate(R.layout.popup_layout,ViewGroup中);       //创建PopupWindow
       最后PopupWindow弹出=新PopupWindow(背景);
       popup.setContentView(布局);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(真);       //有些偏移对齐弹出有点权,有点下降,相对于按钮的位置。
       INT OFFSET_X = 30;
       INT OFFSET_Y = 30;       //清除默认的半透明背景
       popup.setBackgroundDrawable(新BitmapDrawable());       //显示弹出指定位置,+偏移。
       popup.showAtLocation(布局,Gravity.NO_GRAVITY,p.x + OFFSET_X,p.y + OFFSET_Y);       //获取一个参考关闭按钮,点击后关闭弹出。
       按钮关闭=(按钮)layout.findViewById(R.id.close);
       close.setOnClickListener(新OnClickListener(){         @覆盖
         公共无效的onClick(视图v){
           popup.dismiss();
         }
       });
    }    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        开关(v.getId()){
        案例R.id.show_popup:
            如果(P!= NULL)
                   showPopup(TestPopupActivity.this页);
            打破;        默认:
            打破;
        }
    }
    }

屏幕截图

I have implemented Popup Window in one Application. I have sample code of This. If I implement this code in my activity than it's not Working. so i cant found my Error. can any one help me.

Unlock_hud.java:

inf = (LayoutInflater) getApplicationContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    myview = inf.inflate(R.layout.unlock_launcher, null);
    receive = (Button) myview.findViewById(R.id.receive1);
    reject = (Button) myview.findViewById(R.id.reject12);
    slidehandlebutton= (Button) myview.findViewById(R.id.slidehandlebutton);
    slidehandlebutton.setOnClickListener(this);


    Handler ha = new Handler();
    ha.postDelayed(new Runnable() {

        @Override
        public void run() {
            ActivityManager mActivityManager = (ActivityManager) getSystemService("activity");
            List<RunningTaskInfo> list_running = mActivityManager
                    .getRunningTasks(2147483647);
            List<RecentTaskInfo> list_recent = mActivityManager
                    .getRecentTasks(2147483647, 1);

            Log.d("main", "Running: " + list_running.get(0).topActivity);
            Log.d("main", "Recent: " + list_recent.get(0).baseIntent);
        /*  Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory(Intent.CATEGORY_HOME);
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
            startActivity(homeIntent);*/
            // Log.d("main", "Free Memory: " + getUsedMemorySize());
            prefs = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());



        }

    }, 500)
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("main", "Ondestroy");
    // lock.reenableKeyguard();
    if (!call_flag && wm2 != null) {
        if (receive_flag) {
            if (isaddView) {
                wm2.removeView(myview);
            }

        } else {
            wm2.removeView(new_view);
        }

        edit.putBoolean("flag", false);
        edit.commit();
        call_flag = true;
        if (CheckMissCall.bmp != null) {
            CheckMissCall.bmp.recycle();
            CheckMissCall.bmp = null;


        }

    }

}
@Override
public void onClick(View arg0) {
    switch (arg0.getId()) {

    case R.id.slidehandlebutton:
         if (p != null)
               showPopup(Unlock_hud.this, p);
        break;


    }
}

public void onWindowFocusChanged(boolean hasFocus) {

       int[] location = new int[2];
       Button button = (Button)myview. findViewById(R.id.slidehandlebutton);

       // Get the x, y location and store it in the location[] array
       // location[0] = x, location[1] = y.
       button.getLocationOnScreen(location);

       //Initialize the Point with x, and y positions
       p = new Point();
       p.x = location[0];
       p.y = location[1];
    }

private void showPopup(Context context, Point p2) {
    // TODO Auto-generated method stub
    int popupWidth = 180;
       int popupHeight = 80;

       // Inflate the popup_layout.xml
       LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
       LayoutInflater layoutInflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

       // Creating the PopupWindow
       final PopupWindow popup = new PopupWindow(c);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(true);

       // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
       int OFFSET_X =-200;
       int OFFSET_Y =20;

       // Clear the default translucent background
       popup.setBackgroundDrawable(new BitmapDrawable());

       // Displaying the popup at the specified location, + offsets.
       popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

       // Getting a reference to Close button, and close the popup when clicked.
       Button close = (Button) layout.findViewById(R.id.stop);
       close.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
           popup.dismiss();
         }
       });
    }

Now i found error In ( CONTEXT )

 LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
       LayoutInflater layoutInflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

解决方案

I have use your code. But it seems ok.

Full code snippet of Activity

public class TestPopupActivity extends Activity implements OnClickListener {

    //The "x" and "y" position of the "Show Button" on screen.
    Point p;
    private LinearLayout holder;
    private LayoutInflater inflater;
    private View myView;
    private Button btn_show;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       holder  = (LinearLayout) findViewById(R.id.holder);
       inflater = (LayoutInflater) getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

       myView = inflater.inflate(R.layout.inflated_layout, null);
       btn_show = (Button) myView.findViewById(R.id.show_popup);
       btn_show.setOnClickListener(this);
       holder.addView(myView);
    }

    // Get the x and y position after the button is draw on screen
    // (It's important to note that we can't get the position in the onCreate(),
    // because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

       int[] location = new int[2];
      /* Button button = (Button) myView.findViewById(R.id.show_popup);*/

       // Get the x, y location and store it in the location[] array
       // location[0] = x, location[1] = y.
       btn_show.getLocationOnScreen(location);

       //Initialize the Point with x, and y positions
       p = new Point();
       p.x = location[0];
       p.y = location[1];
    }

    // The method that displays the popup.
    private void showPopup(Activity context, Point p) {
       int popupWidth = 200;
       int popupHeight = 150;

       // Inflate the popup_layout.xml
       LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
       LayoutInflater layoutInflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

       // Creating the PopupWindow
       final PopupWindow popup = new PopupWindow(context);
       popup.setContentView(layout);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(true);

       // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
       int OFFSET_X = 30;
       int OFFSET_Y = 30;

       // Clear the default translucent background
       popup.setBackgroundDrawable(new BitmapDrawable());

       // Displaying the popup at the specified location, + offsets.
       popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

       // Getting a reference to Close button, and close the popup when clicked.
       Button close = (Button) layout.findViewById(R.id.close);
       close.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
           popup.dismiss();
         }
       });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.show_popup:
            if (p != null)
                   showPopup(TestPopupActivity.this, p);
            break;

        default:
            break;
        }
    }
    }

Screenshot

这篇关于弹出窗口是不是在我工作的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆