单击侦听器+复选框-在android上
Posted
技术标签:
【中文标题】单击侦听器+复选框-在android上【英文标题】:click listener + checkbox - on android 【发布时间】:2014-03-06 04:31:25 【问题描述】:public class ChooseFavorites extends Activity implements OnItemClickListener
StationManager st;
MyCustomAdapter arr;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_favorites);
st = new StationManager();
list = (ListView)findViewById(R.id.stationsList1);
arr = new MyCustomAdapter(this, android.R.layout.simple_list_item_multiple_choice, st.getNamesOfStations());
list.setAdapter(arr);
list.setOnItemClickListener(this);
class MyCustomAdapter extends ArrayAdapter<String> implements OnClickListener
LayoutInflater inflater;
Context ctx;
CheckBox cb;
String[] stationNames;
TextView stationName1;
public MyCustomAdapter(Context context, int textViewResourceId, String[] stationNames)
super(context, textViewResourceId, textViewResourceId, stationNames);
ctx = context;
this.stationNames = stationNames;
inflater = LayoutInflater.from(context);
@Override
public int getCount()
// TODO Auto-generated method stub
return 23;
@Override
public String getItem(int position)
// TODO Auto-generated method stub
return stationNames[position];
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
View row = convertView;
if(row==null)
// Object reuse
inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favorite_row, parent, false);
stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName1.setText(stationNames[position]);
cb=(CheckBox)row.findViewById(R.id.cbCheck);
row.setOnClickListener(this);
return row;
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.show();
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT);
toast.show();
我不知道你是否只能通过代码看到这个......
但是如果我的服装适配器类中没有onClick
,什么都不会发生......
android 不使用“on item click listener”
但它确实适用于“点击”方法....
问题是:我没有真正需要的位置...
希望任何人都可以帮助我!谢谢大家!
【问题讨论】:
试试这个监听器 onitemclicklistener 会给你列表视图的选定位置 这很重要...因为项目点击侦听器在列表中...这不是列表... 您想检测用户何时点击复选框? 我想检测用户何时以及在哪一行点击了复选框 【参考方案1】:您可以使用此事件来获得有关复选框被选中/取消选中的通知
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
【讨论】:
它仍然不会告诉我我正在使用哪一行......只有当我将位置作为最终...... 确定最终位置没有问题..随着时间的推移,它确实不应该针对一个特定的复选框而改变,所以请随意将其定为最终.. 好吧,如果我做最后...它给我一个奇怪的数字...它只给我 0 或 10 它给了我我正在使用的最后一行的编号......我想做的就是在我的列表上的项目点击监听器上使用......但由于某种原因我不能!跨度> 如何在 getView 方法中设置监听器?请注意,您必须为每个复选框都有一个唯一的实例。然后它应该可以工作。看起来您对所有项目只有一个实例。您可以发布更新的代码吗?【参考方案2】:使用 Checkbox setOnCheckedChangeListener 事件。
试试这样...
CheckBox ChkBx = ( CheckBox ) findViewById( R.id.checkbox);
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
if ( isChecked )
// perform logic
);
【讨论】:
是的,我知道如何使用它......它仍然没有告诉我我正在使用哪条线路......但无论如何 tnx :)【参考方案3】:您可以使用以下内容:
listView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
);
或者您可以将位置作为标签保存到checkbox
并检查checkbox clicklistener
@Override
public View getView(int position, View convertView, ViewGroup parent)
// ur code...
**cb.setTag(position);**
row.setOnClickListener(this);
return row;
@Override
public void onClick(View v)
// TODO Auto-generated method stub
**int position = v.getTag();** //cast & check for Tag not null too.
Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.show();
【讨论】:
第一个选项我试过了,它没有用...第二个选项还不错:) 两者都可以工作,CheckBox 是第一种方法中视图的子项,并且方法已经为您提供了位置【参考方案4】: @Override
public View getView(final int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
View row = convertView;
if(row==null)
// Object reuse
inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favorite_row, parent, false);
stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName1.setText(stationNames[position]);
cb=(CheckBox)row.findViewById(R.id.cbCheck);
cb.setOnCheckedChangeListener(new CompoundButton.
OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
if (isChecked)
cb.setChecked(isChecked);
Toast toast = Toast.makeText(getApplicationContext(), "checked" + position, Toast.LENGTH_SHORT);
toast.show();
else
cb.setChecked(isChecked);
Toast toast = Toast.makeText(getApplicationContext(), "unChecked" + position, Toast.LENGTH_SHORT);
toast.show();
);
return row;
【讨论】:
这行得通吗?它应该..您可以从代码中删除cb.setChecked(isChecked);
..
好吧,如果我删除了那条线,那么盒子就会完全检查(盒子里没有 V)
看看这个简单的项目..它在那里工作github.com/simekadam/checkboxtest【参考方案5】:
public View getView(int position, View convertView, ViewGroup parent)
viewHolder holder;
// Reuse an existing view, if one is supplied, otherwise create a new one
// Check to see if this one is created yet ?
if (convertView == null)
// Get inflater
LayoutInflater inflater = (LayoutInflater)
parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Create a view for it
convertView = inflater.inflate(R.layout.test_reprot_lv_items, parent, false);
// Creates a holder class to contain all objects of a row
holder = new viewHolder();
holder.equipmentID = (TextView) convertView.findViewById(R.id.equipmentID);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set checkBox's Tag with the position of the row in the listview
holder.checkBox.setTag(position);
// Sets the tag associated with this view for later reuse.
convertView.setTag(holder);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
holder.checkBox.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
CheckBox c = (CheckBox) v;
int location = (Integer) c.getTag();
// Set selected state with the location of the row in the listview
itemsList.get(location).setSelectedState(c.isChecked());
);
else
// In order to reuse the preallocated memory, get the holder from View's tag,
// which is allocated and saved in this view object. When a view requests
// to be rendered and it has not been instantiated, we new a holder and save
// it to View's 'Tag' for later reuse.
holder = (viewHolder) convertView.getTag();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set checkBox's Tag with the position of the row in the listview
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
CheckBox c = (CheckBox) v;
int location = (Integer) c.getTag();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set selected state with the location of the row in the listview
// The method setSelectedState, which provided by your own class,
// is called to update the checkbox state.
itemsList.get(location).setSelectedState(c.isChecked());
);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set checkbox state with calling the method getSelectedState which
// provied by your own class.
holder.checkBox.setChecked(itemsList.get(position).getSelectedState());
return convertView;
【讨论】:
以上是关于单击侦听器+复选框-在android上的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 的 EditText 中单击 drawableleft 的侦听器?