android之点击事件ImageView切换

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>

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值