关于Android 开发中的5个Button(按钮)
来源:互联网 发布:java面向对象封装 编辑:程序博客网 时间:2024/05/19 03:29
1.StateListDrawable简介:
StateListDrawable是Drawable资源的一种,可以根据不同的状态,设置不同的图片效果,关键节点 ,我们只需要将Button的background属性设置为该drawable资源即可轻松实现,按下按钮时不同的按钮颜色或背景!
我们可以设置的属性:
drawable:引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态〜
state_focused:是否获得焦点
state_window_focused:是否获得窗口焦点
state_enabled:控件是否可用
state_checkable:控件可否被勾选,例如:复选框
state_checked:控件是否被勾选
state_selected:控件是否被选择,针对有滚轮的情况
state_pressed:控件是否被按下
state_active:控件是否处于活动状态,例如:slidingTab
state_single:控件包含多个子控件时,确定是否只显示一个子控件
state_first:控件包含多个子控件时,确定第一个子控件是否处于显示状态
state_middle:控制包含多个子控件时,确定中间一个子控件是否处于显示状态
state_last:控制包含多个子控件时,确定最后一个子控件是否处于显示状态
2.实现按钮的按下效果:
好的,先准备三个图片背景,一般我们为了避免按钮拉伸变形都会使用.9.png作为按钮的drawable!先来看下 运行效果图:
实现的代码如下:
Layout布局关键代码:
<button android:ID = "@ + ID / btnOne" android:layout_width = "match_parent" android:layout_height = "64dp" android:background= "@draw/ btn_bg1" android:Text= "Button" /> <button android:id = “@ + id / btnTwo” android:layout_width = “match_parent” android:layout_height = “64dp” android:text = “按钮不可用” />
**
3.实现Material Design水波效果的Button
如果你的Android的手机是5.0以上的系统,相信对下面这种按钮点击效果并不会陌生:
实现效果图:
快的那个是按下后的效果,慢的是长按后的效果!
实现逻辑:
1.我们继承的ImageButton,当然你可以换成按钮或者查看,这里笔者想把龟放到中间才继承的ImageButton
2.首先,创建两个涂料(画笔)对象,一个绘制底部背景颜色,一个绘制波纹扩散的
3.接着计算最大半径,开始半径每隔一段时间递增一次,直到等于最大半径,然后重置状态!
PS:大概的核心,刚学可能对自定义查看感到陌生,没事,这里了解下即可,以后我们会讲,当然你可以自己扣扣,注释还是蛮详细的〜
实现代码:
In the process of perfection...
**
4、单选按钮(RadioButton):
**
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="chauncy.example.com.day05_class1222.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/clist"> <TextClock android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="Please selete your hobby:"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cb_01" android:text="Play Computer game"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cb_02" android:text="Play football"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cb_03" android:text="shopping"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_submit" android:text="Submit"/> </LinearLayout></android.support.constraint.ConstraintLayout>
以下是layout代码:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="chauncy.example.com.wenjuan.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="60dp" android:background="#e6e3e3" android:id="@+id/LY_Top"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="试卷名称" android:textStyle="bold" android:textSize="32sp" android:layout_marginLeft="20dp" android:gravity="center" tools:ignore="HardcodedText" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="40dp" android:text="≡" android:background="#e6e3e3" android:textSize="44sp" android:gravity="center" android:layout_toLeftOf="@+id/btn_sumit"/> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="8dp" android:id="@+id/btn_sumit" android:text="√" android:layout_alignParentRight="true" android:background="#e6e3e3" android:textSize="44sp" android:gravity="center" /> </RelativeLayout> <RelativeLayout android:layout_weight="8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/LY_Top" android:layout_above="@+id/RL_Time"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/RLTV_01"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/RG_01"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#c4ea45" android:button="@null" android:text="1.NullPointerException是"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rb_A" android:text="A.算术异常"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rb_B" android:text="B.算术异常"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rb_C" android:text="C.算术异常"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rb_D" android:text="D.算术异常"/> </RadioGroup> </RelativeLayout> </ScrollView> </RelativeLayout> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="50dp" android:background="#ede6c2" android:id="@+id/RL_Time" android:layout_alignParentBottom="true"> <TextClock android:layout_width="match_parent" android:layout_height="match_parent" android:text="30:00" android:id="@+id/txt_time" android:textSize="28sp" android:gravity="center"/> </RelativeLayout> </RelativeLayout></android.support.constraint.ConstraintLayout>
5、多选按钮(多选框ChackButton)
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{ private CheckBox cb_one; private CheckBox cb_two; private CheckBox cb_three; private Button btn_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb_one = (CheckBox) findViewById(R.id.cb_one); cb_two = (CheckBox) findViewById(R.id.cb_two); cb_three = (CheckBox) findViewById(R.id.cb_three); btn_send = (Button) findViewById(R.id.btn_send); cb_one.setOnCheckedChangeListener(this); cb_two.setOnCheckedChangeListener(this); cb_three.setOnCheckedChangeListener(this); btn_send.setOnClickListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show(); } @Override public void onClick(View view) { String choose = ""; if(cb_one.isChecked())choose += cb_one.getText().toString() + ""; if(cb_two.isChecked())choose += cb_two.getText().toString() + ""; if(cb_three.isChecked())choose += cb_three.getText().toString() + ""; Toast.makeText(this,choose,Toast.LENGTH_SHORT).show(); }}
6、开关按钮ToggleButton和开关Switch
1)切换按钮(开关按钮)
可设置的属性:
android:disabledAlpha:设置按钮在禁用时的透明度
android:textOff:按钮没有被选中时显示的文字
android:textOn:按钮被选中时显示的文字另外,除了这个我们还可以自己写个选择器,然后设置下Background(背景)属性也行!
2)开关(Switch)
可供我们设置的属性:
android:showText:设置开启/关闭的时候是否显示文字,布尔
android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,布尔
android:textOff:按钮没有被选中时显示的文字
…
代码如下:
关于按钮就大致讲到你这儿!
原创声明: 部分内容借鉴于搜索引擎!