在运行时Generte资源ID? [英] Generte resource ID at runtime?

查看:128
本文介绍了在运行时Generte资源ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个自定义的preference有搜索栏,到目前为止,它的工作原理,但有一个问题,我的搜索​​栏的ID被定义在XML中,所以当我尝试使用我的自定义$ P $ 2 pfernces然后我更新搜索栏,我不能区分这两个。

 搜索栏栏=(搜索栏)view.findViewById(R.id. preferenceSeekBar);

有没有办法在运行时生成自定义资源ID?
或者另一种方式来解决这个问题?

谢谢,
杰森


解决方案

  

在自定义preference的onBind ovverriden方法,你传递一个View视图,其中包含对当前preference活动各方面的意见。


有没有 onBind() preference 方法。有的一个 onBindView()方法。但是,查看传递给 onBindView()是什么查看 onCreateView返回(),或者从您指定的布局文件膨胀,如果你去这条路线的查看。它不是整个活动

请确保您所呼叫 findViewById()上是为创建的小部件的 preference ,不是与范围更广。

另外,请记住, preferenceActivity ListActivity 。如果您的自定义 preference 关闭屏幕滚动,其查看可以切换到自定义的另一个实例 preference 进行回收。

I am writing a custom preference with a seek bar, so far it works, except for one problem the ID of my seek bar is defined in XML, so when I try to use 2 of my custom prefernces and then I update the Seek Bar I cannot distinguish between the two.

SeekBar bar = (SeekBar)view.findViewById(R.id.PreferenceSeekBar);

Is there a way to generate a custom Resource ID at runtime? Or perhaps another way to solve this?

Thanks, Jason

解决方案

on the onBind ovverriden method of custom preference, you are passed a View view, which contains all the views on the current preference activity.

There is no onBind() method on Preference. There is an onBindView() method. However, the View passed to onBindView() is whatever View you returned from onCreateView(), or the View inflated from your specified layout file if you went that route. It is not the entire activity.

Make sure you are calling findViewById() on the widget that was created for your Preference, not something with broader scope.

Also, bear in mind that PreferenceActivity is a ListActivity. If your custom Preference scrolls off the screen, its View may be handed to another instance of your custom Preference for recycling.

这篇关于在运行时Generte资源ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆