2014-02-20 5 views
2

Я делаю приложение для фортепиано для Android. В качестве образца (теста) у меня есть 4 кнопки в моей деятельности. Родитель является Relative Layout, и у меня также есть несколько текстовых просмотров, которые отображают координаты экрана касания пальца. Существует еще один TextView («Поступил Button»), который определяет, если ваш палец на кнопку или нет
Это то, что он выглядит следующим образом:
Пропустить координаты экрана от кнопки до родителя

Я достиг этого, используя код, указанный ниже.

Java

public class MainActivity extends Activity { 

Button b1, b2, b3, b4; 

int b1x1, b1x2, b1y1, b1y2; 

private TextView xcordview; 
private TextView ycordview; 
private TextView buttonIndicator; 
private RelativeLayout touchview; 
private static int defaultStates[]; 
private Button mLastButton; 
private final static int[] STATE_PRESSED = { 
     android.R.attr.state_pressed, 
     android.R.attr.state_focused 
       | android.R.attr.state_enabled }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    xcordview = (TextView) findViewById(R.id.textView4); 
    ycordview = (TextView) findViewById(R.id.textView3); 
    buttonIndicator = (TextView) findViewById(R.id.button_indicator); 
    touchview = (RelativeLayout) findViewById(R.id.relativelayout); 

    b1 = (Button) findViewById(R.id.button1); 
    b2 = (Button) findViewById(R.id.button2); 
    b3 = (Button) findViewById(R.id.button3); 
    b4 = (Button) findViewById(R.id.button4); 
    defaultStates = b1.getBackground().getState(); 

} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 

    touchview.setOnTouchListener(new View.OnTouchListener() { 

     private boolean isInside = false; 

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

      int x = (int) event.getX(); 
      int y = (int) event.getY(); 

      xcordview.setText(String.valueOf(x)); 
      ycordview.setText(String.valueOf(y)); 

      for (int i = 0; i < touchview.getChildCount(); i++) { 
       View current = touchview.getChildAt(i); 
       if (current instanceof Button) { 
        Button b = (Button) current; 

        if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(), 
          b.getBottom())) { 
         b.getBackground().setState(defaultStates); 
        } 

        if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(), 
          b.getBottom())) { 
         b.getBackground().setState(STATE_PRESSED); 
         if (b != mLastButton) { 
          mLastButton = b; 
          buttonIndicator.setText(mLastButton.getText()); 
         } 
        } 

       } 
      } 
      return true; 
     } 

    }); 

} 

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
} 

static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) { 
    return (x <= x2 && x >= x1 && y <= y2 && y >= y1); 
} 
} 

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/relativelayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff" > 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:text="Y Cord : " 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:text="X Cord : " 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" 
    android:layout_toRightOf="@+id/textView" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#000000" /> 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView" 
    android:layout_marginBottom="10dp" 
    android:layout_toRightOf="@+id/textView" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#000000" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:text="B1" 
    android:textColor="#000000" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/button1" 
    android:text="B2" 
    android:textColor="#000000" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/button2" 
    android:text="B3" 
    android:textColor="#000000" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/button3" 
    android:text="B4" 
    android:textColor="#000000" /> 

<TextView 
    android:id="@+id/button_indicator" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@+id/textView4" 
    android:layout_marginRight="33dp" 
    android:text="No one" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button_indicator" 
    android:layout_alignBottom="@+id/button_indicator" 
    android:layout_marginRight="29dp" 
    android:layout_toLeftOf="@+id/button_indicator" 
    android:text="Entered: " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

Так что этот код, указанный выше, работает хорошо, как он распознает, когда я красть мой палец из белого пространства (Relative макет) по любой кнопке , Но когда я сажусь с одной кнопки на другую, это не работает. Он не получает никаких координат, и не понимает, на какой кнопке мой палец закончился. В приведенном ниже изображении лучше всего объясняется, что происходит, когда я перебираю кнопку с другой кнопкой.

Так как я могу получить координаты во время считывания с одной кнопки на другую?

+0

когда вы проводите, получить левый и получить верх связан с макетом так, для относительного layout 0, 0 - верхний левый угол вашего макета, но когда вы прокручиваете с кнопки 0, 0 находится вверху слева от вашего виджета, см. мой ответ по следующей ссылке, m aybe может помочь вам, http://stackoverflow.com/questions/20988635/android-lock-screen-app-button-hover-and-sequence/20988839#20988839 –

+0

Спасибо! но он не будет поддерживать

+0

просто скопируйте и прошлый код в новый проект, если вы удаляете анимацию, тогда ее работала для всех версий, –

ответ

3

MainActivity.java

package com.example.touch; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.MotionEvent; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.RelativeLayout; 
    import android.widget.TextView; 

    public class MainActivity extends Activity { 

     MyButton b1, b2, b3, b4; 

     int b1x1, b1x2, b1y1, b1y2; 

     private TextView xcordview; 
     private TextView ycordview; 
     private TextView buttonIndicator; 
     private RelativeLayout touchview; 
     private static int defaultStates[]; 
     private Button mLastButton; 
     private final static int[] STATE_PRESSED = { 
       android.R.attr.state_pressed, 
       android.R.attr.state_focused 
         | android.R.attr.state_enabled }; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      xcordview = (TextView) findViewById(R.id.textView4); 
      ycordview = (TextView) findViewById(R.id.textView3); 
      buttonIndicator = (TextView) findViewById(R.id.button_indicator); 
      touchview = (RelativeLayout) findViewById(R.id.relativelayout); 

      b1 = (MyButton) findViewById(R.id.button1); 
      b2 = (MyButton) findViewById(R.id.button2); 
      b3 = (MyButton) findViewById(R.id.button3); 
      b4 = (MyButton) findViewById(R.id.button4); 
      defaultStates = b1.getBackground().getState(); 

     } 

     @Override 
     protected void onResume() { 
      // TODO Auto-generated method stub 
      super.onResume(); 

      touchview.setOnTouchListener(new View.OnTouchListener() { 

       private boolean isInside = false; 

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

        int x = (int) event.getX(); 
        int y = (int) event.getY(); 

        xcordview.setText(String.valueOf(x)); 
        ycordview.setText(String.valueOf(y)); 

        for (int i = 0; i < touchview.getChildCount(); i++) { 
         View current = touchview.getChildAt(i); 
         if (current instanceof Button) { 
          Button b = (Button) current; 

          if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(), 
            b.getBottom())) { 
           b.getBackground().setState(defaultStates); 
           b.getBackground().setAlpha(255); 
          } 

          if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(), 
            b.getBottom())) { 
           b.getBackground().setState(STATE_PRESSED); 
           b.getBackground().setAlpha(150); 
           b.performClick(); 

           if (b != mLastButton) { 
            mLastButton = b; 
            buttonIndicator.setText(mLastButton.getText()); 
           } 
          } 

         } 
        } 
        return true; 
       } 

      }); 

     } 

     @Override 
     public void onWindowFocusChanged(boolean hasFocus) { 
      // TODO Auto-generated method stub 
      super.onWindowFocusChanged(hasFocus); 
     } 

     static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) { 
      return (x <= x2 && x >= x1 && y <= y2 && y >= y1); 
     } 
    } 

MyButton.java

package com.example.touch; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.Button; 

public class MyButton extends Button { 

    public MyButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    public MyButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    public MyButton(Context context) { 
     super(context); 
     // // TODO Auto-generated constructor stub 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     // return super.onTouchEvent(event); 
     return false; 
    } 

} 

Планировка:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:text="Y Cord : " 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:text="X Cord : " 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_toRightOf="@+id/textView" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView" 
     android:layout_marginBottom="10dp" 
     android:layout_toRightOf="@+id/textView" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#000000" /> 

    <com.example.touch.MyButton 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="B1" 
     android:textColor="#000000" /> 

    <com.example.touch.MyButton 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button1" 
     android:text="B2" 
     android:textColor="#000000" /> 

    <com.example.touch.MyButton 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button2" 
     android:text="B3" 
     android:textColor="#000000" /> 

    <com.example.touch.MyButton 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button3" 
     android:text="B4" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/button_indicator" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/textView4" 
     android:layout_marginRight="33dp" 
     android:text="No one" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button_indicator" 
     android:layout_alignBottom="@+id/button_indicator" 
     android:layout_marginRight="29dp" 
     android:layout_toLeftOf="@+id/button_indicator" 
     android:text="Entered: " 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 
+0

Хорошая работа. Я разработал код расширения из вашего кода. В моем коде я найду эти кнопки в раунде, используя FrameLayout. Тем не менее, я не могу поймать текст кнопки, когда я сажусь. Не могли бы вы помочь мне, пожалуйста, по адресу http://stackoverflow.com/questions/39489343/how-to-detect-a-button-in-framelayout-when-swipe-buttons – Jame

Смежные вопросы