1. 程式人生 > >android之點選事件ImageView切換

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>