[英]onClickListener and setOnTouchListener doesn't work in React Native
In my Android Native code I have written an OnClick Listener for a PopUp I need to display in my onCreate(). 在我的Android本机代码中,我为需要显示在onCreate()中的PopUp编写了一个OnClick侦听器。
I want to write this in native rather than JS. 我想用本机而不是JS编写。
public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar,null);
PopupWindow popupWindow = new PopupWindow(
popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
popupView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Popup was Clicked", "I clicked here");
}
});
}
}
The popup shows, however the onClickListner doesn't get triggered and also the setTouchListener doesn't work either. 会显示弹出窗口,但是不会触发onClickListner,而且setTouchListener也不会起作用。 Am I missing some files??
我缺少一些文件吗?
You have set the root of the inflated popupView layout to null, which means it might not have correct layoutparams which may affect a touch interaction. 您已将膨胀的popupView布局的根设置为null,这意味着它可能没有正确的layoutparams,这可能会影响触摸交互。
Try rearranging it so you can do this: 尝试重新排列它,以便执行此操作:
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar, rootLayout, false);
That way the popupView will inherit the layoutparams from rootLayout, but will still not be attached to that view as the final argument (attachToRoot) is false. 这样,popupView将从rootLayout继承layoutparams,但是由于最终参数(attachToRoot)为false,因此popupView仍不会附加到该视图。 See if the onClickListener responds after that.
查看onClickListener之后是否响应。
You've also not used setContentView(View view)
in onCreate, though maybe that's normal when you inherit from ReactActivity, not sure. 您还没有在onCreate中使用
setContentView(View view)
,尽管不确定从ReactActivity继承时这是正常的。
I managed to solve this issue by setting the pointzipreviewtoolbar layout elements to an onClick. 我设法通过将pointzipreviewtoolbar布局元素设置为onClick来解决此问题。 Which in my case, inside the pointzipreviewtoolbar had an ImageButton called preViewCapture.
在我的情况下,在pointzipreviewtoolbar内部有一个名为preViewCapture的ImageButton。 I then set this to on OnClickListner and it got triggered.
然后,将其设置为OnClickListner并触发它。
public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar,null);
PopupWindow popupWindow = new PopupWindow(
popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
ImageButton image = (ImageButton) popupView.findViewById(R.id.preViewCapture);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Popup was Clicked", "I clicked here");
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.