以编程方式为场景中的按钮设置onClickListeners [英] Setting onClickListeners for buttons in scenes programmatically
问题描述
我有2种包含相同按钮的布局
I have 2 layouts which contain the same buttons
layout_1.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_1"
android:text="button2"
android:background="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
和
layout_2.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_1"
android:text="button2"
android:background="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
请假设这些都是有效的布局,等等(我只是添加相关代码.)
Please assume these are all valid layouts etc.(I am just adding the relevant code.).
所以在我的片段中,我充气并在onCreateView
中使用layout_1.xml
.我想使用button_1
在两个场景之间切换.
我可以在onCreateView()
期间在layout_1.xml
中为button_1
设置侦听器.
问题是试图在第二个视图中的那个按钮上设置一个监听器.收听者不会为第二个场景(使用layout_2.xml
)激活.因此,我无法在两个场景之间切换.有没有办法实现这一目标?
So in my fragment ,I inflate and use layout_1.xml
in onCreateView
.I want to toggle between the 2 scenes using button_1
.
I can set the listener for button_1
in layout_1.xml
during the onCreateView()
.
The problem is trying to set a listener on that button in the second view.i.e. the listener does not activate for the second scene(with layout_2.xml
).And hence i canot toggle between the 2 scenes.Is there a way to achieve this?
推荐答案
通常,使用相同的id
具有多个视图不是一个好主意.这就是造成混乱的原因.
In general, it is not a good idea to have multiple views with the same id
. This is what caused the confusion here.
注意:以下是OP使用的适合其特定需求的解决方案:
Note: Below is the solution used by OP that was suitable for their specific needs:
一个简单的解决方案是在XML文件中使用onClick
属性.您可以将相同的onClick方法分配给多个项目.像这样:
One simple solution is to use the onClick
attribute in the XML file. You can assign the same onClick method to multiple items. Like this:
然后在您的activity.java中添加以下内容:
And in your activity.java add this:
public void buttonClicked(View v){
Log.d("TAG","Button clicked!!"
// do stuff here
}
第二个选项:
使用button_1
的id
为一个按钮设置侦听器时,不会为两个按钮都设置listener
,而是仅为第一个按钮设置监听器.如果要为两者设置相同的listener
,则只需为这些按钮分配不同的ids
,然后为它们分配相同的listener
.
When you set a listener for one button with the id
of button_1
, it does not set the listener
for both buttons, it only sets it for the first one. If you want to set the same listener
for both, all you need to do is to assign these button different ids
and then assign them the same listener
.
这是您应该做的:
Listener myListener = new Listener(){.. blah blah....};
((Button) findViewById(R.id.some_id)).setListerner(myListener);
((Button) findViewById(R.id.some_other_id)).setListerner(myListener);
第三个选项:
findViewById(R.id.id_of_layout1).findViewById(R.id.button_1)
findViewById(R.id.id_of_layout2).findViewById(R.id.button_1)
在这种情况下,您需要向布局文件中添加一些ID,例如:layout_1.xml:
in this case, you need add some id to your layout files, for example: layout_1.xml:
<RelativeLayout
android:id="+id/id_of_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_1"
android:text="button2"
android:background="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
这篇关于以编程方式为场景中的按钮设置onClickListeners的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!