web-dev-qa-db-fra.com

comment définir la méthode onClick avec linearLayout?

Je crée une application et pose une question hier comment démarrer une activité en cliquant n'importe où sur la ligne . Vous avez une idée à faire comme ci-dessous mais je reçois ceci error:

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

Voici mon 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

au-dessous de la ligne est votre code: -

 menu_photos.setOnClickListener(new OnClickListener() { 

remplace cette ligne par le code ci-dessous: -

 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();
            }
        });

définir l'attribut LinearLayout Android:clickable="true"
si vous avez un bouton ou une vue de texte dans la disposition set Android:clickable="false" pour chacun

40
Hamidreza

Dans votre fichier de présentation XML, je pense que ce que vous recherchez est une liste, et non des charges de linéarisations. Ici est un bon tutoriel pour vous aider à commencer, peut être exactement ce dont vous avez besoin, rangée avec image et texte. Pour la listview, utilisez simplement setOnItemCLickListener () .

De retour à ce que vous voulez, je pense que vous devez rendre la disposition linéaire focusable/touchable afin de "recevoir" les clics.

0
crios

Bonne capture par @duggu .. En dehors de la réponse, en utilisant ImageView et TextView dans LinearLayout, imbriquer LinearLayout est une mauvaise idée. Utilisez plutôt drawableLeft:@src d'un TextView et pour un diviseur, vous pouvez utiliser une simple vue au lieu de gonfler un dessin.

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