Android的基本组件

Android的基本组件

各种组件之间的关系(类图)
类图

Button按下的效果

引入StateListDrawable ,可根据不同状态设置不同的drawable背景
xml中定义<selector>
属性解释
drawable引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态~
state_focused是否获得焦点
state_window_focused是否获得窗口焦点
state_enabled控件是否可用
state_checkable控件可否被勾选,eg:checkbox
state_checked控件是否被勾选
state_selected控件是否被选择,针对有滚轮的情况
state_pressed控件是否被按下
state_active控件是否处于活动状态,eg:slidingTab
state_single控件包含多个子控件时,确定是否只显示一个子控件
state_first控件包含多个子控件时,确定第一个子控件是否处于显示状态
state_middle控件包含多个子控件时,确定中间一个子控件是否处于显示状态
state_last控件包含多个子控件时,确定最后一个子控件是否处于显示状态
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/btn1"/>
    <item android:state_pressed="false" android:drawable="@drawable/btn2"/>

</selector>

引入样式

<?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">
    <Button
        android:id="@+id/btnOne"
        android:background="@drawable/button_selector"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:text="按钮"/>
 </Linearout>

效果

圆角按钮

设置圆角

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>

        <shape>
<!--            填充颜色-->
            <solid android:color="@color/bbutton_danger_pressed" />
<!--            描边-->
            <stroke android:width="1dp" android:color="#00FF00" />
<!--            设置圆角-->
            <corners android:radius="10dp" />

        </shape>
    </item>

</selector>

布局文件中引入

<?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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆角按钮"
        android:background="@drawable/button_round"
        />

</LinearLayout>

效果:
在这里插入图片描述

Toast
 Toast.makeText(this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
RadioGroup

单选按钮获取选定值的方法:
给RadioGroup注册监视器,监控单选按钮的变化

  • RadioGroup
    • RadioButton
    • RadioButton
      所以单选框的所有组件都要设置id,不然监视器拿不到被选中的单选按钮的id.
 RadioGroup radioGroup = AActivity.this.findViewById(R.id.radioGroup);
       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
               Toast.makeText(AActivity.this,checkedId+"",Toast.LENGTH_SHORT).show();
               Button checked = group.findViewById(checkedId);
               sex = (String) checked.getText();

           }
       });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值