【急】求代码

纠结的木棉花 2010-09-10 09:24:05
有N个listView,当点击listView时,判断button的状态【有两种状态,on,off】如果是on,则相应listview单击事件,如果是off,则用toast提示!
...全文
768 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
纠结的木棉花 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hunankeda110 的回复:]
我现在要设置Button的点击事件,点击Button时才会触发,该如何做?
[/Quote]
在onclick里面设置点击事件啊
hunankeda110 2011-06-04
  • 打赏
  • 举报
回复
我现在要设置Button的点击事件,点击Button时才会触发,该如何做?
纠结的木棉花 2010-09-13
  • 打赏
  • 举报
回复
怎么?????
纠结的木棉花 2010-09-11
  • 打赏
  • 举报
回复


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();
}
}
});


这样写可以吗?
ArashmenZhu 2010-09-10
  • 打赏
  • 举报
回复
建议看下expandListView不好操作
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
帮忙给个代码!

[Quote=引用 6 楼 hero_cat 的回复:]
如果是这样的话,楼主就不能通过setItemClickListener来监听button的状态了。
有种方式是重写adapter.在adapter中的setView方法中对button进行监听。
[/Quote]
Hero_Cat 2010-09-10
  • 打赏
  • 举报
回复
如果是这样的话,楼主就不能通过setItemClickListener来监听button的状态了。
有种方式是重写adapter.在adapter中的setView方法中对button进行监听。
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
AdapterView<?> arg0, View arg1, int arg2,
long arg3

怎么知道是哪个按钮呢
yyy025025025 2010-09-10
  • 打赏
  • 举报
回复
1. 修改没有item的layout,把button加进去。
2. 基本上listview都是adpter来控制的,在adpter的getview或者bindview里面加入控制代码。
ameyume 2010-09-10
  • 打赏
  • 举报
回复
楼主参考一下的用法,把item右侧的图标改成一个button,设置ON/OFF两个状态;然后在点击函数中增加Button状态的判断就可以,根据不同状态执行不同操作。
//添加点击   
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
}
});


Android ListView常用用法
http://www.javaeye.com/topic/540423
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
一个ListView里的N个Item
ameyume 2010-09-10
  • 打赏
  • 举报
回复
是有N个listView,还是一个ListView里的N个Item?
Button有两个状态,就像Checkbox了吧。取得当前的button状态,做相应的处理就是了
楼主还是先自己写写,有问题可以再拿出来探讨。
Hero_Cat 2010-09-10
  • 打赏
  • 举报
回复
不晓得是我没理解楼主的意思还是怎么滴?
我觉得要是像楼主所描述的,应该很简单呀,直接给listview添加一个onitemclicklistener,在里面进行判断不就行了吗?
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ameyume 的回复:]
引用 13 楼 zhq56030207 的回复:
bn.setOnClickListener(this);
指的是什么?

就是设置onclick事件,点击bn时,就会执行到onClick()函数
[/Quote]

我是在一个大大的程序里面加这些,but 我真的不知道这段代码应该加在哪里?我加的地方,运行好像不起作用!!!!
ameyume 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zhq56030207 的回复:]
bn.setOnClickListener(this);
指的是什么?
[/Quote]
就是设置onclick事件,点击bn时,就会执行到onClick()函数
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
bn.setOnClickListener(this);
指的是什么?
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
button的两种状态时用drawable的图片显示,点击的时候,button显示的是红色图片on,再点击则恢复成灰色图片off
ameyume 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhq56030207 的回复:]
都好强哦!
可是我照你的加进去依然没有反应,刚开始我的button状态都被设置成false
public static boolean[] buttonState = { false, false, }
[/Quote]
你多试试
只要不要让button获得焦点就可以了。
纠结的木棉花 2010-09-10
  • 打赏
  • 举报
回复
都好强哦!
可是我照你的加进去依然没有反应,刚开始我的button状态都被设置成false
public static boolean[] buttonState = { false, false, }
ameyume 2010-09-10
  • 打赏
  • 举报
回复
用setItemClickListener也可以,只要取得view的子view就可以了
完整代码如下:
1.TestListView.java
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);
}
}


2.main.xml
<?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>


3.list_items.xml
<?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>

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧