android之点击事件ImageView切换
2013-01-11 13:44
441 查看
1、点击按钮按下抬起事件
poslistview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#578cc2" >
<ImageView
android:id="@+id/return1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
>
</ImageView>
<ImageView
android:id="@+id/mapmodel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
>
</ImageView>
<ImageView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/mapmodel"
android:layout_toRightOf="@+id/return1"
android:src="@drawable/tit_03_parent" />
</RelativeLayout>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@null"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</LinearLayout>
postlistview.java
public class PosListView extends ListActivity implements OnClickListener,
OnTouchListener {
....
private ImageView mapmode;
mapmode = (ImageView) findViewById(R.id.mapmodel);
mapmode.setOnClickListener(this);
mapmode.setOnTouchListener(this);
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.mapmodel:
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("test", "cansal button ---> cancel");
mapmode.setBackgroundResource(R.drawable.ditu1);
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("test", "cansal button ---> down");
mapmode.setBackgroundResource(R.drawable.ditu2);
}
break;
}
return false;
}
2、点击listviewitem变色
在java中
this.getListView().setSelector(R.drawable.bg);
在drawable目录下建bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/xuanqu_02_02" android:state_pressed="true"/>
<item android:drawable="@drawable/udline_02"/>
</selector>
3、点击listviwitem中的某个按钮变色
在listview.xml中
<ImageView
android:id="@+id/zhidian1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/call"
/>
在drawabel目录下建立call.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/zhidian2" android:state_pressed="true"/>
<item android:drawable="@drawable/zhidian1"/>
</selector>
poslistview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#578cc2" >
<ImageView
android:id="@+id/return1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
>
</ImageView>
<ImageView
android:id="@+id/mapmodel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="3dp"
>
</ImageView>
<ImageView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/mapmodel"
android:layout_toRightOf="@+id/return1"
android:src="@drawable/tit_03_parent" />
</RelativeLayout>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@null"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</LinearLayout>
postlistview.java
public class PosListView extends ListActivity implements OnClickListener,
OnTouchListener {
....
private ImageView mapmode;
mapmode = (ImageView) findViewById(R.id.mapmodel);
mapmode.setOnClickListener(this);
mapmode.setOnTouchListener(this);
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.mapmodel:
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("test", "cansal button ---> cancel");
mapmode.setBackgroundResource(R.drawable.ditu1);
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("test", "cansal button ---> down");
mapmode.setBackgroundResource(R.drawable.ditu2);
}
break;
}
return false;
}
2、点击listviewitem变色
在java中
this.getListView().setSelector(R.drawable.bg);
在drawable目录下建bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/xuanqu_02_02" android:state_pressed="true"/>
<item android:drawable="@drawable/udline_02"/>
</selector>
3、点击listviwitem中的某个按钮变色
在listview.xml中
<ImageView
android:id="@+id/zhidian1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/call"
/>
在drawabel目录下建立call.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/zhidian2" android:state_pressed="true"/>
<item android:drawable="@drawable/zhidian1"/>
</selector>
相关文章推荐
- Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件
- android listview 中的子控件(比如ImageView)设置点击事件
- Android 多个ImageView图片层叠透明区域点击事件穿透
- Android 多个ImageView图片层叠透明区域点击事件穿透
- Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件
- Android ImageView包含透明区域的图片点击事件处理
- Android - VGA,ADB,DDMS,安卓开发环境目录结构,安卓目录结构,常见布局,点击事件的几种写法,控件(Button/TextView/ImageView/EditText)
- 完美解决Android 4.1+版本 ViewPager.setPageTransformer()切换动画点击事件失效
- android listview的item里面的imageview的点击事件
- Android自定义ImageView实现点击两张图片切换效果
- android ViewPager实现App主界面Tab菜单页面切换和点击事件
- Android ListView 嵌套 ImageView,如何响应ImageView的点击和长按事件
- Android中点击button,实现ImageView中图片的循环切换
- Android中WebView加载Html中的图片添加点击事件
- Android TextView 高亮字体并添加点击事件
- Android中父View和子view的点击事件的执行过程
- Android TextView 部分文字变色添加点击事件
- android中recycleView的应用和点击事件
- Android 响应webview中图片的点击事件
- 给ImageView添加点击事件