Android – 如何通过按钮标签获取资源?

huangapple go评论75阅读模式
英文:

Android - how to get resource by button tag?

问题

我在我的Activity中有8个按钮,res/raw 目录中有8个小音频文件。当点击一个按钮时,应用会播放与该按钮相关联的音频。我将每个音频的名称命名为与关联按钮的标签相同或相似的名称。我想在我的Activity中只有一个方法来解决所有情况。

目前我已经有了以下代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void playPhrase(View view){

        if(view.getTag().toString().equals("doYouSpeak")){
            MediaPlayer mediaPlayer =  MediaPlayer.create(this, R.raw.doyouspeakenglish);
            mediaPlayer.start();
        }
        else if(view.getTag().toString().equals("goodEvening")){
            MediaPlayer mediaPlayer =  MediaPlayer.create(this, R.raw.goodevening);
            mediaPlayer.start();
        }

        // 其他6种情况

    }
}

是否有一种方式可以通过相关的按钮标签(或ID或其他方式)访问音频,而不使用整个if/else语句?类似于:

public void playPhrase(View view){
    String tag = view.getTag().toString();
    MediaPlayer mediaPlayer =  MediaPlayer.create(this, getResources().getIdentifier(tag, "raw", getPackageName()));
    mediaPlayer.start();
}
英文:

I have 8 buttons in my Activity and 8 small audios in the res/raw directory. When one button is clicked, the app plays the audio associated with that particular button. I named each audio the same / similar to the associated button's tag. I want to have only one method in my activity that solves all cases.

This is what I have so far :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void playPhrase(View view){

        if(view.getTag().toString().equals("doYouSpeak")){
            MediaPlayer mediaPlayer =  MediaPlayer.create(this, R.raw.doyouspeakenglish);
            mediaPlayer.start();
        }
        else if(view.getTag().toString().equals("goodEvening")){
            MediaPlayer mediaPlayer =  MediaPlayer.create(this, R.raw.goodevening);
            mediaPlayer.start();
        }

        // the other 6 cases

    }
}

Is there a way to access the audios using the associated button tag (or id or anyhow) without the whole if/else statements ? Something like :

public void playPhrase(View view){
    String tag = view.getTag().toString();
    MediaPlayer mediaPlayer =  MediaPlayer.create(this, R.raw.tag); 
    mediaPlayer.start();
}

答案1

得分: 1

可以使用getIdentifier()方法通过字符串名称获取ID。

String tag = view.getTag().toString();
int id = getResources().getIdentifier(tag, "raw", getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(this, id);
mediaPlayer.start();
英文:

You can use getIdentifier() method to get the id by String name.

String tag = view.getTag().toString();    
Int id = getResources().getIdentifier(tag, "raw", getPackageName());
MediaPlayer mediaPlayer =  MediaPlayer.create(this, id);
mediaPlayer.start();

huangapple
  • 本文由 发表于 2020年8月19日 17:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63484191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定