您的位置:首页 > 移动开发 > Android开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: