質問

実装した場合 OnClickListener, 、サウンドエフェクトは、ボタンのクリック時に機能します。実装中 OnTouchListener サウンドエフェクトは、触れる際には機能しません。したがって、実装にサウンド効果を有効にする方法 OnTouchListener?

編集:

クリックアプローチを使用する場合のコードは次のとおりです。

public class CalculatorActivity extends Activity implements OnClickListener
{
    //...
    private Button btn1;
    private EditText edLCD;
    //...

    public void onCreate(Bundle savedInstanceState)
    {
        //...
        btn1 = (Button) findViewById(R.id.d1);
        btn1.setOnClickListener(this);

        edLCD = (EditText) findViewById(R.id.edLCD);
    }

    //...

    public void onClick(View v)
    {
        edLCD.setText(edLCD.getText().toString() + "1");
    }
}

そして、私が感動的なアプローチを使用する場合、ここにコードがあります:

public class CalculatorActivity extends Activity implements OnTouchListener
{
    //...
    private Button btn1;
    private EditText edLCD;
    //...

    public void onCreate(Bundle savedInstanceState)
    {
        //...
        btn1 = (Button) findViewById(R.id.d1);
        btn1.setOnTouchListener(this);

        edLCD = (EditText) findViewById(R.id.edLCD);
    }

    //...

    public boolean onTouch(View v, MotionEvent event)
    {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            edLCD.setText(edLCD.getText().toString() + "1");
        }
        return true;
    }
}
役に立ちましたか?

解決

試す...

public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        v.playSoundEffect(SoundEffectConstants.CLICK);
        edLCD.setText(edLCD.getText().toString() + "1");
    }
    return true;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top