的背景图片
back_Imagebutton.setBackgroundResource(R.drawable. left);
photo_Imagebutton.setBackgroundResource(R.drawable. photo); //给 ImageButton 设置事件
监听器
new photo_Imagebutton.setOnClickListener(new ImageButton.OnClickListener()
{ @Override public void onClick(View v) { // TODO Auto-generated method stub Tag=!
Tag;//
更改背景图片
if if(Tag){ back_Imagebutton.setBackgroundResource(R.drawable.
right); else }else else{ back_Imagebutton.setBackgroundResource(R.drawable. left);
} } }); } }
⑤ 结果
6
Android
编程基础 自定义下拉菜单
Spinner
与
setDropDownViewResource
①
新建项目,在
res
目录下新建一个
anim
文件夹,存放动画效果用,在其中新建一个
my_anim.xml
文
件
<?xml version="1.0" encoding="utf-8" ?>
②
在
res
目录下的
layout
文件夹中新建一个
myspinner_dropdown.xml 文件,用来存放下拉菜单
弹出内 容的布局
<?xml version="1.0" encoding="utf-8" ?> <TextView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1"
android:layout_width="wrap_content" android:layout_height="24sp"
android:singleLine="true" style="?android:attr/spinnerDropDownItemStyle" />
③ 修改
main.xml
,添加
UI
元素
<?xml version="1.0" encoding="utf-8" ?>
<TextView android:id="@+id/TextView_Show" android:layout_width="fill_parent"
7
Android 编程基础
android:layout_height="wrap_content" android:text="你选择的是"
android:textSize="25sp"> </TextView>
④
定义一个下拉菜单
private Spinner mySpinner;
/*
以
findViewById()
取得对象
*/ mySpinner = (Spinner) findViewById(R.id.
spinner_City);
⑤
定义一个字符串数组和一个
ArrayAdepter private static final String[]
countriesStr = { "
北京市
", "
上海市
", "
天津市
", "
重庆市
" }; private ArrayAdapter
adapter; new this adapter=new ArrayAdapter(this this, android.R.layout.
simple_spinner_item , countriesStr );
⑥
给下拉菜单内容设置样式
/* myspinner_dropdown
为自定义下拉菜单样式定义在
res/layout
目录下
*/ adapter.setDropDownViewResource(R.layout.
myspinner_dropdown );
⑦
给下拉菜单设置内容适配器
/*
将
ArrayAdapter
添加
Spinner
对象中
*/
mySpinner.setAdapter(adapter);
给下拉菜单添加动画
/* 取得 Animation 定义在 res/anim 目录下
*/ this myAnimation = AnimationUtils. loadAnimation (this R.anim. my_anim ); this,
/* 将 mySpinner 运行 Animation */ mySpinner.startAnimation(myAnimation);
⑧
mainActivity.java
⑨
完整代码
package zyf.Ex_Ctrl_8; import android.app.Activity;
import android.os.Bundle;
8
Android 编程基础
import import import import import import import import
android.view.MotionEvent; android.view.View; android.view.animation.Animation;
android.view.animation.AnimationUtils; android.widget.AdapterView;
android.widget.ArrayAdapter; android.widget.Spinner; android.widget.TextView;
public class Ex_Ctrl_8 extends Activity { /** Called when the activity is
first created. */ private static final String[] countriesStr = { "
北京市
", " 上海市
", "
天津市
", "
重庆市
" }; private TextView myTextView; private Spinner mySpinner;
private ArrayAdapter adapter; Animation myAnimation; @Override public void
onCreate(Bundle savedInstanceState) { super super.onCreate(savedInstanceState); /*
载入
main.xml Layout */ setContentView(R.layout. main); /*
以
findViewById() 取得对象
*/ myTextView = (TextView) findViewById(R.id. TextView_Show); mySpinner =
(Spinner) findViewById(R.id. spinner_City); /* 取得 Animation 定义在 res/anim 目录下*/
this myAnimation = AnimationUtils. loadAnimation (this R.anim. my_anim ); this,
new this adapter=new ArrayAdapter(this this, android.R.layout. simple_spinner_item
, countriesStr ); /* myspinner_dropdown
为自定义下拉菜单样式定义在
res/layout
目录下
*/
adapter.setDropDownViewResource(R.layout. myspinner_dropdown ); /*
将
ArrayAdapter
添加
Spinner
对象中
*/ mySpinner.setAdapter(adapter); /*下拉菜单弹出的内容选项被选中事件处