在Android中,提供了一些按钮类组件,主要包括普通按钮,图片按钮,单选按钮,复选按钮等
一 普通按钮
<Button
android:id="@+id/ID"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="显示文本"
/>
添加按钮点击事件监听器有两种方式,第一种是java代码种完成,
Button login = (Button)findViewById(R.id.login) // 通过ID获取布局文件中添加的按钮
login.setOnClickListener(new OnClickListener(){ // 为按钮添加点击事件监听器
@Overrid
public void onClick(View v){
// 此处编写要执行的代码
}
})
另一种在Activity中编写一个包含View类型参数的方法,并且将要触发的动作代码放在该方法中,然后在布局文件中,通过android:onClick 属性指定对应的方法名实现,
public void myClick(View view){
// 此处编写要执行的动作按钮
}
按钮案例 开心消消乐 “授权登录” 按钮
创建shape资源文件 -> res/drawable new ->Drawable Resource file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--填充颜色-->
<solid android:color="#1FBAF3"/>
<!--设置4个角的弧形半径-->
<corners android:radius="5dp"/>
<!--设置文字与按钮边界的间隔-->
<padding
android:bottom="10dp"
android:left="15dp"
android:right="15dp"
android:top="10dp"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFF4"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="@drawable/top" />
<!-- 添加授权并登录按钮 -->
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="授权并登录"
android:textColor="#FFFFFF" />
</LinearLayout>
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginbtn);
// 为按钮添加单击事件监听器
Button button = (Button) findViewById(R.id.button1);
//为按钮添加单击事件监听器
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(LoginActivity.this,
"您已授权登录开心消消乐",Toast.LENGTH_SHORT).show();
}
});
}
}
图片按钮 ImageButton
<ImageButton
android:id="@+id/ID"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@mipmap/图片文件名"
android:scaleType="缩放方式"
/>
android:scaleType 属性的属性说明
matrix | 使用matrix方式进行缩放 |
fitXY | 对图片横向,纵向独立缩放, |
fitStart | 保持纵横比缩放图片,直到该图片能完全显示在ImageButton中,缩放完成后该图片放在ImageButton的左上角 |
fitCenter | 保持纵横比缩放图片,直到该图片能完全显示在ImageButton中,缩放完成后该图片放在ImageButton的中间 |
fitEnd | 保持纵横比缩放图片,直到该图片能完全显示在ImageButton中,缩放完成后该图片放在ImageButton的右下角 |
center | 把图像放在中间,但是不进行任何缩放 |
centerCrop | 保持纵横比缩放图片,使图片能完全覆盖ImageButton |
centerInside | 保持纵横比缩放图片,使ImageButton能完全显示该图片 |
单选按钮
<RadioButton
android:text="显示文本"
android:id="@+id/id"
android:checked="true| flase"
/>
android:checked 属性用于指定选中状态,属性值为true时表示选中,属性值为false为false表示取消选中,默认为false
通常情况下,RadioButton组件需要与RadioGruoup组件一起使用,组成一个单选按钮组
<RadioGroup
andorid:id="@+id/radioGroup1"
android:orientation="horizontal"
android:layout_width="wrop_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio0"
android:text="男"
android:layout_width="wrop_content"
android:checked="true"
/>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio0"
android:text="女"
android:layout_width="wrop_content"
/>
</RadioGroup>
<Button
android:text="提交"
android:id="@+id/button1"
android:layout_width="wrop_content"
android:layout_height="wrap_content"
/>
在改变单选按钮组的值时,获取被选中的单选按钮的值, 添加OnCheckedChangeListener,并在onChenckedChanged()方法中根据checkedId获取被选中的单选按钮,并通过getText() 方法获取该单选按钮对应的值,