1.Button的作用:
代表一个按钮部件。用户通过按下按钮,或者点击按钮来执行一个动作。
在一个Activity中,监听事件:
button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // 按钮单击时执行的动作 } });
另外,你可以在 XML 布局文件中,用 android:onClick
属性为按钮指定方法名,以代替在活动中使用的 OnClickListener
.例如:
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/self_destruct" android:onClick="selfDestruct" />
多个按钮监听一个事件
2.公有构造函数
public Button (Context context)
public Button (Context context, AttributeSet attrs)
public Button (Context context, AttributeSet attrs, int defStyle)
3. 按钮变色: android:background="@drawable/change"
所谓变色是通过点击或触摸等事件把当前的按钮的颜色改变下,体验效果友好
在Drawable目录下创建一个xml 用于切换按钮的状态
解释每个android:state_xxx下的状态及她的含义
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活(这个麻烦举个例子,不是特明白)
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了
http://api.apkbus.com/reference/android/widget/Button.html