kotlin-android-extensions 在Fragm

2020-06-15  本文已影响0人  XII01
 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      //一定不要在这里用ID,会报空指针
        return inflater.inflate(R.layout.fragment_code, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
       //在这里直接用id
        getCode.setOnClickListener {  }
    }

如果想知道为什么请往下面看,直接上kotlin 字节码

   @Nullable
   public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      Intrinsics.checkParameterIsNotNull(inflater, "inflater");
      return inflater.inflate(layout.fragment_code, container, false);
   }

   public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
      Intrinsics.checkParameterIsNotNull(view, "view");
      super.onViewCreated(view, savedInstanceState);
      ((TextView)this._$_findCachedViewById(id.getCode)).setOnClickListener((OnClickListener)null.INSTANCE);
   }

看kotlin源码步骤tools->kotlin->kotlin show Bytecode ->Decompile,就可以看到对应的文件

上一篇下一篇

猜你喜欢

热点阅读