編輯:關於 Android 開發
除了系統自帶的 Button 之外,Android 還提供了帶圖示的按鈕 ImageButton。
要製作帶圖示的按鈕,先在版面檔中定義 ImageButton,然後用 setImageDrawable() 設定要顯示的圖示。
圖示的設定方式有三種:
1. 直接在版面檔中指定
android:src="@drawable/icon1"
2. 在程式碼中設定自訂圖示
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
3. 使用系統內建的圖示
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));
設定好圖示之後,記得為按鈕掛上 setOnClickListener,才能擷取並處理點擊事件。
下面的例子由四個圖示按鈕組成:前三個使用自訂圖示,第四個使用系統圖示。點擊按鈕1 彈出 dialog;點擊按鈕2 並確認後,按鈕2 的圖示會換成按鈕3 的圖示;點擊按鈕3 並確認後,其圖示換成系統的打電話圖示;點擊按鈕4 則顯示一個提示 dialog。
package org.loulijun.imagebutton;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class ImageButtonTest extends Activity {
/** Called when the activity is first created. */
TextView textview;
ImageButton imgbtn1;
ImageButton imgbtn2;
ImageButton imgbtn3;
ImageButton imgbtn4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView) findViewById(R.id.textview);
// 分別取得 4 個 ImageButton 物件
imgbtn1 = (ImageButton) findViewById(R.id.imagebutton1);
imgbtn2 = (ImageButton) findViewById(R.id.imagebutton2);
imgbtn3 = (ImageButton) findViewById(R.id.imagebutton3);
imgbtn4 = (ImageButton) findViewById(R.id.imagebutton4);
// 分別為 ImageButton 設置圖標
// imgbtn1 已在 main.xml 中設置過圖標,這裡不再重複
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon));
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));
// 為各個按鈕設置事件監聽
imgbtn1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton1")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 相應的處理操作
}
}).create();
dialog.show();
}
});
imgbtn2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton2,我要使用ImageButton3的圖標")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
}
}).create();
dialog.show();
}
});
imgbtn3.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton3,我想使用系統打電話的圖標")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
imgbtn3.setImageDrawable(
getResources().getDrawable(android.R.drawable.sym_action_call));
}
}).create();
dialog.show();
}
});
imgbtn4.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是使用的系統圖標")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 相應的處理操作
}
}).create();
dialog.show();
}
});
}
}
1. ImageButton 與 Button 的差別。Button 是 TextView 的子類別,文字為主、圖示需靠 drawableLeft 等屬性搭配;ImageButton 直接繼承 ImageView,以圖片為主,沒有文字屬性。若需要「圖文並列」,用 Button 加 drawable 屬性會比 ImageButton 合適。
2. 版面檔中記得設定 contentDescription。ImageButton 沒有可見文字,對使用螢幕閱讀器的使用者而言是個黑洞,應加上 android:contentDescription 說明按鈕用途。
3. 觸控回饋不可少。預設的 ImageButton 點下去沒有任何視覺變化,使用者會不確定有沒有按到。建議在 res/drawable 下用 selector 定義 state_pressed 與 normal 兩張圖,並設為 android:src。
4. getDrawable() 的版本差異。上述 getResources().getDrawable(int) 在 API 22 之後已被標記為不建議使用,新專案請改用 ContextCompat.getDrawable(this, R.drawable.xxx) 或 getResources().getDrawable(id, getTheme())。
Android 佈局詳解:RelativeLayout 相對佈局的定位規則與梅花案例
RelativeLayout 相對佈局入門 相對佈局是 Android 早期最常被使用的容器之一,它的核心思路是「以別人為座標」:每個子控制項都可以聲明自己相對於父容器、
Android 入門:Activity 基本概念與 Toast 提示框實作
Activity 是什麼? Activity 是使用者實際看到的應用程式畫面。任何需要顯示介面的元件,通常都會繼承 android.app.Activity。系統會為 A
React Native 控件解析:ProgressBarAndroid 進度條
React Native ProgressBarAndroid 介紹 ProgressBarAndroid 是 React Native 中對 Android 平台 Pr
Android Activity 間切換與數據傳遞的兩種實現方式
Android Activity 間切換與數據傳遞的兩種實現方式 前面照著 Android 系統裁剪圖片的功能,自己寫了一個相似的小工具。大體功能實現了,但留下一個關鍵問