2013-03-24 43 views
3

我是新來的android開發人員,我嘗試在活動中實施滑動(使用Ontouch)作爲活動並單擊事件。如果我在按鈕上滑動,則不會生成滑動事件。在Android中實現滑動並點擊按鈕

如果我爲按鈕添加滑動,按鈕的onclick事件不會生成。

我該怎麼做才能解決這個問題。

如有任何疑問,請詢問我。

佈局代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/default_2x" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".LearnUpperCaseActivity" > 

    <LinearLayout 
     android:id="@+id/content_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="@string/learn_uppercase" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="11" 
      android:background="@drawable/main_area_blue_2x" > 

      <TextView 
       android:id="@+id/question" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:text="@string/hard_coded_char" /> 
     </RelativeLayout> 

     <TextView 
      android:id="@+id/instruction" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="@string/tab_below" /> 

     <RelativeLayout 
      android:id="@+id/button_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="9" 
      android:padding="3dp" > 

      <LinearLayout 
       android:id="@+id/row1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/button1" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/blue_button" 
        android:onClick="button1Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 

       <Button 
        android:id="@+id/button2" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/brown_button_2x" 
        android:onClick="button2Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 

       <Button 
        android:id="@+id/button3" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/dark_green_2x" 
        android:onClick="button3Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/row2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/row1" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/button4" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/fluora_button_2x" 
        android:onClick="button4Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 

       <Button 
        android:id="@+id/button5" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/green_button_2x" 
        android:onClick="button5Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 

       <Button 
        android:id="@+id/button6" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/maroon_2x" 
        android:onClick="button6Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/row3" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/row2" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/button7" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/yellow_button_2x" 
        android:onClick="button7Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 

       <Button 
        android:id="@+id/button8" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/purple_button_2x" 
        android:onClick="button8Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 

       <Button 
        android:id="@+id/button9" 
        style="@style/UpperCaseButton" 
        android:layout_width="wrap_content" 
        android:layout_height="50dp" 
        android:layout_margin="3dp" 
        android:layout_weight="1" 
        android:background="@drawable/pink_button_2x" 
        android:onClick="button9Pressed" 
        android:text="@string/hard_coded_char" > 
       </Button> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/row4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/row3" 
       android:gravity="center" 
       android:orientation="horizontal" > 

       <Button 
        android:id="@+id/home" 
        style="@style/UpperCaseButton" 
        android:layout_width="150dp" 
        android:layout_height="30dp" 
        android:layout_margin="3dp" 
        android:background="@drawable/blue_long_button_2x" 
        android:onClick="home" 
        android:text="@string/home" > 
       </Button> 
      </LinearLayout> 
     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout> 

看到這個鏈接的圖像。 https://sites.google.com/site/temptemptemp123tempqwe/doubt.jpg?attredirects=0

請幫我解決這個問題。

Regards, Bennet。

+0

發佈相關的代碼。 – adneal 2013-03-24 14:50:16

+0

請在鏈接中查看完整的代碼。非常感謝您提供詳細信息,請爲我提出解決方案。 https://sites.google.com/site/temptemptemp123tempqwe/Sample.rar – Bennet 2013-03-26 00:07:03

回答

0

您只需在按下和解壓按鈕時處理座標的差異。 將此添加到活動的onCreate()

 protected void onCreate(Bundle savedInstanceState) { 
      final float len= getResources().getDisplayMetrics().densityDpi/6; 
      OnTouch Listener controlButtonTouchListener=new View.OnTouchListener() { 
       float initX; 
       float initY; 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 

        switch(event.getAction()){ 
         case MotionEvent.ACTION_DOWN: 
          initX=event.getX(); 
          initY=event.getY(); 
          break; 

         case MotionEvent.ACTION_UP: 
          initX-=event.getX(); //now has difference value 
          initY-=event.getY(); //now has difference value 
          if (initY > len) { 
           ((Button)v).setText("Swipe up"); 
           //Toast.makeText(context,"Swipe up",Toast.LENGTH_SHORT).show(); 

          } 
          else if(initY<-len){ 
           ((Button)v).setText("Swipe Down"); 
           //Toast.makeText(context,"Swipe Down",Toast.LENGTH_SHORT).show(); 
          } 
          else{ 
           if(initY < 0)initY=-initY; 
           if(initX < 0) initX=-initX; 
           if(initX <=len/4 && initY <=len/4){ 
            ((Button)v).setText("Clicked"); 
           } 

          } 
          break; 

        } 
        return true; 
       } 
      }; 
      findViewById(R.id.ClickSwipeButton).setOnTouchListener(controlButtonTouchListener); 


     } 
+0

有沒有聽說過GestureDetector? https://developer.android.com/training/gestures/detector.html – 2016-06-28 16:06:56

+0

手勢檢測更復雜。觸摸'case MotionEvent.ACTION_MOVE:'後需要檢測手指移動,並追蹤路徑以找到手指移動的含義。這是如何完成的,但我從來沒有試過 – 2016-06-28 16:13:23

+0

你的代碼確實檢測到了一個手勢(儘管很粗糙),但一定要特別指出。 'GestureDetector'監聽器專門爲此提供了'onFling'方法。 – 2016-06-28 16:17:25