怎么给LinearLayout 增加onClickListener事件

怎么给LinearLayout 增加onClickListener事件,第1张

首先在布局xml中给该LinearLayout添加个id

然后在UI主线程中

View v = (View)findByid(R.id.xxx) //xxx为XML中配置的ID

v.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//在此编写点击需要处理的东西

}

})

<LinearLayout android:descendantFocusability=blocksDescendants

<Button android:background=@layout/selector

</LinearLayout

Button通过设置android:background使得点击时变换背景颜色

------解决方案--------------------------------------------------------

觉得你这联动不能用selector,考虑考虑设计上吧

------解决方案--------------------------------------------------------

别用selector,在代码中添加ontouch事件试一下

------解决方案--------------------------------------------------------

------解决方案--------------------------------------------------------

其实你的思路太局限了,imageView和button都是View,大家都有同样的基类,那干嘛一定要用button呢,而且也根本没必要去监听button的OnClick。

你把Button换成imageView(不换也可以,但是可扩展性不好),然后LinearLayout和imageView都做一个selector,给LinearLayout做一个OnClick监听,绑定在xml里就行了,然后监听里加一行

你的LinearLayout.setSelected(true)就可以了

------解决方案--------------------------------------------------------

Button换成View然后只要给LinearLayout写onClick事件就可以了

------解决方案--------------------------------------------------------

给linearlayout设置一个id:myf_linl_login;

找出控件View home = view.findViewById(R.id.myf_linl_login)之后

linear.setOnClickListener(new View.OnClickListener(){

public void onClick(View v){

//这里写你的点击事件

}

})

***看清楚***

View.OnClickListener


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

原文地址: https://outofmemory.cn/bake/11608018.html

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

发表评论

登录后才能评论

评论列表(0条)

保存