Cannot click on EditText after adding it to Window

2020-04-09 16:06发布

I am creating a layout pretty much dynamically. What I am doing is, creating an EditText, adding it to a RelativeLayout object and putting it to UI through WindowManager. How do I get the edittext to be able to type after I do this? Here is a jist of my code

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

EDIT: I am trying to achieve a popup message like in the app LilyPad (I am not sure if you know about it). I am trying to change the text dynamically. Here is what I did:

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

Then after that, I am following @android developer's method. But I am getting a null pointer exception in the message.setText(text) part. Message is a initialized as a TextView object and I checked there is a TextView id'd message in the popups.xml file. THIS IS FIXED I did not initiate the windowManager. Sorry ignore this please Please see my issue in Edit III

EDIT II: Here is another picture of what I want. http://imgur.com/yXJ36n1 The gray boxes are in RelativeLayout in an XML file. Ex. if I am on my facebook wall I want the facebook wall to appear on the black area on top and want it to be able scrollable in the facebook wall without my app interfering (like in LilyPad). If I do

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

then configure WindowManager with the Transparent w.e flags, and add my view, the background screen is not functional. Background doesn't pause but I cannot like scroll through homescreens for example. The problem seems to be that the RelativeLayout takes up the entire screen. This is my RelativeLayout properties defined in the xml file

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

EDIT IV: https://lh4.ggpht.com/gnNfCsUU7cTCTedhny-wej-nbGmL1fktcw5qo92CCOLQ9ySG5t4pCRKYSt7u--kjpw=h900-rw Here is a pictureo f LilyPad

1条回答
时光不老,我们不散
2楼-- · 2020-04-09 16:46

here's how you put a view on top of everything:

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

you can put the view inside XML if you wish to easily see how it should look like. the code i've written will detach it from your layout and put it on the window itself.

查看更多
登录 后发表回答