【发布时间】:2014-02-21 13:15:56
【问题描述】:
在我的应用活动中,我有 8 个按钮、1 个 textView 、2 个相对布局和 1 个线性布局(父级)。
这就是它的样子:
结构如下:
.........
..................
..................
..................
..................
..................
.........
.........
..................
..................
..................
..................
.........
正如您可能从标题中猜到的那样,我正在尝试获取所有 8 个按钮的坐标,并检测我的手指在屏幕上移动时是否在其中任何一个按钮上。
我可以使用下面给出的代码获取按钮的坐标,但它不能正常工作。根据我的代码,一旦按钮被触摸或悬停,它们的背景颜色应更改为红色。但是发生的事情是按钮 5、6、7 和 8 无法识别我的触摸,而 1、2、3、4 确实识别它,但它们也改变了平行按钮的颜色。例如,如果我触摸 B8,什么都不会发生,但如果我触摸 B4,它会随着 B8 改变颜色。
为了更好地解释,我在下面放了一张图片。此图像表示当我触摸 B4 时会发生什么:
为什么会这样?可能是什么问题?请帮我解决。
这是我的代码: Main_Activity.java
public class MainActivity extends Activity {
MyButton b1, b2, b3, b4,b5,b6,b7,b8;
private TextView buttonIndicator;
private LinearLayout touchview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonIndicator = (TextView) findViewById(R.id.textView);
touchview = (LinearLayout) findViewById(R.id.linearlayout);
b1 = (MyButton) findViewById(R.id.button1);
b2 = (MyButton) findViewById(R.id.button2);
b3 = (MyButton) findViewById(R.id.button3);
b4 = (MyButton) findViewById(R.id.button4);
b5 = (MyButton) findViewById(R.id.button5);
b6 = (MyButton) findViewById(R.id.button6);
b7 = (MyButton) findViewById(R.id.button7);
b8 = (MyButton) findViewById(R.id.button8);
RelativeLayout touch1 = (RelativeLayout) findViewById(R.id.touchview1);
RelativeLayout touch2 = (RelativeLayout) findViewById(R.id.touchview2);
touch1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false; // To pass touch event to linear layout parent
}
});
touch2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false; // To pass touch event to linear layout parent
}
});
}
@Override
protected void onResume() {
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();
if (isPointWithin(x, y, b1.getLeft(), b1.getRight(), b1.getTop(),b1.getBottom())) {
b1.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 1");
}
if (isPointWithin(x, y, b2.getLeft(), b2.getRight(), b2.getTop(),b2.getBottom())) {
b2.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 2");
}
if (isPointWithin(x, y, b3.getLeft(), b3.getRight(), b3.getTop(),b3.getBottom())) {
b3.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 3");
}
if (isPointWithin(x, y, b4.getLeft(), b4.getRight(), b4.getTop(),b4.getBottom())) {
b4.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 4");
}
if (isPointWithin(x, y, b5.getLeft(), b5.getRight(), b5.getTop(),b5.getBottom())) {
b5.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 5");
}
if (isPointWithin(x, y, b6.getLeft(), b6.getRight(), b6.getTop(),b6.getBottom())) {
b6.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 6");
}
if (isPointWithin(x, y, b7.getLeft(), b7.getRight(), b7.getTop(),b7.getBottom())) {
b7.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 7");
}
if (isPointWithin(x, y, b8.getLeft(), b8.getRight(), b8.getTop(),b8.getBottom())) {
b8.setBackgroundColor(Color.RED);
buttonIndicator.setText("Button 8");
}
return true;
}
});
}
//The boolean below determines the exact position of the finger on the screen
static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:background="#ffffff" >
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/touchview1"
android:layout_weight="0.5">
<com.example.coordinates.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:layout_marginBottom="10dp"
android:textColor="#000000" />
<com.example.coordinates.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:layout_marginBottom="10dp"
android:text="B2"
android:textColor="#000000" />
<com.example.coordinates.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:layout_marginBottom="10dp"
android:textColor="#000000" />
<com.example.coordinates.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Touch a button"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/touchview2"
android:layout_weight="0.5">
<com.example.coordinates.MyButton
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="B5"
android:layout_marginBottom="10dp"
android:textColor="#000000" />
<com.example.coordinates.MyButton
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button5"
android:layout_marginBottom="10dp"
android:text="B6"
android:textColor="#000000" />
<com.example.coordinates.MyButton
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button6"
android:text="B7"
android:layout_marginBottom="10dp"
android:textColor="#000000" />
<com.example.coordinates.MyButton
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button7"
android:text="B8"
android:textColor="#000000" />
</RelativeLayout>
</LinearLayout>
MyButton.java
public class MyButton extends Button {
//I am using cutom Button to avoid ontouch for buttons and return false
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;
}
}
我已尽力解释我的问题。请帮帮我!
非常非常感谢大家。
编辑:有人建议我使用View.getLocationOnScreen()。谁能解释一下如何借助这种方法获得 (x1 and x2) & (y1 and y2) 坐标?我认为下图解释得更好!
【问题讨论】:
-
为什么不直接在按钮上尝试 onCLickListener 或 onTouchlistener 并在事件执行时更改它们的背景?如果您的要求不止于此,请见谅。
-
@Atul,是的,我的要求远不止于此。没问题。请帮我找到解决办法!
-
看看这是否有帮助。 stackoverflow.com/questions/3619693/…
-
请检查编辑!谢谢!
-
我看到您尝试过 getLocationOnScreen,但您也尝试过 getLocationOnWindow。这些是这里的 Romain Guy 建议的两个 - stackoverflow.com/questions/2224844/…。另外,我相信要获得上图的所有 4 个坐标,您必须将提供视图 x1 和 y1 的方法与提供视图大小的方法结合起来。
标签: android coordinates android-button