Android:复合控件中的按钮会干扰ListView的ContextMenu

Android:复合控件中的按钮会干扰ListView的ContextMenu,第1张

概述我在这里遇到的问题是,列表项xml中的2个按钮似乎干扰了主活动中的ContextMenu,从而防止了在长按列表项时它膨胀.只是注意,按钮本身可以正常工作.当我删除它们时,ContextMenu可以正常工作.列表项xml:<com.anna.mytallykeeper.views.TallyItemViewxmlns:android="http://schemas.a

我在这里遇到的问题是,列表项xml中的2个按钮似乎干扰了主活动中的ContextMenu,从而防止了在长按列表项时它膨胀.只是注意,按钮本身可以正常工作.当我删除它们时,ContextMenu可以正常工作.

列表项xml:

<com.anna.mytallykeeper.vIEws.TallyItemVIEwxmlns:androID="http://schemas.androID.com/apk/res/androID"androID:orIEntation="vertical"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"><TextVIEw androID:ID="@+ID/item_description"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:gravity="center_horizontal"    androID:textSize="40px"    androID:textcolor="@color/dijon" /><relativeLayout    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content">    <button androID:ID="@+ID/decrement_button"        androID:layout_wIDth="80px"        androID:layout_height="80px"        androID:layout_centerVertical="true"        androID:background="@drawable/minus_button_1" />    <button androID:ID="@+ID/increment_button"        androID:layout_wIDth="80px"        androID:layout_height="80px"        androID:layout_centerVertical="true"        androID:layout_alignParentRight="true"        androID:background="@drawable/add_button_1" />    <TextVIEw androID:ID="@+ID/item_count"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_centerVertical="true"        androID:layout_toleftOf="@ID/increment_button"        androID:layout_toRightOf="@ID/decrement_button"        androID:gravity="center_horizontal"        androID:textSize="80px"        androID:textcolor="@color/dijon" /></relativeLayout></com.anna.mytallykeeper.vIEws.TallyItemVIEw>

main.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:background="@color/piplup">    <ListVIEw androID:ID="@androID:ID/List"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent" /></linearLayout>

从主要活动(继承自ListActivity):

    public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    app = (MyTallyKeeperApplication) getApplication();    adapter = new TallyItemlistadapter(this, app.getTallyItems());    setlistadapter(adapter);    Utility.logI((getListVIEw() == null ?            "ListVIEw is null" : "ListVIEw is not null"));    Utility.logI((getListVIEw().getID() ==            androID.R.ID.List ? "ListVIEw ID is correct" :"ListVIEw ID is not correct"));    registerForContextMenu(getListVIEw());}//public voID onCreateContextMenu(ContextMenu menu, VIEw v, ContextMenuInfo menuInfo)     {    Utility.logI(MyTallyKeeperMain.class.getSimple@R_404_6889@() + ".onCreateContextMenu");    super.onCreateContextMenu(menu, v, menuInfo);    if (v.getID() == androID.R.ID.List) {        getMenuInflater().inflate(R.menu.item_menu, menu);    }}

从列表适配器类(继承自BaseAdapter):

    public VIEw getVIEw(int position, VIEw vIEw, VIEwGroup parent) {    TallyItemVIEw tiv;    if (vIEw == null) {        tiv = (TallyItemVIEw)VIEw.inflate(context, R.layout.tally_item_row, null);    } else {        tiv = (TallyItemVIEw)vIEw;    }    tiv.setTallyItem(tallyItems.get(position));    tiv.getDecrementbutton().setonClickListener(new DecrementListener(position));    tiv.getIncrementbutton().setonClickListener(new IncrementListener(position));    return tiv;}

解决方法:

我终于想出了解决按钮干扰ContextMenu的方法:将每个按钮的focusable属性设置为false.

    <button androID:ID="@+ID/increment_button"        androID:layout_wIDth="80px"        androID:layout_height="80px"        androID:layout_centerVertical="true"        androID:layout_alignParentRight="true"        androID:focusable="false"        androID:background="@drawable/add_button_1" />
总结

以上是内存溢出为你收集整理的Android:复合控件中的按钮会干扰ListView的ContextMenu全部内容,希望文章能够帮你解决Android:复合控件中的按钮会干扰ListView的ContextMenu所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1070209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存