2011-06-14 54 views
2

我正在製作一個android應用程序,其中放置了10張圖像並將ontouch功能添加到該應用程序中,但每當我到達第10張圖像時,它仍在工作並返回到第1張圖像。 我想停止第10張圖片或最後一張圖片上的ontouch功能。如何控制android中的ontouch功能?

任何人都知道這個功能?

這裏是Java代碼:

public class Activity1 extends Activity implements OnTouchListener{ 

float downXValue; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Set main.XML as the layout for this Activity 
    setContentView(R.layout.main); 

    // Add these two lines 
    LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main); 
    layMain.setOnTouchListener((OnTouchListener) this); 

    // Add a few countries to the spinner 
    Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country); 
    ArrayAdapter countryArrayAdapter = new ArrayAdapter(this, 
       android.R.layout.simple_spinner_dropdown_item, 
       new String[] { "Canada", "USA" }); 
    spinnerCountries.setAdapter(countryArrayAdapter); 

} 

public boolean onTouch(View arg0, MotionEvent arg1) { 

    // Get the action that was done on this touch event 
    switch (arg1.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
     { 
      // store the X value when the user's finger was pressed down 
      downXValue = arg1.getX(); 
      break; 
     } 

     case MotionEvent.ACTION_UP: 
     { 
      // Get the X value when the user released his/her finger 
      float currentX = arg1.getX();    

      // going backwards: pushing stuff to the right 
      if (downXValue < currentX) 
      { 
       // Get a reference to the ViewFlipper 
       ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
       // Set the animation 
        vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); 
        // Flip! 
        vf.showPrevious(); 
      } 

      // going forwards: pushing stuff to the left 
      if (downXValue > currentX) 
      { 
       // Get a reference to the ViewFlipper 
       ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
       // Set the animation 
       vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); 
        // Flip! 
       vf.showNext(); 
      } 
      break; 
     } 
    } 

    // if you return false, these actions will not be recorded 
    return true; 
} 

}

這裏是XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/layout_main" 
    > 

    <ViewFlipper android:id="@+id/details" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 




     <LinearLayout 
       android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

      <ImageView android:id="@+id/ImageView01" android:background="@drawable/eea" 
      android:layout_x="1dip" android:layout_y="1dip" 
      android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView> 
     </LinearLayout> 

     <LinearLayout 
       android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

      <ImageView android:id="@+id/ImageView01" android:background="@drawable/ef" 
      android:layout_x="1dip" android:layout_y="1dip" 
      android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView> 
     </LinearLayout> 

     <LinearLayout 
       android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

      <ImageView android:id="@+id/ImageView01" android:background="@drawable/eg" 
      android:layout_x="1dip" android:layout_y="1dip" 
      android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView> 
     </LinearLayout> 

     <LinearLayout 
       android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

      <ImageView android:id="@+id/ImageView01" android:background="@drawable/eh" 
      android:layout_x="1dip" android:layout_y="1dip" 
      android:layout_height="fill_parent" android:layout_width="fill_parent"></ImageView> 
     </LinearLayout> 


    </ViewFlipper> 

</LinearLayout> 

我不得不使用瀏覽腳蹼在我的代碼,然後將其添加圖像。

+0

哪裏形象的事ü在說的..? – ngesh 2011-06-14 13:38:56

+0

對不起,我發佈錯誤的XML文件,現在我張貼rightone,你可以看到它。 – 2011-06-14 13:47:40

回答

2

月10圖像的onTouch()方法setFocussable("false");setFocussableInTouchMode("false")所有其他圖像。只有這樣,我想..

+0

我可以告訴你我的代碼,然後你會告訴我我在哪裏放這個。代碼是在問題 – 2011-06-14 13:32:37

+0

我可以告訴你我的代碼,然後你會告訴我在哪裏我要把這個 – 2011-06-14 13:33:41

+0

告訴我然後... – ngesh 2011-06-14 13:34:11