using findviewbyid in a class that does NOT extend Activity in android

前端 未结 4 623
无人及你
无人及你 2020-12-13 06:35

I have a class that is currently extending Activity and I have methods like findViewById, ArrayAdapter etc. I want to turn it into an independent class but all the above met

4条回答
  •  囚心锁ツ
    2020-12-13 06:39

    Please try the following:

    public static View getLayoutByRes(@LayoutRes int layoutRes,@Nullable ViewGroup viewGroup)
    {
        final LayoutInflater factory = getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return factory.inflate(layoutRes, viewGroup);
    }
    
    TextView tv = (TextView) getLayoutByRes(R.layout.xxx ,Null).findViewById(R.id.editd);
    

提交回复
热议问题