0

我有一个网格视图,每个单元格中都有一个名为 bg_button 的自定义按钮。我正在尝试创建一个类似于 boggle 的游戏,但仍然是 Android 中的新手。我在互联网上搜索这个问题一个多星期了,仍然一无所获。

问题是,当 touch_down 时我可以毫无问题地获得特定项目,但是当我开始对角线移动时,我得到多个我不想要的网格项目。例如;

AOFT

克图尔

TRSV

JOKU

我上面的网格,当我触摸 T 然后试图移动到 O 时,我得到了;

T -> J -> O 或 T -> R -> O

我不想要 J 或 R,但我仍然在触摸它。我试图更改为填充或垂直和水平间距,但问题仍然存在。你能帮我解决这个问题吗,或者至少你能给我一个方法来做这个,或者至少一个特定的标签,我可以用谷歌搜索并找到可以帮助我的信息?非常感谢您的参与。

这是我的触摸事件代码的一部分。我正在保存 ArrayList 的路径,我很抱歉代码混乱。完成硬编码后,我将进行清理:

 final ArrayList<Integer> myList = new ArrayList<Integer>();
        gridView = (GridView) this.findViewById(R.id.gridFriends);
        MyAdapter gridAdapter = new MyAdapter(Boggler.this,board_1d);
        gridView.setAdapter(gridAdapter);
        gridView.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event)  {


                    final GridView layout = (GridView)v;
                    int action = event.getActionMasked();
                    float currentXPosition = event.getX();
                    float currentYPosition = event.getY();
                    int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
                    // position = layout.pointToPosition( (int)event.getX(), (int)event.getY() );

                     while(position == -1)
                         position=event.getAction();



                     View v2 =layout.getChildAt(position);



                         myList.add(position);
                        Bg_button bt = (Bg_button) v2.findViewById(R.id.grid_item);

                       bt.setPressed(true);




                         Log.d(this.getClass().getName(), String.format("Over view.id[%d]", position));

                         if (action == MotionEvent.ACTION_MOVE){



                             myList.add(position);


                            return true;

                         }


                        if (action == MotionEvent.ACTION_UP) {

                            Log.d(this.getClass().getName(), myList.toString());
                            int i=0,j=0;
                            int state = 0;
                            Object[] st = myList.toArray();
                            for (Object s : st) {
                                if (myList.indexOf(s) != myList.lastIndexOf(s)) {
                                    myList.remove(myList.lastIndexOf(s));}

                                    else {  
                                     v2 =layout.getChildAt(myList.get(myList.lastIndexOf(s)));

                                     bt = (Bg_button) v2.findViewById(R.id.grid_item);
                                     bt.setPressed(false);
                                       name = name + bt.getText();



                                     Log.d(this.getClass().getName(), name);

                                   }
                            }

这是我正在使用 button_boggler 的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.proje_test.bg_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<com.example.proje_test.Bg_button
    android:id="@+id/grid_item"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:clickable="false"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:background="@drawable/color_bg_selector"
    android:textSize="50dp" 

/>

</LinearLayout>

和activity_boggler:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<GridView
    android:id="@+id/gridFriends"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:clipChildren="true"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="4"
    android:scrollbars="none"
    android:stretchMode="columnWidth" >

</GridView>
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/feedback"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

</LinearLayout>
4

1 回答 1

0

I have found a solution that is working for me. I have seen a couple of same questions about this kind of issues without answers because of that I will answer my own question so maybe it can help those who have this kind of issues. I made custom button class with different attrs called Bg_button:

public class Bg_button extends Button {

private static final int[] STATE_C = {R.attr.state_chosen};
private static final int[] STATE_R = {R.attr.state_right};
private static final int[] STATE_W = {R.attr.state_wrong};
public boolean mIschosen = false;
public boolean mIsright = false;
public boolean mIswrong = false;

public Bg_button(Context context, AttributeSet attrs) {
    super(context, attrs);
}

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
       final int[] drawableState = super.onCreateDrawableState(extraSpace + 3);
       if (mIschosen) {
           mergeDrawableStates(drawableState, STATE_C);
               }
       if (mIsright) {
           mergeDrawableStates(drawableState, STATE_R);
               }
       if (mIswrong) {
           mergeDrawableStates(drawableState, STATE_W);
               }
       return drawableState;
          }

       public void setchosen(boolean ischosen) {mIschosen = ischosen;
           refreshDrawableState();}
       public void setright(boolean isright) {mIsright = isright;
           refreshDrawableState();}
       public boolean setwrong(boolean iswrong) {mIswrong = iswrong;
           refreshDrawableState();
       return true;}

       @Override
       public void getHitRect(Rect outRect) {
     outRect.set(getLeft() + 20, getTop() + 20, getRight() - 20, getBottom() - 20);
                 }
               }

So the solution I have found is limiting the touch area of the button so they do not intercept. I don't know how ethical this is but it is working for me now.

@Override
    public void getHitRect(Rect outRect) {
        outRect.set(getLeft() + 20, getTop() + 20, getRight() - 20, getBottom() - 20);
    }
}

This is the best answer I can come up with so far. I limited the touch area of button to its center with reversing the transaction that we do for expanding it. And the 3 lines code above did the trick. I hope this helps.

于 2014-06-18T16:48:01.710 回答