【问题标题】:Button onClick not detected with a ListView and ListActivity未使用 ListView 和 ListActivity 检测到按钮 onClick
【发布时间】:2011-05-26 18:39:22
【问题描述】:

我有以下布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">
 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:text="Height" 
        android:layout_height="wrap_content" 
        android:id="@+id/buttonHeight" 
        android:layout_width="wrap_content"
        android:layout_weight="15"
        android:onClick="OnClickHeight">
    </Button>
    <Button 
        android:text="Width" 
        android:layout_height="wrap_content" 
        android:id="@+id/buttonWidth" 
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/buttonHeight"
        android:layout_weight="1"
        android:onClick="onClickWidth">
   </Button>
 </LinearLayout>
 <ListView android:id="@android:id/list"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 android:dividerHeight="1px"
 android:drawSelectorOnTop="false"/>
 </RelativeLayout>

然后我有一个扩展 ListActivity 的类,其中我有我的 onClickWidth 和 onClickHeight 方法,例如:

 public void onClickWidth(View v) 
 {
   // does stuff
 }

但是,这些 onClick 事件并未得到处理。如果我将类更改为扩展 Activity,而不是 ListActivity,并从我的布局中删除 ListView,然后它被检测到!

我曾尝试设置 android:clickable="true"、使用 android:focusable 属性和其他各种东西,但我就是无法让它工作。我该如何解决这个问题,还是根本不允许这样做?

【问题讨论】:

    标签: android listview button onclick listactivity


    【解决方案1】:

    你能告诉我们你想做什么吗?为什么需要 ListActivity?

    抱歉编辑:P

    你可以看这里:How to handle ListView click in Android

    【讨论】:

    • 我需要显示一些数据的列表,这就是我使用ListActivity的原因。我将它与扩展 BaseAdapter 的类一起使用以填充此列表。
    • 以上链接与我无关,因为我知道如何检测 ListView 上的点击,但它是未检测到 onClick 的 Button。
    【解决方案2】:

    将 OnClicklistener 放入适配器本身。

    【讨论】:

    • 不幸的是这不起作用 - 我尝试将 OnClickListener 放在适配器的构造函数中,并在 getView 方法中尝试过,但没有运气。
    【解决方案3】:

    而不是在布局中使用 onClick 的东西,你有没有尝试过这样的事情:

    Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
    buttonWidth.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //do stuff
        }
    });
    

    然后对 buttonHeight 执行相同的操作。

    【讨论】:

    • 嗨,谢谢你的建议,但是我也试过了,即我把它放在我的 ListActivity 类的 OnCreate 中,但仍然没有运气。我还尝试在我的 ListActivity 类上“实现 OnClickListener”并覆盖 OnClick 方法,这也不起作用:(
    【解决方案4】:
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_land);
    
                Button btn = (Button) findViewbyid(R.id.buttonWidth);
    
                btn.setOnClickListener(this);
    }
    public void onClickWidth(View v) 
    {
          if(v == btn){
                //your code...
          }
    }
    

    【讨论】:

      【解决方案5】:

      将包含 ListView 的父布局的 android:descendantFocusability 属性设置为 blocksDescendants,例如:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:descendantFocusability="blocksDescendants"> ...</LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多