
我有两组布局,用按钮填充.有没有办法禁用相同布局的按钮之间的任何交互?当我单击一个按钮然后从同一布局中单击另一个按钮时,我不需要发生任何事情.对于属于上下布局的按钮,我用“和”标记每个按钮.我有5种.
我有一些相互作用beetwen按钮的oposite布局,并且工作正常(实际上交互在任何两个给定的按钮之间工作正常,这很糟糕),但我不希望相同布局的按钮之间的交互.
这是我的游戏课程,而不是全部,只是重要的部分.此外,我删除了其余按钮的代码,只留下4个按钮,每个布局2个,以节省一些空间.我从sqlite数据库导入一些文本,并随机将其设置为我的按钮.
final OnClickListener clickListener = new OnClickListener() { private button buttonClicked; public voID onClick(VIEw v) { button button = (button) v; button.getBackground().setcolorFilter(new lightingcolorFilter(0xFFFFFFFF, 0x003333)); if (buttonClicked == null) { // first button is clicked buttonClicked = button; } else { // second button is clicked if (buttonClicked.getTag().equals(button.getTag())) { button.getBackground().setcolorFilter(new lightingcolorFilter(0xFFFFFFFF, 0x66FF33)); buttonClicked.getBackground().setcolorFilter(new lightingcolorFilter(0xFFFFFFFF, 0x66FF33)); buttonClicked.setEnabled(false); button.setEnabled(false); } else { buttonClicked.setEnabled(false); buttonClicked.setTextcolor(color.GRAY); buttonClicked.getBackground().setcolorFilter(new lightingcolorFilter(0xFFFFFFFF, 0xFFCC99)); button.getBackground().clearcolorFilter(); } buttonClicked = null; } }; protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.game); a1 = (button) findVIEwByID(R.ID.bA1); a2 = (button) findVIEwByID(R.ID.bA2); b1 = (button) findVIEwByID(R.ID.bB1); b2 = (button) findVIEwByID(R.ID.bB2); nextQuestion(); } public voID nextQuestion() { TestAdapter mDbHelper = new TestAdapter(this); mDbHelper.createDatabase(); try{ mDbHelper.open(); Cursor c = mDbHelper.getTestData(generateWhereClause()); mAnsweredQuestions.add(c.getLong(0)); ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>(); ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>(); labelsA.add(new MyStruct(c.getString(2), "1")); // this tag should be the same to button that matches labelsB.add(new MyStruct(c.getString(3), "1")); labelsA.add(new MyStruct(c.getString(4), "2")); labelsB.add(new MyStruct(c.getString(5), "2")); Collections.shuffle(labelsA); Collections.shuffle(labelsB); pitanje.setText(c.getString(1)); a1.setText(labelsA.get(0).label); a1.setTag(labelsA.get(0).tag); a1.setonClickListener(clickListener); a1.getBackground().clearcolorFilter(); a1.setEnabled(true); b1.setText(labelsB.get(0).label); b1.setTag(labelsB.get(0).tag); b1.setonClickListener(clickListener); b1.getBackground().clearcolorFilter(); b1.setEnabled(true); a2.setText(labelsA.get(1).label); a2.setTag(labelsA.get(1).tag); a2.setonClickListener(clickListener); a2.getBackground().clearcolorFilter(); a2.setEnabled(true); b2.setText(labelsB.get(1).label); b2.setTag(labelsB.get(1).tag); b2.setonClickListener(clickListener); b2.getBackground().clearcolorFilter(); b2.setEnabled(true); } finally{ mDbHelper.close(); }XML:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" androID:background="@drawable/background"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="30dp" androID:gravity="top|center" androID:orIEntation="vertical" > <TextVIEw androID:ID="@+ID/tvPitanje" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Ovde ce biti postavljeno pitanje" androID:textcolor="#ffffff" androID:textSize="18sp" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="360dp" androID:orIEntation="horizontal" > <linearLayout androID:ID="@+ID/kolona1" androID:layout_wIDth="0dp" androID:layout_height="fill_parent" androID:layout_weight="50" androID:gravity="top" androID:orIEntation="vertical" androID:paddingleft="3dp" androID:paddingRight="2dp" > <button androID:ID="@+ID/bA1" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_gravity="clip_horizontal" androID:layout_marginBottom="1.5dp" androID:background="@drawable/buttons" androID:gravity="center_vertical|center_horizontal" androID:padding="0dp" androID:tag="l" androID:text="A1" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bA2" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A2" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> <button androID:ID="@+ID/bA3" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A3" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> <button androID:ID="@+ID/bA4" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A4" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> <button androID:ID="@+ID/bA5" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A5" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> <button androID:ID="@+ID/bA6" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A6" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> <button androID:ID="@+ID/bA7" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A7" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> <button androID:ID="@+ID/bA8" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:text="A8" androID:background="@drawable/buttons" androID:textcolor="#ffffff" androID:layout_margintop="1.5dp" androID:tag="l" androID:padding="0dp" androID:textSize="16sp" /> </linearLayout> <linearLayout androID:ID="@+ID/kolona2" androID:layout_wIDth="0dp" androID:layout_height="fill_parent" androID:layout_weight="50" androID:gravity="top" androID:orIEntation="vertical" androID:paddingleft="2dp" androID:paddingRight="3dp" > <button androID:ID="@+ID/bB1" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_gravity="top|fill_horizontal" androID:layout_marginBottom="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B1" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB2" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B2" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB3" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B3" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB4" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B4" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB5" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B5" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB6" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B6" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB7" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_marginBottom="1.5dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B7" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/bB8" androID:layout_wIDth="fill_parent" androID:layout_height="41dp" androID:layout_margintop="1.5dp" androID:background="@drawable/buttons" androID:gravity="top|center_horizontal" androID:tag="r" androID:text="B8" androID:textcolor="#ffffff" androID:textSize="16sp" /> </linearLayout> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:orIEntation="horizontal" androID:weightSum="3" > <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_weight="1" androID:gravity="center" > <button androID:ID="@+ID/bIzlaz" androID:layout_wIDth="80dp" androID:layout_height="42dp" androID:layout_gravity="center" androID:background="@drawable/buttons" androID:gravity="center" androID:text="Izlaz!" androID:textcolor="#ffffff" androID:textSize="20sp" /> </linearLayout> <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_weight="1" androID:gravity="center" > <TextVIEw androID:ID="@+ID/tvPoeni" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/tvscore" androID:textcolor="#ffffff" androID:textSize="21sp" /> </linearLayout> <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:layout_weight="1" androID:gravity="center" > <TextVIEw androID:ID="@+ID/tvVreme" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="30" androID:textcolor="#ff0000" androID:textSize="21sp" /> </linearLayout> </linearLayout></linearLayout>解决方法:
您可以使用getParent方法并比较两个视图的父级.如果父级是相同的,则按钮处于相同的布局中,不会发生任何事情.
改变你的别人:
} else { // second button is clicked……如果:
// only do stuff if buttons are in different layouts} else if (button.getParent () != buttonClicked.getParent()) { // second button is clicked 总结 以上是内存溢出为你收集整理的java – 如何禁用相同布局的按钮之间的交互?全部内容,希望文章能够帮你解决java – 如何禁用相同布局的按钮之间的交互?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)