[英]onClickListener and setOnTouchListener doesn't work in React Native
在我的Android本機代碼中,我為需要顯示在onCreate()中的PopUp編寫了一個OnClick偵聽器。
我想用本機而不是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");
}
});
}
}
會顯示彈出窗口,但是不會觸發onClickListner,而且setTouchListener也不會起作用。 我缺少一些文件嗎?
您已將膨脹的popupView布局的根設置為null,這意味着它可能沒有正確的layoutparams,這可能會影響觸摸交互。
嘗試重新排列它,以便執行此操作:
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar, rootLayout, false);
這樣,popupView將從rootLayout繼承layoutparams,但是由於最終參數(attachToRoot)為false,因此popupView仍不會附加到該視圖。 查看onClickListener之后是否響應。
您還沒有在onCreate中使用setContentView(View view)
,盡管不確定從ReactActivity繼承時這是正常的。
我設法通過將pointzipreviewtoolbar布局元素設置為onClick來解決此問題。 在我的情況下,在pointzipreviewtoolbar內部有一個名為preViewCapture的ImageButton。 然后,將其設置為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.