it-swarm.com.de

wie kann ich die onClick-Methode mit linearLayout einstellen?

Ich erstelle eine Anwendung und habe gestern eine Frage gepostet wie Sie die Aktivität starten können, indem Sie auf eine beliebige Stelle in der Zeile klicken . Haben Sie eine Idee wie unten, aber ich bekomme diese error:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})

Hier ist mein code:

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>

 

28
Smarty Gurl

unter der Zeile ist der Code: -

 menu_photos.setOnClickListener(new OnClickListener() { 

ersetzen Sie diese Zeile durch folgenden Code: -

 menu_photos.setOnClickListener(new View.OnClickListener() { 
44
duggu
        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();
            }
        });

linearLayout-Attribut setzen Android:clickable="true"
wenn Sie eine Schaltfläche oder Textansicht im Layout set Android:clickable="false" für alle haben

40
Hamidreza

Ich denke, aus Ihrer XML-Layoutdatei denke ich, was Sie suchen, ist eine Listenansicht, nicht eine Menge linearer Layouts. Hier ist ein schönes Tutorial, um Ihnen den Einstieg zu erleichtern, vielleicht genau das, was Sie brauchen, Zeile mit Bild und Text. Für die Listenansicht verwenden Sie einfach setOnItemCLickListener () .

Zurück zu dem, was Sie wollen, ich denke, Sie müssen das Linearlayout fokussierbar/berührbar machen, um Klicks zu erhalten.

0
crios

Great catch by @duggu . Abgesehen von der Antwort ist die Verschachtelung von LinearLayout mit ImageView und TextView in einem LinearLayout eine schlechte Idee. Verwenden Sie stattdessen drawableLeft:@src eines TextView und auch für einen Teiler können Sie eine einfache Ansicht verwenden, anstatt ein Drawable aufzublähen.

<View
Android:background="#aeaeae"
Android:layout_width="match_parent"
Android:layout_height="0.5dp"/>
0
Sagar Wankhede