如何解决ListView和ListActivity未检测到按钮onClick
| 我有以下布局: <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属性和其他各种方法玩,但我只是无法使它正常工作。我该如何解决?还是根本不允许这样做?
解决方法
你能告诉我们你想做什么吗?为什么需要ListActivity?
很抱歉编辑:P
您可以在这里看看:如何处理Android中的ListView单击
,将OnClicklistener放在适配器本身内。
,而不是在布局中使用onClick东西,您是否尝试过以下方法:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
然后对buttonHeight执行相同的操作。
,@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...
}
}
,将包含ListView的父布局的android:descendantFocusability属性设置为valuesblockDescendants值:
<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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。