前言
小夥伴們肯定都有在玩兒一些遊戲,比如和平精英,在界面上展示的名字,其實就是Android中就是咱們上一篇介紹到的TextView控件。而今天,我們再給大家帶來一個非常好玩兒的控件,這個控件可以結合咱們之前的講到的內容做很多有趣的事情~
一、結構
public class Button extends TextView
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
已知直接子類
CompoundButton
已知間接子類
CheckBox, RadioButton, Switch, ToggleButton
二、典型點擊用法如下
Button是Android中一個非常簡單的控件,在我們平時的項目中,可以說是非常的常見,使用率也是相當高。用戶可以按下或單擊按鈕來執行操作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_clickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一個按鈕,快點我" />
<ImageButton
android:id="@+id/btn_clickImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnClickMe = (Button) findViewById(R.id.btn_clickMe);
btnClickMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this, "已點中", Toast.LENGTH_SHORT).show();
}
});
ImageButton btnClickImg = (ImageButton) findViewById(R.id.btn_clickImg);
btnClickImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this, "已點中圖片", Toast.LENGTH_SHORT).show();
}
});
}
}
你還可以通過實現View.OnClickListener
接口並重寫onClick
方法,來設置多個點擊事件
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_clickMe).setOnClickListener(this);
findViewById(R.id.btn_clickImg).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_clickMe:
Toast.makeText(MainActivity.this, "已點中", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_clickImg:
Toast.makeText(MainActivity.this, "已點中圖片", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
但是,您還可以使用android:onClick
屬性爲XML
佈局中的按鈕分配一個方法,而不是對Activity中對按鈕實現onClickListener。例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="我是一個按鈕,快點我" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:onClick="clickImg" />
</LinearLayout>
現在,當用戶點擊按鈕時,Android系統會調用Activity的自定義(視圖)方法。此方法必須是公共的,並且接受一個視圖作爲它的唯一參數。例如:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickButton(View view) {
Toast.makeText(MainActivity.this, "已點中", Toast.LENGTH_SHORT).show();
}
public void clickImg(View view) {
Toast.makeText(MainActivity.this, "已點中圖片", Toast.LENGTH_SHORT).show();
}
}
最終效果如下:
三、按鈕樣式設置
每個按鈕都使用系統的默認按鈕背景進行樣式化,如果您對默認按鈕樣式不滿意,並且希望對其進行自定義以匹配應用程序的設計,那麼您可以用可繪製的狀態列表替換按鈕的背景圖像。狀態列表可繪製是在XML中定義的可繪製資源,它根據按鈕的當前狀態更改其圖像。一旦定義了一個可以用XML繪製的狀態列表,就可以將它應用到具有android:background
屬性的按鈕上。
1.設置背景圖
方法一:在xml佈局裏直接設置背景圖
方法二:在Java代碼裏給button
設置背景圖(setBackground
和setBackgroundResource
兩種任意一種即可)
btnClickMe.setBackground(ContextCompat.getDrawable(this, R.mipmap.ic_launcher));
btnClickMe.setBackgroundResource(R.mipmap.ic_launcher);
2.設置背景色
方法一:在xml佈局裏直接設置背景色
方法二:在Java代碼裏給button
設置背景色(setBackgroundColor
中以下兩種方法任意一種即可)
btnClickMe.setBackgroundColor(getResources().getColor(R.color.colorAccent));
btnClickMe.setBackgroundColor(Color.parseColor("#ff0000"));
3.通過shape
設置背景樣式
在drawable包下新建xml,我這裏命名爲bg_btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="50dp"
android:topLeftRadius="10dp"
android:topRightRadius="50dp" />
<stroke
android:width="3dp"
android:color="#99CCFF" />
</shape>
-
android:shape="rectangle"
//樣式爲矩形(四個屬性:rectangle
矩形、oval
橢圓形、line
線性形狀、ring
環形) -
solid
:指定內部填充色 -
corners
:定義圓角 (radius全部的圓角半徑 、bottomLeftRadius
左下角的圓角半徑 、bottomRightRadius
右上角的圓角半徑 、topLeftRadius
左上角的圓角半徑 、topRightRadius
右上角的圓角半徑 ) -
stroke
:描邊屬性,可以定義描邊的寬度,顏色,虛實線等(width
描邊的寬度、color
描邊的顏色)
方法一:在xml佈局裏直接設置自定義shape
背景,如果想給按鈕內部文字設置邊距還可以自己設置padding
方法二:在Java代碼裏給button
設置自定義shape
背景(setBackground
和setBackgroundResource
兩種任意一種即可)
btnClickMe.setBackground(ContextCompat.getDrawable(this, R.drawable.bg_btn));
btnClickMe.setBackgroundResource(R.drawable.bg_btn);
此時,如果還想要仿iOS一樣的按鈕點擊按下和鬆開效果,那麼可以這樣
再在drawable包下新建一個xml,我這裏命名爲bg_btn_press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="50dp"
android:topLeftRadius="10dp"
android:topRightRadius="50dp" />
<stroke
android:width="3dp"
android:color="#99CCFF" />
</shape>
然後再新建一個xml,同樣是在drawable包下,使用<selector../>
元素定義了一個StateListDrawable
對象,我這裏命名爲bg_btn.xml
。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_btn_press" android:state_pressed="true" />
<item android:drawable="@drawable/bg_btn_normal" android:state_focused="true" />
<item android:drawable="@drawable/bg_btn_normal" />
</selector>
4.設置顯示隱藏
方法一:在xml佈局裏直接設置
android:visibility="invisible"
方法二:在Java代碼裏給button
設置
btnClickMe.setVisibility(View.VISIBLE);
下面有清楚的佈局講述
其中visibility
還有三種屬性
-
VISIBLE
(view可見,佔據屏幕區域)、 -
INVISIBLE
(view不可見,佔據屏幕區域)、 -
GONE
(view不可見,不佔屏幕空間,原先佔有的區域被其他view佔據,原先佈局會發生變化)。
5.給button設置某一邊設置圖片
方法一:在xml佈局裏直接設置
方法二:在Java代碼裏給button
設置
Button btnClickMe = findViewById(R.id.btn_clickMe);
//代碼設置drawableLeft資源圖片
Drawable drawableLeft = getResources().getDrawable(R.mipmap.ic_launcher);
btnClickMe.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
//代碼設置drawable和view之間的距離
btnClickMe.setCompoundDrawablePadding(4);
-
drawableLeft
左圖標 -
drawableRight
右圖標 -
drawableTop
上圖標 -
drawableBottom
下圖標 -
drawablePadding
圖標與文字的間距
四、結語
以上就是Android
中最常用的UI控件Button
的介紹,在我們的實際開發中,好多控件都擁有onClick()事件,那麼上一篇文章的TextView有沒有,就要靠小夥伴們自己去嘗試了~
PS:如果還有未看懂的小夥伴,歡迎加入我們的QQ技術交流羣:892271582,裏面有各種大神回答小夥伴們遇到的問題哦~