80,493
社区成员
发帖
与我相关
我的任务
分享
Button btn = (Button)arg1.findViewById(R.id.bn);
if (btn.getBackground().
equals(R.drawable.b_on))
{
} else {
Toast.makeText(XXX.this, "dhfhdjfhdsjfhajfhjahhfa", Toast.LENGTH_SHORT).show();
}
}
});
//添加点击
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
}
}); package com.min.TestListView;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class TestListView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 绑定Layout里面的ListView
ListView list = (ListView) findViewById(R.id.ListView01);
// 生成动态数组,加入数据
ArrayList<HashMap<String, Object>> listItem
= new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
if (0 == i%2) {
map.put("ItemButton", "ON");
} else {
map.put("ItemButton", "OFF");
}
map.put("ItemTitle", "Level "+i);
map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");
listItem.add(map);
}
// 生成适配器的Item和动态数组对应的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,// 数据源
R.layout.list_items,//ListItem的XML实现
// 动态数组与ImageItem对应的子项
new String[] {"ItemButton","ItemTitle", "ItemText"},
// list_items的XML文件里面的一个Button,两个TextView ID
new int[] {R.id.ItemButton,R.id.ItemTitle,R.id.ItemText}
);
// 添加并且显示
list.setAdapter(listItemAdapter);
// 添加点击
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
Button btn = (Button)arg1.findViewById(R.id.ItemButton);
if (btn.getText().equals("ON"))
{
setTitle("Button is ON");
} else {
Toast.makeText(getApplicationContext(), "Button is OFF!", Toast.LENGTH_SHORT).show();
}
}
});
// 添加长按点击
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长按菜单-ContextMenu");
menu.add(0, 0, 0, "弹出长按菜单0");
menu.add(0, 1, 0, "弹出长按菜单1");
}
});
}
// 长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
return super.onContextItemSelected(item);
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView01"
/>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<Button
android:paddingTop="12dip"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ItemButton"
android:text="ON"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:text="TextView01"
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="fill_parent"
android:id="@+id/ItemTitle"
/>
<TextView
android:text="TextView02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/ItemTitle"
android:id="@+id/ItemText"
/>
</RelativeLayout>