开发者

How do I link my sound files to my buttons?

I have the image buttons inserted, i know how to get images to them, and I have the soundfiles in the res>raw folder.

How do I link开发者_如何学C these sounds to each button? I'm guessing something with the mediacontroller button? (i'm in the graphical layout)

Additional questions:

  • Do I have to make edits in both the main.xml and .java file?
  • Is there anything I need to do to the manifest or r.java file?
  • Does anybody know of some great learning resources out of the ordinary? (i've already scoped out about every vid tutorial on Google dev page)


The best learning resource is the Android Examples attached to the SDK.

http://developer.android.com/guide/topics/media/index.html

Shows example code of what you want to do, all you need to do is adapt it slightly.


then you probably read this documentation about MediaPlayers. You don't need MediaButtons, just apply a simple OnClickListener on each Button. No need to change XML for this, neither the manifest. Just apply OnClickListeners to them. You can do to R.Java whatever you want as it gets overridden all the time you change something in XML, copy a new Resource or whatever.

EDIT due to comment: So if you have a RootView like LinearLayout you could call

LinearLayout yourRootLayout = findView....
int count = yourRootLayout.getChildCount();
for(int i = 0; i < count; i++){
    View v = yourRootLayout.getChildAt(i);
    if(v instanceof Button){
        Button b = (Button)v;
        b.setOnClickListener(this);
    }
}

You just have to let your Activity implement OnClickListener and make a switch in onClick(View v) on Button.getId() and compare it with your R.id.YOUR_IDENTIFIER

Edit2:
I don't know exactly what count it will return you. just check it. I assume that it can either be only direct Childs, or all Childs. if it is every Child you can be happy and just switch the LinearLayout to TableLayout. If not you have to ask TableLayout for ChildCount, iterate over them like above, but Cast them to TableRow. then ask TableRow for Childs and recieve your Buttons.

it may be easier to call findViewById for every Button and set an onClickListener for these 10.

Yes, anything can be done in Java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜