我有一个带有 15 个按钮的 RelativeLayout,我正在开发一个新项目并使用 OnTouchListener,我希望我的应用执行如下操作:当用户触摸例如按钮 1,Mp1 将开始播放,直到用户抬起手指或将其移至按钮 2,然后按钮 2 上的 mp2 应开始播放,依此类推。
但这是发生了什么,用户触摸屏幕和抬起的部分工作正常,但是如果用户 move 他的手指(而不是抬起),如果按下按钮 1,它仍将处于按下状态( Action 向下),直到用户抬起他的手指。
是这样的:
我的问题:
当手指离开按钮边框以停止按钮并打开按下的按钮(手指触摸的位置)时,我需要添加什么? 我的代码:
sound1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
pressed1 = true;
mp1 = MediaPlayer.create(MainClass.this, R.raw.item1);
mp1.start();
sound1.setBackgroundResource(R.drawable.pad_pressed);
if (looping == true) {
mp1.setLooping(true);
} else if (looping == false) {
mp1.setLooping(false);
}
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
pressed1 = false;
mp1.stop();
mp1.reset();
mp1.release();
sound1.setBackgroundResource(R.drawable.pad_normal);
}
return true;
}
});
最佳答案
你必须关注 RelativeLayout。
MyActivity.java
public class MyActivity extends Activity {
private RelativeLayout mainLayout;
private TextView myTag;
private TextView xcordview;
private TextView ycordview;
private MediaPlayer mp1;
private String currentTag;
private boolean areaDetected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
myTag = (TextView) findViewById(R.id.tag);
xcordview = (TextView) findViewById(R.id.x_view);
ycordview = (TextView) findViewById(R.id.y_view);
currentTag = "";
initEvent();
}
private void initEvent() {
mainLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
xcordview.setText("X : " + String.valueOf(x));
ycordview.setText("Y : " + String.valueOf(y));
areaDetected = false;
for (int i = 0; i < mainLayout.getChildCount(); i++) {
View currentButton = mainLayout.getChildAt(i);
if (currentButton instanceof Button) {
Button b = (Button) currentButton;
String tag = b.getTag().toString();
if (!pointInside(x, y, b.getLeft(), b.getRight(), b.getTop(), b.getBottom())) {
b.setText("");
// b.setBackgroundResource(getResources().getColor(android.R.color.black));
} else {
areaDetected = true;
if (!currentTag.equals(tag)) {
currentTag = tag;
stopPlaying();
b.setText(tag);
mp1 = getMediaPlayer(tag);
mp1.start();
// b.setBackgroundResource(getResources().getColor(android.R.color.white));
}
}
}
}
if (!areaDetected) {
currentTag = "";
stopPlaying();
}
myTag.setText("Current tag : " + currentTag);
return true;
}
});
}
private void stopPlaying() {
if (mp1 != null) {
mp1.stop();
mp1.release();
mp1 = null;
}
}
private MediaPlayer getMediaPlayer(String tag) {
if (tag.equals("b1")) {
return MediaPlayer.create(MyActivity.this, R.raw.coffee_and_snow);
}
return MediaPlayer.create(MyActivity.this, R.raw.coffee_and_snow2);
}
static boolean pointInside(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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<com.example.antoine.touchtest.MyButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="b1" />
<com.example.antoine.touchtest.MyButton
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/b1"
android:tag="b2" />
<com.example.antoine.touchtest.MyButton
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/b2"
android:tag="b3" />
<com.example.antoine.touchtest.MyButton
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/b3"
android:tag="b4" />
<TextView
android:id="@+id/x_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/y_view"
android:text="X : " />
<TextView
android:id="@+id/tag"
android:layout_above="@+id/x_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current tag : " />
<TextView
android:id="@+id/y_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Y : " />
</RelativeLayout>
最后是我的自定义按钮
我的按钮
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;
}
}
非常感谢这篇文章 ==> Get button coordinates and detect if finger is over them - Android
关于android - 当手指 move Android 时,On Touch Listener 不会释放按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24956298/
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有可变数量的表格和可变数量的行,我想让它们一个接一个地显示,但如果表格不适合当前页面,请将其放在下一页,然后继续。我已将表格放入事务中,以便我可以回滚然后打印它(如果高度适合当前页面),但我如何获得表格高度?我现在有这段代码pdf.transactiondopdf.table@data,:font_size=>12,:border_style=>:grid,:horizontal_padding=>10,:vertical_padding=>3,:border_width=>2,:position=>:left,:row_colors=>["FFFFFF","DDDDDD"]pdf.
基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub
我在ruby表单中有一个提交按钮f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id"我想在不使用任何javascript的情况下通过ruby禁用此按钮 最佳答案 添加disabled:true选项。f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id",disabled:true 关于ruby-on-rails-如何在Rails中添加禁用的提交按钮,我们在St
我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac
正如标题所暗示的那样,我只是在寻找一种无需单击即可按下Shoes中的按钮的方法。我已经搜索了论坛,但不幸的是找不到任何东西。谢谢 最佳答案 这在Windows下的红色鞋子中不起作用,因为Windows控件窃取了所有事件。不过,我设法在window下穿着绿鞋工作。举个例子['green_shoes'].each(&method(:require))Shoes.app{e=edit_linebutton("Clickme!"){alert("Youclickedme.")}keypress{|k|alert("YoupressedEnt