【发布时间】:2016-11-02 10:52:12
【问题描述】:
我正在开发一个钢琴应用程序,目前我正在做一些事情,以使我的按钮在我用手指滑过时可以正常工作。
这里有更全面的解释:
Here is what happens when i move my finger outside of this button, without releasing my finger
我的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/imageButtonSelector"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/new_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
new_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/redcolor"
android:state_pressed="true" />
<item android:drawable="@drawable/orange"
android:state_focused="true" />
<item android:drawable="@drawable/greencolor" />
</selector>
MainActivity.java
package com.example.android.focus;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button imageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (Button) findViewById(R.id.imageButtonSelector);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,
"ImageButton (selector) is clicked!",
Toast.LENGTH_SHORT).show();
}
});
}
}
我需要做什么才能使我的应用程序按预期工作? 任何帮助将不胜感激!
【问题讨论】:
-
不,不。不是 onClickListener,您需要一个 onTouchListener,因为您需要找出手指何时向下、向上(在向下之后)以及手指何时移动。此外,由于它是一架钢琴,而且人们很少一次只弹一个琴键,因此您需要了解 Android 是如何处理多点触控事件的。