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();
}
});