可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to add button to each row of my listview. I created an XML file called row.xml
in my layout folder and added two textviews and a button in that file. But when a button is added, I am unable to click the item of listview. I'm only able to click the button. Here is row.xml
:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text11" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="#000000" /> <TextView android:id="@+id/text2" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:textColor="#000000" /> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
I want to refer to textviews and button in my activity. Please help me and suggest some ideas.
回答1:
回答2:
I had a similar issue. The simple trick is to add android:focusable="false"
to your Button.
回答3:
You can use a custom adapter (extending an array adapter is fairly simple). In the getView
method, set a onClickListener
on your TextView
, this way both your button and the other parts of the ListItem
will respond to touch.
回答4:
If you want to experiment something new for problem check LINK
Hope this will be helpfull.
回答5:
you must add focusable="false" and you can
<Button android:id="@+id/bt_do" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" **android:focusable="false"** />
in your adapter
public class MyAdapter extends BaseAdapter { private Context context; private List< Objet > objects; private OnClickListener listener; public MyAdapter(Context context, List<Objet> objects, OnClickListener listener) { this.context = context; this.objects = objects; this.listener = listener; // TODO Auto-generated constructor stub } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater infalInflater = ((Activity) context) .getLayoutInflater(); convertView = infalInflater.inflate(R.layout.my_line_list, null); } Button bt_do=(Button)convertView.findViewById(R.id.bt_do); bt_do.setOnClickListener(listener); return convertView; } @Override public boolean hasStableIds() { return true; } @Override public int getCount() { // TODO Auto-generated method stub return objects.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return objects.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } }
and in your activity create un adapter and implement listener of button.
回答6:
Please check this link...
http://commonsware.com/Android/excerpt.pdf
In that pdf in page 104, he added an image in every row of a list view. You replace it with button..
Hope this solves your problem.