![](/img/trans.png)
[英]How do I call findViewWithTag in the OnCreate() method of an activity?
[英]How do i assign view in onCreate method in an activity android
我有以下通過 View 的方法
private void doSomething(View view){ }
我遇到的問題是如何在 Activity 的 onCreate 方法中調用此視圖,我將不得不傳遞視圖
例如
View view;
doSomething(view)
如何分配視圖/實例化視圖,正在使用 getView() 但它不起作用
喜歡
view = getView()
例如片段 onViewCreatedMethod 有一個參數視圖,我可以在調用它時將其分配給該方法。 下面的例子
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
doSomething(view);
}
是否可以在片段的 onCreate 方法中執行此操作,我可以在活動的 onCreate 方法中解析 View arguments
在您的 onCreate 方法中,一旦您調用了setContentView
,您就可以使用findViewById
來獲取您想要的任何視圖。 如果你出於某種原因想要根視圖,你可以通過android.R.id.content
。
例如:
@Override
public void onCreate(Bundle savedInstanceState) {
// Replace your_layout_id with your Activity layout ID
setContentView(R.layout.your_layout_id);
View rootView = findViewById(android.R.id.content);
// Replace with whatever ID/View type you have in your code
// Button and your_button_id are just examples
Button button = findViewById(R.id.your_button_id);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.