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:按鈕沒有被選中時顯示的文字
…
代碼如下:
關於按鈕就大致講到你這兒!
原創聲明: 部分內容借鑑於搜索引擎!