上述代码转为java
时间: 2023-07-03 16:25:35 浏览: 46
以下是上述 Kotlin 代码的 Java 版本:
```
public class MyFragment extends Fragment {
private ImageView picture;
private TextView title1, task1, title2, task2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
picture = getActivity().findViewById(R.id.picture);
title1 = getActivity().findViewById(R.id.title1);
task1 = getActivity().findViewById(R.id.task1);
title2 = getActivity().findViewById(R.id.title2);
task2 = getActivity().findViewById(R.id.task2);
picture.setImageResource(R.drawable.frag1);
title1.setText("可回收物是指");
task1.setText("废纸张、废塑料、废玻璃制品、废金属、废织物等适宜回收、可循环利用的生活废弃品。");
title2.setText("可回收物投放要求");
task2.setText("1. 轻投轻放\n2. 清洁干燥,避免污染\n3. 废纸尽量平整\n4. 立体包装物请清空内容物,清洁后压扁投放\n5. 有尖锐边角的,应包裹后投放");
}
}
```
需要注意的是,上述代码中使用了 Kotlin 中的扩展函数 findViewById(),在 Java 中需要替换为 getActivity().findViewById()。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)