Java 在动态添加的线性布局上放置onClickListeners
我正在尝试将clickListener添加到动态添加的LinearLayout中的ImageButton中,该LinearLayout是另一个(包装器)LinearLayout的一部分: 包装器:Java 在动态添加的线性布局上放置onClickListeners,java,android,onclicklistener,Java,Android,Onclicklistener,我正在尝试将clickListener添加到动态添加的LinearLayout中的ImageButton中,该LinearLayout是另一个(包装器)LinearLayout的一部分: 包装器: <LinearLayout android:id="@+id/persons_wrapper" android:layout_width="fill_parent" android:layout_height="wrap_content"
<LinearLayout
android:id="@+id/persons_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
嗯,如果你调用了onClicklistener,正如你的日志所示,那么问题似乎出现在你没有提供的shareTextWithChosenApp方法中。我将添加该方法,但我认为你错了,因为日志应该在调用shareTextWithChosenApp之前出现。你不是说,“clicked”被记录了吗?,我所说的logged是指打印到logcatWell,这是我的错,我从onClick内部复制标签后没有更改标签。它应该像:“for loop”,“inside”啊好的,所以你的问题是你没有注册你的点击事件。这似乎是某种自制的listview。你为什么要建立自己的公司?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/person_row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/share_with_person"
android:src="@drawable/share_with"
android:layout_weight="0"
android:layout_marginTop="5dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="fitCenter"
android:layout_marginRight="5dp"
/>
<TextView
android:id="@+id/person_name"
android:textColor="@color/secondary_text_color"
android:hint="@string/person_name_hint"
android:layout_marginTop="@dimen/margin_between_form_elements"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/person_amount"
android:textColor="@color/secondary_text_color"
android:hint="10.20"
android:layout_marginTop="@dimen/margin_between_form_elements"
android:layout_weight="0"
android:layout_width="90dp"
android:layout_height="fill_parent" />
</LinearLayout>
ViewGroup parentView = (ViewGroup) findViewById(R.id.persons_wrapper);
for (int i = 0; i < parentView.getChildCount(); i++) {
//get the parent's childview, which is the linear layout wrapped around the name and amount for a person
final View childView = parentView.getChildAt(i);
Log.i("for loop", "looping");
ImageButton shareWithPerson = (ImageButton) childView.findViewById(R.id.share_with_person);
shareWithPerson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("clicked", "share");
shareTextWithChosenApp(childView);
}
});
shareWithPerson.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.i("clicked", "long click");
return false;
}
});
}
private void shareTextWithChosenApp(View personRow) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
TextView personAmount = (TextView) personRow.findViewById(R.id.person_amount);
String shareBody = personAmount.getText().toString();
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Rekening delen");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Delen met: "));
}