Android OnClick无法在Android 4.3上运行

时间:2014-10-17 17:26:18

标签: android android-fragments android-imageview android-view

我在主要活动中有一个片段。片段包含一个图像视图。我的目标是将图像视图分成3个不同的按钮。所以,我设置了一个与图像视图重叠的线性布局,它包含3个可点击的同等重要的观点。

在片段中我实现了OnClickListener,就像这个 -

public class FragmentHomeMenu extends Fragment implements OnClickListener {

View view;

Context activity_context;

View button_search_1;

View button_search_2;

View button_search_3;

...plenty of other stuff

public FragmentHomeMenu() {

}

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

    view = inflater.inflate(R.layout.fragment_home_menu, container, false);

    activity_context = getActivity().getApplicationContext();

    button_search_1 = (View) view
            .findViewById(R.id.button_search_1);
    button_search_1.setOnClickListener(this);

    button_search_2 = (View) view.findViewById(R.id.button_search_2);
    button_search_2.setOnClickListener(this);


    button_search_3 = (View) view.findViewById(R.id.button_search_3);
    button_search_3.setOnClickListener(this);
    ....plenty of other stuff

return view;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    activity_context = activity.getApplicationContext();
}


@Override
public void onClick(View v) {
    Log.w("", "some thing was clicked");
    switch (v.getId()) {
    case R.id.button_search_2:
        dostuff1();//stuff1 is not happening in android 4.3,but works on 4.0 
        break;
    case R.id.button_search_1:
        dostuff2();//stuff2 is not happening in android 4.3,but works on 4.0
        break;
    ......plenty of other stuff
}

我在很多地方检查过这个问题,但找不到解决方案。我的代码完美无缺 使用Android 4.0但在4.3中的设备当我点击图像时没有任何反应。

这是xml -

.......

<ImageView
            android:id="@+id/imageview_searchbar"
            android:layout_width="370dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rltv1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:src="@drawable/search_bar" />



        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageview_searchbar"
            android:layout_alignLeft="@+id/imageview_searchbar"
            android:layout_alignRight="@id/imageview_searchbar"
            android:layout_alignTop="@+id/imageview_searchbar"
            android:orientation="horizontal"
            android:weightSum="21" >

            <View
                android:id="@+id/button_search_1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="7"
                android:clickable="true" />

            <View
                android:id="@+id/button_search_2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:clickable="true" />

            <View
                android:id="@+id/button_search_3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:clickable="true" />
        </LinearLayout>

.......lots of other stuff

1 个答案:

答案 0 :(得分:0)

我想出来了 - 我只需将线性布局(包含按钮)带到前面。 当使用android 4.3时,它隐藏在图像下,所以我做了这个 -

LinearLayout search_bar_buttons = (LinearLayout) view.findViewById(R.id.ll_search_bar_buttons);
    search_bar_buttons.bringToFront();

谢谢你们的帮助