Android 控件开发之ImageButton

/**
 * 除了Android系统自带的Button按钮一万,还提供了带图标的按钮ImageButton 
 * 要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable
 * 方法来设置要显示的图标。

注意:
我们可以在布局文件中就直接设置按钮的图标,如
android:src="@drawable/icon1"

我们也可以在程序中设置自定义图标
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));

我们还可以使用系统自带的图标
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));

       设置完按钮的图标后,需要为按钮设置监听setOnClickListener,以此捕获事件并处理

*/

   ImageButton 效果图:



本程序main.xml源码:
  

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="这是一个带图片的按钮.ImageButton"/>
    
<ImageButton 
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>





本程序java源码

package com.sx.ImageButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;


public class ImageButtonActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
        
        imageButton.setImageDrawable((getResources().getDrawable(R.drawable.icon)));
        
        imageButton.setOnClickListener(new Button.OnClickListener()
        {
		@Override
		public void onClick(View arg0) 
		{
			//ImageButton事件响应
		}
         
        });
    }
}



   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章