80,359
社区成员
发帖
与我相关
我的任务
分享
view.findViewById(R.id.btn).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (MotionEvent.ACTION_DOWN == action)
v.setSelected(true);
else if (MotionEvent.ACTION_MOVE != action)
v.setSelected(false);
return false;
}
});
修改selector_shape_btn_blue.xml
<item android:state_pressed="true"> 为 <item android:state_selected="true">
如果还有更简单的方法请回复我,谢谢!
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<corners android:radius="3dp" />
<solid android:color="@color/red" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<solid android:color="@color/blue" />
</shape>
</item>
</selector>
selector_shape_btn_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape>
<corners android:radius="2dip"/>
<solid android:color="@color/gray_btn_fouce" />
</shape>
</item>
<item>
<shape>
<corners android:radius="2dip"/>
<solid android:color="@color/gray_btn" />
</shape>
</item>
</selector>
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lvmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:descendantFocusability="blocksDescendants"
android:listSelector="@drawable/selector_shape_btn_gray" />
</RelativeLayout>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dip"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_shape_btn_blue"
android:text="aadfasfadsf" />
</LinearLayout>
点击item的时候item变色,button变色(我希望button不变色)
点击button的时候item没有变色,不头痛变色
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
实在不行可以在OnItemClickListener里面进行强制修改