android应用开发揭秘examples_04-16笔记(ImageButton的使用)

其实ImageButton用法比较简单,有2种方式:
1. 静态xml定义
  <ImageButton
  android:id="@+id/ImageButton01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:s rc="@drawable/button1"
  >
2. 动态程序调用
例如
m_ImageButton2.setImageDrawable(getResources().getDrawable(R.drawable.button2)); //写法比较复杂,常练习背过它!

以上所谓drawable都是指的res/drawable分支下的图片之类。

//----------------------代码 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:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
  <ImageButton
  android:id="@+id/ImageButton01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:s rc="@drawable/button1"
  >
  </ImageButton> 
  <ImageButton
  android:id="@+id/ImageButton02"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  >
  </ImageButton>
  <ImageButton
  android:id="@+id/ImageButton03"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  >
  </ImageButton> 
  <ImageButton
  android:id="@+id/ImageButton04"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  >
  </ImageButton>
</LinearLayout>

//----------------------代码java----------------------------
package com.yarin.android.Examples_04_16;

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.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class Activity01 extends Activity
{
    TextView    m_TextView;
    //声明4个ImageButton对象
    ImageButton    m_ImageButton1;
    ImageButton    m_ImageButton2;
    ImageButton    m_ImageButton3;
    ImageButton    m_ImageButton4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        m_TextView = (TextView) findViewById(R.id.TextView01);
        //分别取得4个ImageButton对象
        m_ImageButton1 = (ImageButton) findViewById(R.id.ImageButton01);
        m_ImageButton2 = (ImageButton) findViewById(R.id.ImageButton02);
        m_ImageButton3 = (ImageButton) findViewById(R.id.ImageButton03);
        m_ImageButton4 = (ImageButton) findViewById(R.id.ImageButton04);
       
        //分别设置所使用的图标
        //m_ImageButton1是在xml布局中设置的,这里就暂时不设置了
        m_ImageButton2.setImageDrawable(getResources().getDrawable(R.drawable.button2));
        m_ImageButton3.setImageDrawable(getResources().getDrawable(R.drawable.button3));
        m_ImageButton4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));
       
        //以下分别为每个按钮设置事件监听setOnClickListener
        m_ImageButton1.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
              //对话框
              Dialog dialog = new AlertDialog.Builder(Activity01.this)
                .setTitle("提示")
                .setMessage("我是ImageButton1")
                .setPositiveButton("确定",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                       
                    }
                }).create();//创建按钮
             
              dialog.show();
          }
        });
        m_ImageButton2.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
              Dialog dialog = new AlertDialog.Builder(Activity01.this)
                .setTitle("提示")
                .setMessage("我是ImageButton2,我要使用ImageButton3的图标")
                .setPositiveButton("确定",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        m_ImageButton2.setImageDrawable(getResources().getDrawable(R.drawable.button3));
                    }
                }).create();//创建按钮
             
              dialog.show();
          }
        });
        m_ImageButton3.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
              Dialog dialog = new AlertDialog.Builder(Activity01.this)
                .setTitle("提示")
                .setMessage("我是ImageButton3,我要使用系统打电话图标")
                .setPositiveButton("确定",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        m_ImageButton3.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));
                    }
                }).create();//创建按钮
             
              dialog.show();
          }
        });
        m_ImageButton4.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
              Dialog dialog = new AlertDialog.Builder(Activity01.this)
                .setTitle("提示")
                .setMessage("我是使用的系统图标!")
                .setPositiveButton("确定",
                new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                       
                    }
                }).create();//创建按钮
             
              dialog.show();
          }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值