方法一
1.放在drawable下的selector.xml文件

复制代码 代码如下:


   android:drawable="@drawable/temp2" />

2.布局文件main.xml

复制代码 代码如下:
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        android:layout_height="wrap_content"
    android:text="@string/hello"
    />
  android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:background="@drawable/selector"/>

 方法二
1.布局文件main.xml
[code]
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
  android:drawableTop="@drawable/shouru"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:background="@drawable/temp4"/>


2.主要的java代码,实现点击效果:
复制代码 代码如下:
   Button button = (Button) this.findViewById(R.id.button);
   button.setOnTouchListener(new Button.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){  
                    v.setBackgroundResource(R.drawable.temp1);  
                    Log.i("TestAndroid Button", "MotionEvent.ACTION_DOWN");
                }  
                else if(event.getAction() == MotionEvent.ACTION_UP){  
                    v.setBackgroundResource(R.drawable.temp2);
                    Log.i("TestAndroid Button", "MotionEvent.ACTION_UP");
                }
    return false;
   }
  });

更多相关文章

  1. Edittext设置输入属性,包括使用代码设置
  2. Android:WebView与Javascript交互(相互调用参数、传值)
  3. Android(安卓)build.gradle文件详解(转述自《Android第一行代码》
  4. 建立一个简单的android涂鸦工程
  5. android 谷歌地图准备
  6. Android查看源码
  7. android全屏设置代码:
  8. 转:Android(安卓)发送短信的方法
  9. android中去掉标题栏和状态栏

随机推荐

  1. android scroller类的使用
  2. android相关
  3. ch020 Android(安卓)SQLite3(第二部分)
  4. android中ImageView、ImageButton、Butto
  5. Android(安卓)进度条
  6. Android(安卓)设置隐式意图
  7. Android(安卓)JSON,Gson,fastjson实现比
  8. [Android] android:scaleType详解
  9. How Android(安卓)Draws Views
  10. android 打电话 发送短信