如何从onClick更改为onTouch方法?

时间:2019-01-27 20:57:07

标签: java android

我正在创建音乐应用程序,需要将onClick更改为onTouch方法,这样我才能在按下按钮的那一刻(不仅是在释放它时)收听声音。我无法弄清楚我需要添加和更改的内容。

MainActivity.java:

public class MainActivity extends AppCompatActivity  {

    private SoundPool mSoundPool;
    private int btn2;
    private int btn5;
    private int btn13;
    private int btn14;
    private int btn15;
    private int btn16;
    private int btn17;
    private int btn18;
    private int btn19;
    private int btn20;
    private int btn21;
    private int btn22;



    private float LEFT_VOL = 1.0f;
    private float RIGHT_VOL = 1.0f;
    private int PRIORITY = 1;
    private int LOOP = 0;
    private float RATE = 1.0f;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);




        mSoundPool = new SoundPool(24, AudioManager.STREAM_MUSIC,0);
        btn2 = mSoundPool.load(getApplicationContext(),R.raw.r,1);
        btn5 = mSoundPool.load(getApplicationContext(),R.raw.g,1);
        btn13 = mSoundPool.load(getApplicationContext(),R.raw.k,1);
        btn14 = mSoundPool.load(getApplicationContext(),R.raw.hat,1);
        btn15 = mSoundPool.load(getApplicationContext(),R.raw.s,1);
        btn16 = mSoundPool.load(getApplicationContext(),R.raw.a,1);
        btn17 = mSoundPool.load(getApplicationContext(),R.raw.d,1);
        btn18 = mSoundPool.load(getApplicationContext(),R.raw.c,1);
        btn19 = mSoundPool.load(getApplicationContext(),R.raw.f,1);
        btn20 = mSoundPool.load(getApplicationContext(),R.raw.e,1);
        btn21 = mSoundPool.load(getApplicationContext(),R.raw.h,1);
        btn22 = mSoundPool.load(getApplicationContext(),R.raw.b,1);


    }


    public void playC(View v){
        mSoundPool.play(btn2,1,1,0,0,1);
    }

    public void playD(View v){
        mSoundPool.play(btn5,1,1,0,0,1);
    }

    public void playE(View v){
        mSoundPool.play(btn13,1,1,0,0,1);
    }

    public void playF(View v){
        mSoundPool.play(btn14,1,1,0,0,1);
    }

    public void playG(View v){
        mSoundPool.play(btn15,1,1,0,0,1);
    }

    public void playA(View v){
        mSoundPool.play(btn16,1,1,0,0,1);
    }

    public void playB(View v){
        mSoundPool.play(btn17,1,1,0,0,1);
    }

    public void playCC(View v){
        mSoundPool.play(btn18,1,1,0,0,1);
    }

    public void playCS(View v){
        mSoundPool.play(btn19,1,1,0,0,1);
    }

    public void playDS(View v){
        mSoundPool.play(btn20,1,1,0,0,1);
    }

    public void playFS(View v){
        mSoundPool.play(btn21,1,1,0,0,1);
    }

    public void playGS(View v){
        mSoundPool.play(btn22,1,1,0,0,1);
    }

Activity_Main XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:screenOrientation="landscape"
    tools:context=".MainActivity">



    <Button
        android:id="@+id/btn22"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playGS" />


    <Button
        android:id="@+id/btn21"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playFS" />

    <Button
        android:id="@+id/btn20"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playDS" />

    <Button
        android:id="@+id/btn19"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playCS" />

    <Button
        android:id="@+id/btn18"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playCC" />

    <Button
        android:id="@+id/btn17"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playB" />

    <Button
        android:id="@+id/btn16"
        android:layout_width="76dp"
        android:layout_height="70dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playA" />

    <Button
        android:id="@+id/btn15"
        android:layout_width="87dp"
        android:layout_height="80dp"
        android:background="@android:color/background_light"
        android:onClick="playG"
        />

    <Button
        android:id="@+id/btn14"
        android:layout_width="87dp"
        android:layout_height="80dp"
        android:background="@android:color/background_light"
        android:onClick="playF" />

    <Button
        android:id="@+id/btn13"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="87dp"
        android:layout_height="80dp"
        android:background="@android:color/background_light"
        android:onClick="playE" />

    <Button
        android:id="@+id/btn2"
        style="@style/Widget.AppCompat.Button.Small"
        android:layout_width="87dp"
        android:layout_height="80dp"
        android:background="@android:color/background_light"
        android:onClick="playC" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="76dp"
        android:layout_height="69dp"
        android:background="@android:color/holo_red_dark"
        android:onClick="playD" />

我尝试了很多互联网上的例子,但是总是以失败告终。

1 个答案:

答案 0 :(得分:0)

第1步:删除android:onClick属性

<Button
    android:id="@+id/btn5"
    android:layout_width="76dp"
    android:layout_height="69dp"
    android:background="@android:color/holo_red_dark"
    android:onClick="playD" />

成为

<Button
    android:id="@+id/btn5"
    android:layout_width="76dp"
    android:layout_height="69dp"
    android:background="@android:color/holo_red_dark" />

第2步:在onCreate()方法的末尾(紧接在btn22 = mSoundPool.load(getApplicationContext(),R.raw.b,1)之后,并在结束}之前)插入此代码

findViewById(R.id.btn5).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) playD(v);
        return false;
    }
});

首先尝试使用其中一个按钮进行操作,然后再尝试下一个按钮。

还有很多其他方法,例如,您可以删除该方法

public void playD(View v){
    mSoundPool.play(btn5,1,1,0,0,1);
}

并将其放在onCreate()中:

findViewById(R.id.btn5).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) mSoundPool.play(btn5,1,1,0,0,1);
        return false;
    }
});

@Pratik_Butani指出了How to define setOnTouchListener for multiple buttons in Android fragment,它虽然稍微复杂一些,但以后再了解一点时,可以很好地查看。