将EditText添加到WindowManager后,无法单击它



我正在非常动态地创建一个布局。我正在做的是,创建一个EditText,将其添加到RelativeLayout对象中,并通过WindowManager将其放到UI中。完成此操作后,如何使编辑文本能够键入?这是我的代码

 RelativeLayout layout = new RelativeLayout(this);
 private EditText response = new EditText(this); 
 **Set width/height/extra**
 layout.addView(response, params);  //Params are set properly
 WindowManager myWindow = (WindowManager) getSystemService(WINDOW_SERVICE);
 myWindow.addView(layout, params_window);

编辑:我正在尝试实现一个弹出消息,就像在应用程序LilyPad中一样(我不确定你是否知道)。我正在尝试动态地更改文本。以下是我所做的:

  LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  myView = layoutInflater.inflate(R.layout.popups, null );
  message = (TextView)myView.findViewById(R.id.message);
  message.setText(text);

然后,我遵循@androiddeveloper的方法。但是我在message.setText(text)部分得到了一个空指针异常。Message是一个初始化为TextView对象的消息,我检查了popups.xml文件中是否有TextView id’d消息此问题已解决我没有启动windowManager。很抱歉忽略了这个,请在Edit III 中查看我的问题

第二版:这是我想要的另一张照片。https://i.stack.imgur.com/K4maH.jpg灰色框位于XML文件的RelativeLayout中。例如,如果我在我的脸书墙上,我希望脸书墙出现在顶部的黑色区域,并希望它能够在脸书墙中滚动,而不会受到我的应用程序的干扰(比如在LilyPad中)。如果我做

      LayoutInflater layoutInflater =    
      (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  myView = layoutInflater.inflate(R.layout.popups, null );

然后用Transparent w.e标志配置WindowManager,再加上我的视图,背景屏幕就不起作用了。背景不会暂停,但我不喜欢滚动浏览主屏幕。问题似乎是RelativeLayout占据了整个屏幕。这是我在xml文件中定义的RelativeLayout属性

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 

第四版:https://lh4.ggpht.com/gnNfCsUU7cTCTedhny-wej-nbGmL1fktcw5qo92CCOLQ9ySG5t4pCRKYSt7u--kjpw=h900-rw这是LilyPad 的图片

以下是如何将视图置于一切之上的方法:

final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=...
final ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null)
  parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=view.getLayoutParams().width;
param.height=view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW

如果您希望轻松地查看视图的外观,可以将视图放在XML中。我编写的代码将把它从布局中分离出来,并放到窗口本身。

我刚刚将PixelFormat.RGBA_888添加到layoutParams,然后键盘显示

最新更新