2016-08-03 93 views
-1

我希望點擊效果時,點擊圖像,但從我的代碼,每當我第一次單擊圖像沒有發生,但是當我第二次點擊它顯示橙色影響被點擊的效果。下面是我的代碼,我知道這可能不是這樣做的正確方法。但我想知道爲什麼發生這種情況它需要雙擊點擊效果onClick按鈕()

image.xml

<ImageView 
       style="@style/icon" 
       android:background="@drawable/fear_96" 
       android:onClick="see" 
       android:id="@+id/abulation" 
       /> 
下面

是的onclick方法

public void see(View view) { 

    view.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 

      switch (motionEvent.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        view.getBackground().setColorFilter(0xf0f47521, 
PorterDuff.Mode.SRC_ATOP); 
        view.invalidate(); 
        break; 

       case MotionEvent.ACTION_CANCEL: 
       case MotionEvent.ACTION_UP: 

        view.getBackground().clearColorFilter(); 
        view.invalidate(); 
      startActivity(view.getId()); 
        break; 
      } 

      return true; 
     } 
    }); 
    } 

    public void startActivity(int id) 
    { 
    Intent intent=new Intent(this,DuwaListView.class); 
    switch (id) 
    { 
     case R.id.abulation: 
      intent.putExtra(Intent.EXTRA_TEXT,"Abulation"); 
      break; 
     case R.id.dressing: 
      intent.putExtra(Intent.EXTRA_TEXT,"Dressing"); 
     break; 
     case R.id.restroom: 
      intent.putExtra(Intent.EXTRA_TEXT,"Restroom"); 
      break; 
     default: 

Toast.makeText(this,"underconstruction",Toast.LENGTH_SHORT).show(); 
      return; 


    } 


    startActivity(intent); 
} 

請幫助爲什麼這個代碼表明這種行爲

+0

只需使用_AlphaAnimation_或_ObjectAnimator_ – Piyush

+0

我的問題是爲什麼它顯示這樣的行爲?我知道還有其他方法可以做到這一點。請在簡單地投票之前閱讀我的問題的最後一行 – FaisalAhmed

回答

1

當你點擊第一個圖片時,只需在圖片上設置Touch Listener。 所以,而不是添加觸摸監聽器內部的看圖功能。 在外面試試吧。

<ImageView 
    style="@style/icon" 
    android:background="@drawable/fear_96" 
    android:id="@+id/abulation" /> 

ImageView imageView = (ImageView)findViewById(R.id.abulation); 
imageView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 

      switch (motionEvent.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        view.getBackground().setColorFilter(0xf0f47521, PorterDuff.Mode.SRC_ATOP); 
        view.invalidate(); 
        break; 

       case MotionEvent.ACTION_CANCEL: 
       case MotionEvent.ACTION_UP: 
        view.getBackground().clearColorFilter(); 
        view.invalidate(); 
        startActivity(view.getId()); 
        break; 
      } 

      return true; 
     } 
    }); 
    } 
+0

我有大約45個imageButton,我並不想要製作那麼多的對象,所以我只是想以這種方式來做。我也是這樣想的,但很高興知道背後的真正原因。無論如何感謝您的答案。 – FaisalAhmed

0

嘗試把

startActivity(view.getId()); 

case MotionEvent.ACTION_CANCEL: 

case MotionEvent.ACTION_UP: 

它爲我工作上面。 希望這可以幫助