调用onClick在不同布局上的按钮

时间:2017-08-27 08:40:54

标签: android

我正在开发一个我有列表视图的应用程序。列表视图有一个位于不同布局文件上的按钮。如何从我的活动中调出按钮?我已经尝试将xml文件添加到我的活动中,但是setOnClickListener似乎不起作用。

这是我的xml文件的样子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:orientation="horizontal"
    android:padding="20dp">

    <EditText
        android:id="@+id/objectiveOtherAddedEditText"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginLeft="28dp"
        android:ems="10" />

    <ImageButton
        android:id="@+id/addOtherObjectiveTextBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:background="@drawable/round_button"
        android:src="@android:drawable/ic_input_add"/>

</LinearLayout>

这就是我想要的地方:

public class VisitObjectiveFragment extends Fragment {

public static final String TITLE = "Visit Objective";
SQLiteDatabase database;
SQLiteOpenHelper dbHelper;

public static VisitObjectiveFragment newInstance() {

    return new VisitObjectiveFragment();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_visit_objective, container, false);
    final View view1 = inflater.inflate(R.layout.other_objective_list_view, container, false);


    ListView listView = (ListView) view.findViewById(R.id.listview_objective);
    ImageButton buttonAdd = (ImageButton) view1.findViewById(R.id.addOtherObjectiveTextBox);

    buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

}

4 个答案:

答案 0 :(得分:1)

您可以使用<include></include>标记指定父xml

中的xml

答案 1 :(得分:1)

setOnClickListener(new View.OnClickListener() {}放入适配器类中。如果你没有。创建它

答案 2 :(得分:0)

您应该将膨胀的对象添加到主视图

public class VisitObjectiveFragment extends Fragment {

public static final String TITLE = "Visit Objective";
SQLiteDatabase database;
SQLiteOpenHelper dbHelper;

public static VisitObjectiveFragment newInstance() {

    return new VisitObjectiveFragment();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_visit_objective, container, false);
    final View view1 = getActivity().getLayoutInflater().inflate(R.layout.other_objective_list_view, null);


    ListView listView = (ListView) view.findViewById(R.id.listview_objective);
    ImageButton buttonAdd = (ImageButton) view1.findViewById(R.id.addNewOtherObjectiveTextBox);

    view.addView(buttonAdd);

    buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

答案 3 :(得分:0)

您只需要创建回调接口。请参阅官方文档(下面的链接)https://developer.android.com/training/basics/fragments/communicating.html 它允许您在片段活动之间进行通信。