博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android 按钮选中事件实现及其原理分析

Posted on 2012-02-16 17:01  mhlstar  阅读(1591)  评论(0)    收藏  举报

刚刚好多人问到关于 Button、ImageView等自定义选中、按下、未选中等效果的问题,看了网上一些文章,特此整理一下:

  

方法一:代码实现

1. 自定义状态效果可以通过代码实现,也可以通过xml定义style实现。

2. 下面先介绍代码实现,通过StateListDrawable定义Button背景。

3. 由于View类中PRESSED_ENABLED_STATE_SET值不是公共常量,所以通过继承来访问了。

特注:其他控件的效果,比如ImageView,也可以通过这种方法实现,但是由于ImageView默认是没焦点,不可点击的,需要自己更改(需要点击就设置android:clickable="true" , 需要能够选中就设置android:focusable="true" )

 

JAVA代码:

 1 package com.test.TestButton;
2
3
4 import android.app.Activity;
5 import android.content.Context;
6 import android.graphics.drawable.Drawable;
7 import android.graphics.drawable.StateListDrawable;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.Button;
11
12 public class TestButton extends Activity {
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17 Integer[] mButtonState = { R.drawable.defaultbutton,
18 R.drawable.focusedpressed, R.drawable.pressed };
19 Button mButton = (Button) findViewById(R.id.button);
20 MyButton myButton = new MyButton(this);
21 mButton.setBackgroundDrawable(myButton.setbg(mButtonState));
22 }
23
24 class MyButton extends View {
25
26 public MyButton(Context context) {
27 super(context);
28 }
29
30 // 以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选
31 // 中,按下,选中效果。
32 public StateListDrawable setbg(Integer[] mImageIds) {
33 StateListDrawable bg = new StateListDrawable();
34 Drawable normal = this.getResources().getDrawable(mImageIds[0]);
35 Drawable selected = this.getResources().getDrawable(mImageIds[1]);
36 Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
37 bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
38 bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
39 bg.addState(View.ENABLED_STATE_SET, normal);
40 bg.addState(View.FOCUSED_STATE_SET, selected);
41 bg.addState(View.EMPTY_STATE_SET, normal);
42 return bg;
43 }
44 }
45 }

 

方法二:XML自定义风格

在res/drawable下面新建mybutton_background.xml文件,内容如下:

 1 <?xml version=”1.0″ encoding=”utf-8″?>
2
3 <selector xmlns:android=”http://schemas.android.com/apk/res/android“>
4
5 <item android:state_focused=”true”
6
7 android:state_pressed=”false”
8
9 android:drawable=”@drawable/yellow” />
10
11 <item android:state_focused=”true”
12
13 android:state_pressed=”true”
14
15 android:drawable=”@drawable/green” />
16
17 <item android:state_focused=”false”
18
19 android:state_pressed=”true”
20
21 android:drawable=”@drawable/blue” />
22
23 <item android:drawable=”@drawable/grey” />
24
25 </selector>



这里面就定义了在不同状态下的显示图片,然后在layout里面定义Button的时候,指定它的background为这个mybutton_background

 1 <?xml version=”1.0″ encoding=”utf-8″?>
2
3 <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
4
5 android:orientation=”vertical”
6
7 android:layout_width=”fill_parent”
8
9 android:layout_height=”fill_parent”
10
11 >
12
13 <Button android:id=”@+id/btn”
14
15 android:layout_width=”wrap_content”
16
17 android:layout_height=”wrap_content”
18
19 android:text=”@string/mybtn”
20
21 android:background=”@drawable/mybutton_background” />
22
23 </LinearLayout>

这种方式开发比较简单,适合做一些风格一致的Button,设置成同一个background就可以了。ImageView等控件如方法一中所述。

 

方法三:JAVA代码中通过监听事件改变,这个不推荐,就此略过.