实例1:实现跟踪鼠标单击状态的图片按钮

来源:互联网 发布:ubuntu 16.04卸载qq 编辑:程序博客网 时间:2024/04/30 08:44

1、布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"    android:layout_height="fill_parent" tools:context=".MainActivity"    android:orientation="vertical"    android:gravity="center">    <ImageView        android:id="@+id/start"        android:background="#000"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>

2、编写Drawable资源对应的XML文件button_state.xml,用于设置当鼠标按下时显示的图片和鼠标设置没有按下时显示的图片

<?xml version="1.0" encoding="utf-8"?>    <selector        xmlns:android="http://schemas.android.com/apk/res/android">        <item android:state_pressed="true" android:drawable="@drawable/start_a"/>        <item android:state_pressed="false" android:drawable="@drawable/start_b"/>    </selector>

3、为main.xml布局文件中的图片按钮设置android:src属性

android:src = "@drawable/button_state"

4、在主活动中获取添加的图片按钮,并为其添加鼠标单击事件监听器

<span style="font-size:12px;">ImageView imageView = (ImageView)findViewById(R.id.start);        imageView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,"开始",Toast.LENGTH_SHORT).show();            }        });</span>



0 0
原创粉丝点击