2015-05-27 49 views
-1

我試圖讓控制按鈕在屏幕被點擊時彈出開/關屏幕。他們將覆蓋視頻。我遇到的問題是如何爲已膨脹的佈局創建觸摸事件。
例程如下:
1)將初始內容設置爲視頻;
2)在視頻頂部打開包含控件的佈局(XML);
3)(需要做)爲用戶點擊屏幕時隱藏/顯示按鈕的新膨脹佈局創建onTouch事件。如何將onTouch設置爲通過通貨膨脹添加的佈局(XML)?

包含的按鈕佈局顯示/隱藏:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/dpad_grid" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:columnCount="3" 
       android:orientation="horizontal" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true"> 

     <Button 
      android:id="@+id/tiltUp" 
      android:background="@drawable/arrow_up" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_row="0" 
      android:layout_column="1"/> 

     <Button 
      android:id="@+id/panRight" 
      android:background="@drawable/arrow_right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_row="1" 
      android:layout_column="2"/> 

     <Button 
      android:id="@+id/tiltDown" 
      android:background="@drawable/arrow_down" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="8dp" 
      android:layout_row="2" 
      android:layout_column="1"/> 

     <Button 
      android:id="@+id/panLeft" 
      android:background="@drawable/arrow_left" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:layout_row="1" 
      android:layout_column="0"/> 

    </GridLayout> 

    <Button 
     android:id="@+id/zoomIn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:background="@drawable/zoom_in" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true"/> 

    <Button 
     android:id="@+id/zoomOut" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/zoom_out" 
     android:layout_marginRight="0dp" 
     android:layout_alignParentBottom="true" 
     android:layout_toStartOf="@+id/zoomIn"/> 

    <Button 
     android:id="@+id/irisDarker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="8dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:background="@drawable/iris_darker"/> 

    <Button 
     android:id="@+id/irisBrighter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_toEndOf="@+id/irisDarker" 
     android:background="@drawable/iris_brighter"/> 

    <Button 
     android:id="@+id/focusFar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/dpad_grid" 
     android:layout_marginRight="20dp" 
     android:layout_alignParentStart="true" 
     android:background="@drawable/focus_far"/> 

    <Button 
     android:id="@+id/focusNear" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/focus_near" 
     android:layout_alignTop="@+id/focusFar" 
     android:layout_alignStart="@+id/irisBrighter"/> 

    <Button 
     android:id="@+id/snapshot" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/camera" 
     android:layout_alignEnd="@+id/focusFar" 
     android:layout_alignParentStart="true"/> 

    <Button 
     android:id="@+id/settings" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/gear"/> 

</RelativeLayout> 

代碼添加視頻和充氣上述佈局:

@Override 
    public void onResume() { 
     super.onResume();  

     RelativeLayout.LayoutParams relativeLayoutParams = 
       new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 

     mv = new MjpegView(this); 
     setContentView(mv); 

     LayoutInflater inflater = getLayoutInflater(); 
     getWindow().addContentView(inflater.inflate(R.layout.screen2, null), relativeLayoutParams); 

感謝您的幫助!

編輯:這是我目前的工作。當我觸摸屏幕時,爲什麼我的代碼不能進入觸摸事件?

LayoutInflater inflater = getLayoutInflater(); 
     control_buttons = inflater.inflate(R.layout.screen2, null); 

     control_buttons.setOnTouchListener(new View.OnTouchListener() { 
      boolean gone = true; 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

       System.out.println("In OnTouch"); 

       if ((event.getAction() == MotionEvent.ACTION_DOWN) && (gone == true)){ 

        getWindow().addContentView(control_buttons, relativeLayoutParams); 

        gone = false; 
        return true; 
       } 

       else if ((event.getAction() == MotionEvent.ACTION_DOWN) && (gone == false)) { 
        v.setVisibility(View.GONE); 
        gone = true; 
        return true; 
       } 
       return false; 
      } 
     }); 
+0

touch events?你的意思是MotionEvents(http://developer.android.com/reference/android/view/MotionEvent.html)? – pskink

+0

我不認爲我需要所有的動作像滑動和什麼不。我只想讓按鈕在用戶點擊屏幕時顯示/隱藏。 – shoota

+0

你讀過什麼inflater.inflate()返回? – pskink

回答

0

這可能對您更好。這隻有在您的RelativeLayout中的子項是包含按鈕的按鈕或GridLayouts時纔有效。

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId); 
    relativeLayout.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if(event.getAction() == MotionEvent.ACTION_DOWN) { 
       for(int i = 0; i < relativeLayout.getChildCount(); i++) { 
        if(relativeLayout.getChildAt(i) instanceof Button) { 
         Button temp = (Button) relativeLayout.getChildAt(i); 
         if(temp.getVisibility() == View.INVISIBLE) { 
          temp.setVisibility(View.VISIBLE); 
         } else { 
          temp.setVisibility(View.INVISIBLE); 
         } 
        } else if(relativeLayout.getChildAt(i) instanceof GridLayout) { 
         GridLayout temp = (GridLayout) relativeLayout.getChildAt(i); 
         for(int g = 0; g < temp.getChildCount(); g++) { 
          Button tempButton = (Button) temp.getChildAt(g); 
          if(tempButton.getVisibility() == View.INVISIBLE) { 
           tempButton.setVisibility(View.VISIBLE); 
          } else { 
           temp.setVisibility(View.INVISIBLE); 
          } 
         } 
        } 
       } 
      } 

      return false; 
     } 
    }); 
+0

謝謝,檢查我的編輯上面,看看是否改變任何東西。 – shoota

+0

我只是試過你的代碼,它拋出:java.lang.NullPointerException:試圖調用虛擬方法'無效android.widget.GridLayout.setOnTouchListener – shoota

+0

我不知道這是否會工作。我爲onInterceptTouchEvent()添加了覆蓋...我在我自己的應用程序中實現這種類型的事情的方式是我創建了一個擴展GridLayout的自定義ViewGroup。在我的CustomGridLayout.java文件中,我能夠輕鬆覆蓋onInterceptTouchEvent()以及onTouchEvent()。這樣我就可以確保所有觸摸事件都是先由我的自定義ViewGroup處理的。我不知道是否可以擴展GridLayout來完成這項工作。 –