34

我正在创建一个应用程序并昨天发布了一个问题, 如何通过单击行上的任何位置来开始活动。有一个想法,如下所示,但我收到此错误:

View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数(new OnClickListener(){})

这是我的代码:

LinearLayout menu_photos = (LinearLayout )findViewById(R.id.picture_part);
menu_photos.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View v) {
        Intent picture_intent = new Intent(CurrentActivity.this,PictureActivity.class);
        startActivity(picture_intent );     
    }
});

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/main_background"
   android:orientation="vertical" >
   <include
        android:id="@id/includeTop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/private_space_title" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<LinearLayout
    android:id="@id/mail_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_list_bg"
    android:orientation="vertical"
    android:paddingBottom="10.0dip"
    android:paddingLeft="20.0dip"
    android:paddingRight="20.0dip"
    android:paddingTop="15.0dip" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@id/picture_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <ImageView
            android:id="@id/left_icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10.0dip"
            android:src="@drawable/icon_picture" />

        <TextView
            android:id="@id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_img_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/video_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <ImageView
            android:id="@id/left_icon2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10.0dip"
            android:src="@drawable/icon_video" />

        <TextView
            android:id="@id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_video_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/sms_call_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@id/left_icon2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10.0dip"
                android:src="@drawable/icon_sms_call" />

            <TextView
                android:id="@id/unread_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@id/left_icon2"
                android:layout_marginRight="5.0dip"
                android:layout_marginTop="3.0dip"
                android:background="@drawable/counter"
                android:gravity="center"
                android:textColor="@color/white"
                android:visibility="visible" />
        </RelativeLayout>

        <TextView
            android:id="@id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_sms_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/private_contact_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <ImageView
            android:id="@id/left_icon4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10.0dip"
            android:src="@drawable/icon_contact" />

        <TextView
            android:id="@id/textView4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_privacy_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />

    <LinearLayout
        android:id="@id/break_in_part"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:clickable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10.0dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@id/left_icon5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10.0dip"
                android:src="@drawable/icon_break_in" />

            <TextView
                android:id="@id/login_record_main_unread_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@id/left_icon5"
                android:layout_marginRight="5.0dip"
                android:layout_marginTop="3.0dip"
                android:background="@drawable/counter"
                android:gravity="center"
                android:textColor="@color/white"
                android:visibility="visible" />
        </RelativeLayout>

        <TextView
            android:id="@id/textView5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="@string/function_break_in_management"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@id/right_icon5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/listview_arrow" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/list_view_divider" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="90.0dip"
    android:layout_weight="1.0"
    android:gravity="center" >

    <Button
        android:id="@id/member_up_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20.0dip"
        android:layout_marginRight="20.0dip"
        android:background="@drawable/upgrade_selector"
        android:text="@string/main_upgrade_button"
        android:textSize="18.0sp" />
</LinearLayout>

4

4 回答 4

54

下面的行是你的代码: -

menu_photos.setOnClickListener(new OnClickListener() { 

将此行替换为以下代码:-

menu_photos.setOnClickListener(new View.OnClickListener() { 
于 2013-03-24T08:32:21.497 回答
48
LinearLayout app_layer = (LinearLayout) findViewById (R.id.main_activity_app_layer);
app_layer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
    }
});

如果您在所有android:clickable="true"
布局中都有任何按钮或文本视图,请设置 LinearLayout 属性set android:clickable="false"

于 2014-09-03T09:21:48.163 回答
0

从您的 xml 布局文件中,我认为您正在寻找的是一个列表视图,而不是大量的线性布局。是一个很好的教程,可以帮助您入门,可能正是您需要的,带有图像和文本的行。对于列表视图,只需使用setOnItemCLickListener()

回到你想要的,我认为你需要使线性布局可聚焦/可触摸以便“接收”点击。

于 2013-03-24T08:44:04.253 回答
0

@duggu 的大收获。除了答案之外,在 LinearLayout 中使用 ImageView 和 TextView,嵌套 LinearLayout 是个坏主意。代替使用drawableLeft:@srcTextView 和分隔线,您可以使用简单的 View 而不是膨胀 drawable。

<View
android:background="#aeaeae"
android:layout_width="match_parent"
android:layout_height="0.5dp"/>
于 2018-06-26T09:31:39.823 回答