我在使用片段的 onCreateView 中使用什么代替 findViewById
what do i use instead of findViewById in onCreateView using fragment
我是 Android 开发新手,当然还有 Fragments。
我有下面的代码,我想在片段中使用 findViewById 但是因为它在 onCreateView 上所以有错误
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main,null);
x2=(Button)getView().findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)getView().findViewById(R.id.display);
}
你可以试试这个。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.activity_main,container,false);
x2=(Button)v.findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)v.findViewById(R.id.display);
return v;
}
试试这个对你有帮助
View view=inflater.inflate(R.layout.activity_main,null);
x2=(Button)view.findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)view.findViewById(R.id.display);
return view;
我是 Android 开发新手,当然还有 Fragments。
我有下面的代码,我想在片段中使用 findViewById 但是因为它在 onCreateView 上所以有错误
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main,null);
x2=(Button)getView().findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)getView().findViewById(R.id.display);
}
你可以试试这个。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.activity_main,container,false);
x2=(Button)v.findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)v.findViewById(R.id.display);
return v;
}
试试这个对你有帮助
View view=inflater.inflate(R.layout.activity_main,null);
x2=(Button)view.findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)view.findViewById(R.id.display);
return view;