文章目录
Button
Button继承了TextView组件,它主要是在UI界面上生成一个按钮,该按钮可以供用户点击,当用户点击Button时,会自动触发onClick事件。下面是Button的一些常见的用法:
(1)普通按钮
(2)圆角按钮
(3)描边按钮
(4)按压变色按钮
(5)按钮点击事件
普通按钮
由于Button继承于TextView所以,TextView的属性也适用于Button。
示例程序1
<!--布局使用相对布局-->
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF2600"
android:text="ButtonOne"
android:textColor="#00ff00"
android:textSize="30sp"/>
圆角按钮
如果需要给按钮添加样式,主要是通过修改android:background
属性来实现。
- (1)在res/drawable下添加XML文件。eg:botton_two_style.xml
- (2)选择根元素为:shape
- (3)在XML中选择感兴趣的属性进行修改。
- (4)通过
android:background
来使用该xml文件
botton_two_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置圆角的半径为5dp-->
<corners android:radius="5dp"></corners>
<!--设置组件的填充颜色-->
<solid android:color="#ff00ff"></solid>
</shape>
示例程序2
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_one"
android:layout_marginTop="10dp"
android:background="@drawable/button_two_style"
android:text="ButtonTwo"
android:textColor="#000000"
android:textSize="30sp"/>
描边按钮
在shape
中有6个基本子元素:corners(圆角)、gradient(渐变)、padding(内边距)、size(尺寸)、solid(填充)、stroke(描边)。刚才的示例程序中展示corners的功能,这个列子将展示描边的功能,步骤和刚才是一样的。
button_three_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<stroke android:width="1dp"
android:color="#00FFD9"/>
</shape>
示例程序3
<Button
android:id="@+id/btn_Three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/button_three_style"
android:layout_below="@id/btn_two"
android:onClick="showToast"
android:textSize="30sp"
android:textColor="#2196F3"
android:text="ButtonThree"/>
这里推荐一篇文章:简书:Android shape属性大全
按压变色按钮
按压变色这个功能在我们平时的使用会经常使用到,那它是如何实现的?利用selector
选择器我们可以是实现这个功能。
button_four_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--没有被按压的效果-->
<item android:state_pressed="false">
<shape>
<corners android:radius="5dp"/>
<solid android:color="#0099FF"/>
</shape>
</item>
<!--按压效果-->
<item android:state_pressed="true">
<shape>
<corners android:radius="5dp"/>
<solid android:color="#3F51B5"/>
</shape>
</item>
</selector>
示例程序4
<Button
android:id="@+id/btn_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/button_four_style"
android:layout_below="@id/btn_Three"
android:text="ButtonFour"
android:textSize="30sp"/>
按钮点击事件
给按键添加点击事件两种方法:
(1)onClick
通过onClick属性可以为按钮添加一个点击事件。
- (1)在onclick中指定点击事件触发方法的方法名。
- (2)在对应的.java文件中实现该方法。
需要注意的是:方法的形式必须为:public void 方法名(View),方法有一个类型为View的参数。示例程序参照:示例程序3
(2) 在.java中为对应按钮设置对应的点击事件
- (1)通过
findViewById
方法找到对应的按钮 - (2)通过
setOnClickListener
方法设置点击事件 - (3)重写
View.OnClickListener
的onClick
方法
示例程序参照:示例程序4
.java
private Button m_ButtonFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_test);
m_ButtonFour=findViewById(R.id.btn_four);
m_ButtonFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取按钮文本内容
String str=((Button) v).getText().toString();
str+="被点击";
Toast.makeText(ButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
public void showToast(View view)
{
String str=((Button) view).getText().toString();
str+="被点击";
Toast.makeText(ButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
}