如何将视图寻呼机的当前聚焦视图提供给我的自定义 onClick 方法?
我有一个带有 ViewPager 的 Activity,并且有三个布局。在每个布局中,每个按钮都有 10 个按钮,我想让每个按钮在单击时都能发挥作用。因此,如果我采用为每个按钮定义 onClick() 方法的简单方法,这将是有趣且令人厌烦的,因为我将有 30 个这样的 setOnClickListener(this) 调用和 30 个 onClick 方法。
在浏览开发者网站时,我发现了一个非常重要的功能 android :onClick,使用它我可以将所有键注册到 xml 文件本身中的自定义 onClick 方法,然后单击按钮时,将调用 Activity 的此方法。
但就我而言,使用视图寻呼机我已经将视图带入屏幕。我在我的 Activity 中注册了一个名为 onClickTest() 的自定义 onClick 方法。
public void onClickTest(View v) {
int id = v.getId();
String idname = getResources().getResourceEntryName(id);
Log.i("Sen", "clicked view = "+idname);
switch (id) {
case R.id.btn_test:
testActivity();
break;
default:
break;
}
但是当我单击 viewPager 视图中的按钮时,我无法执行 onClickMethod。我怀疑它没有得到视图。
这是我的ViewPager的实例化方法
public Object instantiateItem(View collection, int position) {
final LayoutInflater inflater = (LayoutInflater) TestActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
View v0 = inflater.inflate(R.layout.layout1, null, false);
((ViewPager) collection).addView(v0, 0);
return v0;
default:
return true;
}
}
layout1有10个键,btn_test是其中的一个键。
你能告诉我如何让它发挥作用吗?
谢谢,
森
I have an Activity with a ViewPager and I have three layouts. In each layout I have 10 buttons each and i would like to make each button do function on a click. So if i go with the trivial method of defining onClick() method for each button, it is going to be funny and tiresome, because I will have 30 of these setOnClickListener(this) calls and 30 onClick Methods.
On going through the Developers site i found a very important piece of function android:onClick, using which i can register all the keys to a custom onClick method in the xml file itself, and then on clicking the button, this method of the Activity will be called.
But In my case, using the View pager i have already brought a view into the screen. I have registered a custom onClick Method called onClickTest() in my Activity.
public void onClickTest(View v) {
int id = v.getId();
String idname = getResources().getResourceEntryName(id);
Log.i("Sen", "clicked view = "+idname);
switch (id) {
case R.id.btn_test:
testActivity();
break;
default:
break;
}
But when i click the button in the viewPager view, i am not able to execute my onClickMethod. I suspect it is not getting the view.
This is the instantiate method of my ViewPager
public Object instantiateItem(View collection, int position) {
final LayoutInflater inflater = (LayoutInflater) TestActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
View v0 = inflater.inflate(R.layout.layout1, null, false);
((ViewPager) collection).addView(v0, 0);
return v0;
default:
return true;
}
}
The layout1 has 10 keys and btn_test is one key in it.
Could you please tell me how to make this working?
Thanks,
Sen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题有点脱节。您讨论了多个
onClick()
,然后讨论了android:onClick
的学习,然后跳转到“注册”一个onClick()
监听器您没有说明onClickTest()
与什么相关。您可以选择其中任何一种方法并使其发挥作用。尝试将
android:onClick="onClickTest"
添加到 XML 中的按钮btn_test
中。如果您已经这样做了,请下次展示出来,这样我们就不会猜测您已经做了什么:)。Your question is a bit disjoint. You talk about multiple
onClick()
, then talk about learning aboutandroid:onClick
, then jump to "registering" anonClick()
listener though you don't say whatonClickTest()
is tied to.You could choose any one of those methods and get it working. Try adding
android:onClick="onClickTest"
to your buttonbtn_test
in the XML. If you already have, show that next time so we're not left guessing what you've already done :).