播放按钮的默认的Andr​​oid声音,点击onTouch()方法 [英] Playing default android sound of button, clicking onTouch() method

查看:225
本文介绍了播放按钮的默认的Andr​​oid声音,点击onTouch()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我有一些按钮,这应该是 onTouch()方式,当然,我需要改变按钮的文本,当手指ACTION_DOWN位置。但这个按钮应该起到默认按钮点击Android的声音(像的onClick()方法)。我在哪里可以找到这样的声音?我想,那一定是在SDK,但我怎么能找到呢?什么方法对于这样的操作比较好?我使用的MediaPlayer。

清单:

 公共布尔onTouch(视图V,MotionEvent事件){
    INT行动= event.getAction();
    开关(动作){
    案例MotionEvent.ACTION_DOWN:
        addTextShadow(五);
        Log.v(LOG_TAGACTION_DOWN);
        打破;
    案例MotionEvent.ACTION_MOVE:
        clearTextShadow(五);
        isMoved = TRUE;
        Log.v(LOG_TAGACTION_MOVE);
        打破;
    案例MotionEvent.ACTION_UP:
        Log.v(LOG_TAGACTION_UP);
        如果(!isMoved){
            clearTextShadow(五);
            如果(v.getId()== R.id.btn_new){
                Log.v(LOG_TAG,新的pressed);

                            playSound(); //我的方法来播放声音

            }否则,如果(v.getId()== R.id.btn_saved){
                Log.v(LOG_TAG,保存pressed);
            }否则,如果(v.getId()== R.id.btn_random){
                Log.v(LOG_TAG,随机pressed);
            }
        }
        isMoved = FALSE;
        打破;
    }
    返回true;
}
 

和我playSound()方法:

 公共无效playSound(){
    如果(熔点!= NULL){
        mp.release();
        熔点=无效;
    }
    尝试 {
        MP = MediaPlayer的
    .create(MyActivity.this,R.raw.sound); //但在这里我需要默认的声音!
        mp.start();
    }赶上(例外五){
        e.printStackTrace();
    }
}
 

解决方案

使用这种code在onTouch()方法:

  view.playSoundEffect(android.view.SoundEffectConstants.CLICK);
 

如果你只是寻找一个preSET Android的声音那么最好使用年代由框架提供给你免费,此功能。别人做什么,除非有必要进行自定义,只会导致你对工作的框架,而不是与它工作

In my android app I have some buttons, that should work with onTouch() method, course I need to change button's text, when finger in ACTION_DOWN position. But this buttons should to play default android sound of button clicking (like in onClick() method). Where I can find such sound? I think, it must be in SDK, but how can I find it? And what methods are better for such operation? I'm using MediaPlayer.

Listing:

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        addTextShadow(v);
        Log.v(LOG_TAG, "ACTION_DOWN");
        break;
    case MotionEvent.ACTION_MOVE:
        clearTextShadow(v);
        isMoved = true;
        Log.v(LOG_TAG, "ACTION_MOVE");
        break;
    case MotionEvent.ACTION_UP:
        Log.v(LOG_TAG, "ACTION_UP");
        if (!isMoved) {
            clearTextShadow(v);
            if (v.getId() == R.id.btn_new) {
                Log.v(LOG_TAG, "New pressed");

                            playSound(); //MY METHOD TO PLAY SOUND

            } else if (v.getId() == R.id.btn_saved) {
                Log.v(LOG_TAG, "Saved pressed");
            } else if (v.getId() == R.id.btn_random) {
                Log.v(LOG_TAG, "Random pressed");
            }
        }
        isMoved = false;
        break;
    }
    return true;
}

And my playSound() method:

    public void playSound(){
    if (mp != null) {
        mp.release();
        mp = null;
    }
    try {
        mp = MediaPlayer
    .create(MyActivity.this, R.raw.sound); //BUT HERE I NEED DEFAULT SOUND!
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

解决方案

Use this code in your onTouch() method:

view.playSoundEffect(android.view.SoundEffectConstants.CLICK);

If you are simply looking for a preset Android sound then it's better to use this functionality that's provided to you for free by the framework. Doing anything else, unless necessary for customization, would simply result in you working against the framework instead of working with it.

这篇关于播放按钮的默认的Andr​​oid声音,点击onTouch()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆