从一个视图移动到另一个视图时,无需将手指从屏幕上抬起,java动作将向上
这是我关于同一主题的第三个问题。我正在为一个客户制作一个手机拨号器应用程序,用于盲人,我想从一个按钮移动到另一个按钮,而不需要手指抬起屏幕,当手指停在按钮上时播放数字的声音,如果他想让按钮被键入,他就离开了手指,依此类推,我在前面的问题中找到了这段代码,它可以工作只有手指在移动,但如果我抬起手指,我就什么也做不到,所以我的问题是,当我从按钮上抬起手指时,如何采取行动,以及如何单独处理每个按钮强>
package com.example.touch;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.MotionEvent;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.RelativeLayout;
import 安卓.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 = {
安卓.R.attr.state_pressed,
安卓.R.attr.state_focused
| 安卓.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);
}
}
共 (0) 个答案