3

I have not found any answer for my problem, so I need your help ... I have an LinearLayout which I want to be clickable in order to lunch another activity. So I implement an onClickListener to it. I created an selector for this LinearLayout in order that when someone click on it, the background change.

I just don't understand that :

  • If my LinearLayout doesn't have android:clickable="true" in the xml, I'm able to click on it and get what I want but the selector doesn't work.
  • If I remove this line, it is the opposite .. the selector work but not the onClick event.

So, can anyone can explain me why ? Just in case, here is my the content of my selector file :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/btn_restaurants_background_state_pressed" android:state_pressed="true"></item>
   <item android:drawable="@drawable/btn_restaurants_background_state_pressed" android:state_focused="true"></item>
   <item android:drawable="@drawable/btn_restaurants_background_state_pressed" android:state_selected="true"></item>
   <item android:drawable="@drawable/btn_restaurants_background_state_normal"></item>
</selector>

Thanks you in advance

4

1 回答 1

0

在 onCreate 方法中:

private LinearLayout llayout;
@Override
public void onCreate(Bundle savedInstance) {
   super.onCreate(savedInstance);
   llayout = (LinearLayout) findViewById(R.id.layout_name);
   llayout.setOnClickListener(this);

然后覆盖activity中的onClick方法,

  @override
  public void onClick(View v) {

     switch(v.getId()) {

        case R.id.layout_name :
              // To change the background
              llayout.setBackgroundResource(R.drawable.image_name1);

             // To call AnotherActivity
             Intent in = new Intent(this, TargetActivityName.class);
             startActivity(in);
             break;
     }
  }
于 2012-10-21T05:28:34.587 回答