android - 将 EditText 添加到 WindowManager 后无法单击

标签 android android-edittext

我正在非常动态地创建布局。我正在做的是,创建一个 EditText,将其添加到 RelativeLayout 对象并通过 WindowManager 将其放入 UI。执行此操作后如何让 edittext 能够输入?这是我的代码的一部分

 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);

之后,我将按照@android 开发人员的方法进行操作。但是我在 message.setText(text) 部分得到了一个空指针异常。消息被初始化为一个 TextView 对象,我检查了 popups.xml 文件中有一个 TextView id'd 消息。 已修复 我没有启动 windowManager。对不起,请忽略这个请在 Edit III 中查看我的问题

编辑 II:这是我想要的另一张图片。 http://imgur.com/yXJ36n1灰色框位于 XML 文件的 RelativeLayout 中。前任。如果我在我的 facebook 墙上,我希望 facebook 墙出现在顶部的黑色区域,并希望它能够在 facebook 墙上滚动而不受我的应用程序干扰(就像在 LilyPad 中)。如果我这样做

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

然后使用透明 w.e 标志配置 WindowManager,并添加我的 View ,背景屏幕不起作用。背景不会暂停,但我不喜欢滚动浏览主屏幕。问题似乎是 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 的图片

最佳答案

下面是如何将 View 置于一切之上:

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

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

关于android - 将 EditText 添加到 WindowManager 后无法单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18424613/

相关文章:

android - 使用为 android 编译的 LibVLC 显示来自 MPEG2 流的 DVB 字幕

android - 在方向更改时以编程方式调整 EditText 的大小

android - 滚动列表中的 EditText 项目在滚动离开屏幕时会丢失其更改

AndroidAnnotations + Instant App - 找不到生成的 <applicationId>.R 类

android - 摇动设备以启动 Android 中的应用程序

android - java.lang.UnsatisfiedLinkError 与 android 5.0 一起使用时

android - Facebook android sdk 下载错误

android - 使用 TextInputLayout 时更改 EditText 提示颜色

android-layout - 如何在不可见的文本字段中输入文本

java - 在 TextView 和 EditText 之间切换