做过radiobutton的人都知道,它自带有图标,但是 俺上下键却没有出现选中的图标,我现在要实现这样的效果
下下键时 出现
radiobutton我不知道是如何实现的(没找到,如果有知道的 请分享一下哈)
我是采用listview来实现的
listview有一个ItemSelectedListener事件,我们只需要注册这个事件就行了
listView.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int postion, long id) {
int counts=adapterView.getCount();
for (int i = 0; i < counts; i++) {
View changeView = adapterView.getChildAt(((postion+i)==counts?counts-1-i:postion+i));// 获得下一个view
ImageView imgView=(ImageView) view.findViewById(R.id.img);
ImageView changeImgView=(ImageView) changeView.findViewById(R.id.img);
imgView.setImageResource(R.drawable.asd);
changeImgView.setImageResource(R.drawable.ddd);
}
}
当然 事先得填充listviewlistView = new ListView(this);
adapter = new SimpleAdapter(this, getDefaultDate(), R.layout.listitem, new String[] { "img", "text" },
new int[] { R.id.img, R.id.text });
// rd=(RadioGroup) findViewById(R.id.menu);
listView.setAdapter(adapter);
setContentView(listView);
private List<Map<String, Object>> getDefaultDate() {
List<Map<String, Object>> date = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.asd);
map.put("text", "开启");
date.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.ddd);
map.put("text", "关闭");
date.add(map);
return date;
}
listitem.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<TextView android:id="@+id/text"
android:layout_width="fill_parent" android:textColor="@color/white" android:textSize="24px"
android:layout_height="wrap_content"/>
</LinearLayout>