Button 在GridView中一键获取多个项目
我有一个gridview,每个单元格中都有一个名为bg_按钮的自定义按钮。我正在尝试创建一个类似于boggle的游戏,但仍然是Android的新手。我在网上搜索了一个多星期,仍然一无所获 问题是,当触屏向下时,我可以毫无问题地获得特定项目,但当我开始沿对角线移动时,我会得到多个我不想要的网格项目。比如, 奥夫特 K T U L T R S V 周国库 我上面的网格,当我触摸T,然后试图移动到O,我得到 T->J->O或T->R->O 我不想要J或R,但我仍然在触摸它。我曾试图改变填充,或垂直和水平间距,但问题仍然是一样的。你能帮我解决这个问题吗,或者至少你能给我一个方法来解决这个问题,或者至少给我一个特定的标签,我可以用谷歌搜索并找到可以帮助我的信息?非常感谢您抽出时间 这是我的触摸事件代码的一部分。我正在保存ArrayList的路径,很抱歉代码太乱。我将在完成硬编码后进行清理:Button 在GridView中一键获取多个项目,button,cell,ontouch,Button,Cell,Ontouch,我有一个gridview,每个单元格中都有一个名为bg_按钮的自定义按钮。我正在尝试创建一个类似于boggle的游戏,但仍然是Android的新手。我在网上搜索了一个多星期,仍然一无所获 问题是,当触屏向下时,我可以毫无问题地获得特定项目,但当我开始沿对角线移动时,我会得到多个我不想要的网格项目。比如, 奥夫特 K T U L T R S V 周国库 我上面的网格,当我触摸T,然后试图移动到O,我得到 T->J->O或T->R->O 我不想要J或R,但我仍然在触摸它。我曾试图改变填充,或垂直和水
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);
}
}
final ArrayList myList=new ArrayList();
gridView=(gridView)this.findViewById(R.id.gridFriends);
MyAdapter gridAdapter=新的MyAdapter(Boggler.this,board_1d);
setAdapter(gridAdapter);
gridView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
最终GridView布局=(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());
而(位置==-1)
position=event.getAction();
视图v2=布局。getChildAt(位置);
myList.add(位置);
Bg_按钮bt=(Bg_按钮)v2.findViewById(R.id.grid_项);
bt.setPressed(真);
Log.d(this.getClass().getName(),String.format(“Over-view.id[%d]”,position));
if(action==MotionEvent.action\u MOVE){
myList.add(位置);
返回true;
}
if(action==MotionEvent.action\u UP){
Log.d(this.getClass().getName(),myList.toString());
int i=0,j=0;
int state=0;
Object[]st=myList.toArray();
用于(对象s:st){
如果(myList.indexOf(s)!=myList.lastIndexOf(s)){
myList.remove(myList.lastIndexOf(s));}
否则{
v2=layout.getChildAt(myList.get(myList.lastIndexOf));
bt=(Bg_按钮)v2.findViewById(R.id.grid_项);
bt.setPressed(假);
name=name+bt.getText();
Log.d(this.getClass().getName(),name);
}
}
这是我正在使用的xml文件button_boggler:
<?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>
和活动(u 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>
我找到了一个适合我的解决方案。我看到过一些关于此类问题的相同问题没有答案,因为我将回答我自己的问题,这样也许它可以帮助那些有此类问题的人。我创建了自定义按钮类,具有不同属性,称为Bg_按钮:
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);
}
}
所以我找到的解决办法是限制按钮的触摸区域,这样他们就不会拦截。我不知道这有多合乎道德,但现在它对我起作用了
@Override
public void getHitRect(Rect outRect) {
outRect.set(getLeft() + 20, getTop() + 20, getRight() - 20, getBottom() - 20);
}
}
这是到目前为止我能想到的最好的答案。我将按钮的触摸区域限制在它的中心,与我们为扩展它所做的交易相反。上面的3行代码做到了这一点。我希望这能有所帮助。我对此感兴趣,我能得到完整的源代码吗?