android--widget介绍之ImageButton

在Android UI设计中,为了提升界面艺术感,通常会使用ImageButton替代普通的Button。本文通过一个包含点击事件处理的demo,介绍了如何在XML布局文件中设置ImageButton,并在活动代码中进行交互操作。
android--widget介绍之ImageButton

在UI设计中,Button是一个常用控件,但是Button太普通,不够艺术化,和页面的其他元素不协调,这时,往往会希望用图片代替,ImageButton由此产生。

如下是一个完整的demo,包含了click事件处理

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout 
	    android:orientation="vertical"
	    android:layout_width="wrap_content"
	    android:layout_height="fill_parent"
	    >
	    <Button android:text="ok1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	    <ImageButton android:src="@drawable/lock1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageButton1"></ImageButton>
	</LinearLayout>
    <ImageButton android:src="@drawable/lock2" android:layout_height="wrap_content" android:layout_width="wrap_content" 
    		android:id="@+id/imageButton2"  android:layout_marginTop="30px"></ImageButton>
</LinearLayout>

activity代码:
public class Ex02Activity extends Activity {
	
	private Button btnOk;
	private ImageButton btnCc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnOk = (Button) findViewById(R.id.button1);
        btnCc = (ImageButton) findViewById(R.id.imageButton1);
        
        btnOk.setBackgroundColor(Color.BLUE);
        btnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setTitle("this is btnOk");
			}
		});
        btnCc.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(Ex02Activity.this, "BMI 計算器", Toast.LENGTH_SHORT).show();
				/*new AlertDialog.Builder(Ex02Activity.this)
				.setTitle("關於 Android BMI")
				.setMessage("Android BMI Calc")
				.setPositiveButton("ok", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				})
				.show();*/
        	}
		});
    }

说明: android有ImageButton控件,只需附上图片的来源即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值