Android Button是我们用的最平常的控件之一了,最近由于某种需求,在系统整理这些基本知识,发现还有一些平常用的比较少的知识,特奉送给大家。
1. 一般用法
在xml布局文件种定义个Button
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_one"
android:text="@string/btn_one_string"
/>
然后在Activity里面
mBt1 = (Button) findViewById(R.id.btn_one);
mBt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this, "第一个按钮 已经被点击", Toast.LENGTH_SHORT).show();
}
});
这是没问题的。
2. 在xml文件里面自定义onClick方法
上面我们是在代码中书写onClick方法,这里我们在布局文件中去定义。
a. 首先定义我们的布局文件
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_two"
android:onClick="sayHello"
android:text="@string/btn_two_string"
/>
上面蓝色字体比较重要,它说的意思是这个Button onClick的时候将调用sayHello方法
b. 在我们的Context中(一般是Activity),定义sayHello方法,参数是View ,如:
public void sayHello(View view){
Log.d("Sandy", "sayHello method invoke, view: " + view);
Toast.makeText(ButtonActivity.this, "第二个按钮已经被点击,用xml文件注册的方法", Toast.LENGTH_SHORT).show();
}
这里要注意的是,我们这里定义的方法名必须和android:onClick中配置的名称是一样的。而且带有View参数,这个view就是我们上面的Button对象。
3. 自定义Button样式
Android默认的Button样式有点丑,有木有?
所以,我们自定义我们的Button样式
a. 在res/drawable文件夹里面自定义一个 xml文件,如:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/ic_call_select_cdma_pressed" />
<item android:state_enabled="true" android:drawable="@drawable/ic_call_select_cdma" />
</selector>
这个xml文件描述的是,被按下时显示按下的图片,其他时候显示正常图片。
b. 在我们的布局文件中,设置Button 的背景是这个文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/call_type_chooser_cdma"
/>
这里要注意的是第一步定义的xml文件中,item顺序不能变,如果调换两个item位置,会导致按下时没有效果。
原因分析:
相信大家肯定用过switch case的语法, 那么这里的item匹配和case匹配一样,如果匹配到满足条件的,就不会继续往下面匹配了。
所以,如果android:state_enabled在前面,那肯定是匹配到android:state_enabled,这样就不会继续往下面匹配了。
所以android:state_pressed配置在android:state_enabled后面没有任何作用。
顺便列出所有的状态
android:state_pressed 控件被按下
android:state_focused 控件获取到焦点
android:state_hovered 控件上光标是否悬浮 //android 4.0才有的属性
android:state_selected 控件被选择
android:state_checkable 控件能否被点击
android:state_checked 控件被点击
android:state_enabled 控件可用
android:state_window_focused 所在窗口有焦点
原文链接:http://bbs.51cto.com/thread-1011715-1.html
1. 一般用法
在xml布局文件种定义个Button
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_one"
android:text="@string/btn_one_string"
/>
然后在Activity里面
mBt1 = (Button) findViewById(R.id.btn_one);
mBt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this, "第一个按钮 已经被点击", Toast.LENGTH_SHORT).show();
}
});
这是没问题的。
2. 在xml文件里面自定义onClick方法
上面我们是在代码中书写onClick方法,这里我们在布局文件中去定义。
a. 首先定义我们的布局文件
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_two"
android:onClick="sayHello"
android:text="@string/btn_two_string"
/>
上面蓝色字体比较重要,它说的意思是这个Button onClick的时候将调用sayHello方法
b. 在我们的Context中(一般是Activity),定义sayHello方法,参数是View ,如:
public void sayHello(View view){
Log.d("Sandy", "sayHello method invoke, view: " + view);
Toast.makeText(ButtonActivity.this, "第二个按钮已经被点击,用xml文件注册的方法", Toast.LENGTH_SHORT).show();
}
这里要注意的是,我们这里定义的方法名必须和android:onClick中配置的名称是一样的。而且带有View参数,这个view就是我们上面的Button对象。
3. 自定义Button样式
Android默认的Button样式有点丑,有木有?
所以,我们自定义我们的Button样式
a. 在res/drawable文件夹里面自定义一个 xml文件,如:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/ic_call_select_cdma_pressed" />
<item android:state_enabled="true" android:drawable="@drawable/ic_call_select_cdma" />
</selector>
这个xml文件描述的是,被按下时显示按下的图片,其他时候显示正常图片。
b. 在我们的布局文件中,设置Button 的背景是这个文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/call_type_chooser_cdma"
/>
这里要注意的是第一步定义的xml文件中,item顺序不能变,如果调换两个item位置,会导致按下时没有效果。
原因分析:
相信大家肯定用过switch case的语法, 那么这里的item匹配和case匹配一样,如果匹配到满足条件的,就不会继续往下面匹配了。
所以,如果android:state_enabled在前面,那肯定是匹配到android:state_enabled,这样就不会继续往下面匹配了。
所以android:state_pressed配置在android:state_enabled后面没有任何作用。
顺便列出所有的状态
android:state_pressed 控件被按下
android:state_focused 控件获取到焦点
android:state_hovered 控件上光标是否悬浮 //android 4.0才有的属性
android:state_selected 控件被选择
android:state_checkable 控件能否被点击
android:state_checked 控件被点击
android:state_enabled 控件可用
android:state_window_focused 所在窗口有焦点
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。
原文链接:http://bbs.51cto.com/thread-1011715-1.html