27.02.2014 Views

Android 开发教程

Android 开发教程

Android 开发教程

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

-----------------------------------<strong>Android</strong> 编 程 基 础<br />

3 修 改 mainActivity.java 文 件 , 实 现 选 择 效 果<br />

package zyf.Ex_Ctrl_12ME;<br />

/* 导 入 使 用 的 包 */<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.view.View;<br />

import android.widget.ImageButton;<br />

import android.widget.TextView;<br />

public class Ex_Ctrl_12ME extends Activity implements<br />

ImageButton.OnClickListener {<br />

/** Called when the activity is first created. */<br />

/* 定 义 使 用 的 类 对 象 */<br />

private TextView showyourClick;<br />

private ImageButton button_A, button_B;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

}<br />

/* 载 入 主 屏 布 局 main.xml */<br />

setContentView(R.layout.main);<br />

/* 从 XML 中 获 取 UI 元 素 对 象 */<br />

showyourClick = (TextView) findViewById(R.id.showinform_TextView);<br />

button_A = (ImageButton) findViewById(R.id.ImageButton_A);<br />

button_B = (ImageButton) findViewById(R.id.ImageButton_B);<br />

/* 给 两 个 ImageButton 添 加 点 击 事 件 监 听 器 */<br />

button_A.setOnClickListener(this<br />

this);<br />

button_B.setOnClickListener(this<br />

this);<br />

/* button 点 击 事 件 处 理 */<br />

@Override<br />

public void onClick(View v) {<br />

// TODO Auto-generated method stub<br />

switch (v.getId()) {<br />

case R.id.ImageButton_A: {<br />

}<br />

/*button_A 按 钮 被 点 击 */<br />

/* 显 示 信 息 */<br />

showyourClick.setText(" 你 点 击 了 :\nbutton_A");<br />

/* 切 换 背 景 图 片 */<br />

button_A.setBackgroundResource(R.drawable.android);<br />

button_B.setBackgroundResource(R.drawable.installer);<br />

27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!