android教程之用户界面篇ImageButton

本文将演示如何在Android应用中使用ImageButton控件,通过整张图片作为响应按钮,并展示如何在XML文件和Java文件中定义图片,以及如何为按钮设置点击事件。

       图片按钮控件的使用跟其他控件的使用大同小异,就是使用整张图片作为响应按钮,而ImageButton中的图片既可以再xml文件中定义,也可以在java文件中定义,还是老规矩,我们直接上代码演示:

imagebutton.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- android:contentDescription 新版android系统需加上这个属性,要不然会有小警告 -->
    <ImageButton 
        android:id="@+id/imgbt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imagebutton"/>
    <TextView 
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

TestImageButton.java

public class TestImageButton extends Activity{
	private ImageButton imgbt;
	private TextView text3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagebutton);
		imgbt=(ImageButton) this.findViewById(R.id.imgbt);
		imgbt.setImageResource(android.R.drawable.ic_menu_more); //使用系统自带的图片
		text3=(TextView) this.findViewById(R.id.text3);
		
		imgbt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				text3.setText("您点击的图片按钮");
			}
		});
	}
}

赶快把你自己的程序运行起来吧

转载于:https://www.cnblogs.com/IntelligentBrain/p/5111301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值