ImageView onClickListener无法正常工作

时间:2013-06-03 12:45:44

标签: android

我在LinearLayout中有两个ImageView,一个文本字段和一个WebView。我的ImageView监听器无法正常工作。这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_main"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="back"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:src="@drawable/back_icon" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="forward"
        android:src="@drawable/back_icon"/>
    <EditText
        android:id="@+id/adressBar"
        android:imeOptions="actionDone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ems="10" >
    </EditText>

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这是我的Java代码。我应该指出Log.d语句没有触发,因此绝对不会被调用:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        L.d(">>onCreateView", "savedInstanceState null");
    }
    View v = inflater.inflate(R.layout.browser,null);
    mWebView = (WebView) v.findViewById(R.id.webView);
    back =(ImageView) v.findViewById(R.id.imageView1);
    back.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("backkkkkkkkkkkkkkkk", "msg");

        }
    });
    return v;
}

5 个答案:

答案 0 :(得分:1)

试试这个

    v = inflater.inflate(R.layout.browser, container, false);
 ImageView back = (ImageView)findViewById(R.id.imageVIew1);
        back.setOnClickListener(new View.OnClickListener(){

       @Override
    public void onClick(View v) {
        Log.d("backkkkkkkkkkkkkkkk", "msg");

    }
});  

答案 1 :(得分:0)

您需要像

那样夸大视图
v = inflater.inflate(R.layout.browser, container, false);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new View.OnClickListener())
{
..
}

答案 2 :(得分:0)

试试这个 -

back.setOnClickListener(this);

并实现onclicklistener ..

答案 3 :(得分:0)

为xml文件中的所有视图设置为focusable false。或者片段中的onActivityCreated()方法中的imageview的setOnClickListener。

答案 4 :(得分:0)

我遇到了类似问题,并通过添加{{1}}来修复此问题,以确保用户有足够的区域点击。然后,您可以按照您想要的方式对齐图像内容。就我而言:

{{1}}
相关问题