android学习笔记27--------------ImageButton

转载请注明出处:http://blog.csdn.net/yf210yf



public class ImageButtonActivity extends Activity
{
	private ImageButton imageButton;
	private TextView textView;
	private int i = 0;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		imageButton = (ImageButton) findViewById(R.id.ImageButton1);
		textView = (TextView) findViewById(R.id.text);

		/*
		 * imageButton.setOnFocusChangeListener(new OnFocusChangeListener() {
		 * 
		 * @Override public void onFocusChange(View v, boolean hasFocus) { if
		 * (hasFocus==true) { imageButton.setImageResource(R.drawable.image1);
		 * textView.setText("获得焦点"); } else {
		 * imageButton.setImageResource(R.drawable.image2);
		 * textView.setText("失去焦点"); }
		 * 
		 * } });
		 */

		imageButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (i == 0)
				{
					imageButton.setImageResource(R.drawable.image2);
					textView.setText("点击了按钮");
					i = 1;
				} else
				{
					imageButton.setImageResource(R.drawable.image1);
					textView.setText("又点击了按钮");
					i = 0;
				}
			}
		});
	}
}

<?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:id="@+id/text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageButton 
	android:id="@+id/ImageButton1" 
	android:background="@drawable/image1"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"></ImageButton>
	
</LinearLayout>



原文链接: http://blog.csdn.net/yf210yf/article/details/7274856

转载于:https://my.oschina.net/chen106106/blog/49346

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值