我已经看到很多人都有这个问题,但提供给他们的解决方案(比如把布局放在另一个布局中)不起作用。所以我决定上传我自己的代码,希望有人知道它有什么问题。我有每行2个按钮(subtract_button & ADD_BUTTON),即使用以下适配器类显示一个列表视图:在列表视图按钮点击,日志显示:“ViewPostImeInputStage ACTION_DOWN”
public class AdapterSIUsed2 extends CursorAdapter implements View.OnClickListener {
private RowViewHolder rowView = new RowViewHolder();
public static class RowViewHolder {
public TextView name,used,category,amount;
public Button subtract,add;
public int position;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case (rowView.add.getId()):
// Do something
Log.d("Button clicked","add");
return;
case (rowView.subtract.getId()):
Log.d("Button clicked","subtract");
return;
}
}
public AdapterSIUsed2(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor csr, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View listView = inflater.inflate(R.layout.listlayout_shopping_items_used, null);
rowView.name = (TextView) listView.findViewById(R.id.shopping_items_name);
rowView.used = (TextView) listView.findViewById(R.id.shopping_items_used);
rowView.category = (TextView) listView.findViewById(R.id.shopping_items_category);
rowView.amount = (TextView) listView.findViewById(R.id.shopping_items_amount);
rowView.subtract = (Button) listView.findViewById(R.id.subtract_button);
rowView.add = (Button) listView.findViewById(R.id.shopping_items_add_button);
rowView.position = csr.getPosition();
rowView.subtract.setOnClickListener(this);
rowView.add.setOnClickListener(this);
listView.setTag(rowView);
rowView.name.setTag(rowView);
rowView.used.setTag(rowView);
rowView.category.setTag(rowView);
rowView.amount.setTag(rowView);
return listView;
}
@Override
public void bindView(View view, Context context, Cursor csr) {
TextView name = (TextView) view.findViewById(R.id.shopping_items_name);
TextView used = (TextView) view.findViewById(R.id.shopping_items_used);
TextView category = (TextView) view.findViewById(R.id.shopping_items_category);
TextView amount = (TextView) view.findViewById(R.id.shopping_items_amount);
name.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_1)));
used.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_2)));
category.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_3)));
amount.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_4)));
Button add = (Button) view.findViewById(R.id.shopping_items_add_button);
add.setOnClickListener(this);
Button subtract = (Button) view.findViewById(R.id.subtract_button);
subtract.setOnClickListener(this);
int pos = csr.getPosition();
add.setTag(pos);
subtract.setTag(pos);
}
奇怪的是,当我点击添加或减按钮,在两种情况下我的日志显示以下两行: d/ViewRootImpl:ViewPostImeInputStage ACTION_DOWN I /条点击::( ,当我点击列表中的项目本身,我的日志只显示以下行: d/ViewRootImpl:ViewPostImeInputStage ACTION_DOWN
有谁知道这里发生了什么,以及如何fi x它?提前致谢!
首先,非常感谢你的回复我尽我所能明白你在说什么,我修改了代码一些方法,但不幸的是没有成功,在问题是我现在的代码。在与“案件”的线条,它告诉我“需要不断表达”,你知道吗?如何解决这个问题? – BramH
对,我忘了那个。你不能在案例陈述中加入可能会改变的东西 - 它必须是不变的。在这种情况下,只需使用正常的if语句。我会编辑我的帖子,以便更清楚地了解如何运作。 – evanklicker
非常好,现在给我的是以下内容:按减按钮; “D/ViewRootImpl:ViewPostImeInputStage ACTION_DOWN”和“D/Button clicked:subtract”,按add按钮; “ViewPostImeInputStage ACTION_DOWN”和“D/Button clicked:add”,在列表视图项中按其他位置; “D/ViewRootImpl:ViewPostImeInputStage ACTION_DOWN”。所以它现在可以识别按钮。我希望它仍然给ACTION_DOWN的事实并不重要,你知道它是否会发生? – BramH