2017-07-31 59 views
-6

我的项目有40个按钮,我必须在我的java类(我的意思是我的java类中的所有按钮都是findViewById)初始化它们。然后这些按钮播放mediaPlayer,我也必须使用MediaPlayer类(我的意思是MediaPlayer mp = MediaPlayer.create(this,R.id.sound)),然后我必须写每个按钮setOnClickListener初始化java类中的大量小部件

所有这些作品一个接一个地真的很难和无聊。有没有更快的方法来做所有这些事情?我是android新手。请帮忙!

public class MainActivity extends AppCompatActivity { 
    Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40; 
    MediaPlayer mb1, mb2, mb3, mb4, mb5, mb6, mb7, mb8, mb9, mb10, mb11, mb12, mb13, mb14, mb15, mb16, mb17, mb18, mb19, mb20, mb21, mb22, mb23, mb24, mb25, mb26, mb27, mb28, mb29, mb30, mb31, mb32, mb33, mb34, mb35, mb36; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     b1 = (Button) findViewById(R.id.button1); 
     b2 = (Button) findViewById(R.id.button2); 
     b3 = (Button) findViewById(R.id.button3); 
     b4 = (Button) findViewById(R.id.button4); 
     b5 = (Button) findViewById(R.id.button5); 
     b6 = (Button) findViewById(R.id.button6); 
     b7 = (Button) findViewById(R.id.button7); 
     b8 = (Button) findViewById(R.id.button8); 
     b9 = (Button) findViewById(R.id.button9); 
     b10 = (Button) findViewById(R.id.button10); 
     b11 = (Button) findViewById(R.id.button11); 
     b12 = (Button) findViewById(R.id.button12); 
     b13 = (Button) findViewById(R.id.button13); 
     b14 = (Button) findViewById(R.id.button14); 
     b15 = (Button) findViewById(R.id.button15); 
     b16 = (Button) findViewById(R.id.button16); 
     b17 = (Button) findViewById(R.id.button17); 
     b18 = (Button) findViewById(R.id.button18); 
     b19 = (Button) findViewById(R.id.button19); 
     b20 = (Button) findViewById(R.id.button20); 
     b21 = (Button) findViewById(R.id.button21); 
     b22 = (Button) findViewById(R.id.button22); 
     b23 = (Button) findViewById(R.id.button23); 
     b24 = (Button) findViewById(R.id.button24); 
     b25 = (Button) findViewById(R.id.button25); 
     b26 = (Button) findViewById(R.id.button26); 
     b27 = (Button) findViewById(R.id.button27); 
     b28 = (Button) findViewById(R.id.button28); 
     b29 = (Button) findViewById(R.id.button29); 
     b30 = (Button) findViewById(R.id.button30); 
     b31 = (Button) findViewById(R.id.button31); 
     b32 = (Button) findViewById(R.id.button32); 
     b33 = (Button) findViewById(R.id.button33); 
     b34 = (Button) findViewById(R.id.button34); 
     b35 = (Button) findViewById(R.id.button35); 
     b36 = (Button) findViewById(R.id.button36); 
     b37 = (Button) findViewById(R.id.button37); 
     b38 = (Button) findViewById(R.id.button38); 
     b39 = (Button) findViewById(R.id.button39); 
     b40 = (Button) findViewById(R.id.button40); 
     mb1 = MediaPlayer.create(this, R.raw.ability); 
     mb2 = MediaPlayer.create(this, R.raw.applause); 
     mb3 = MediaPlayer.create(this, R.raw.appointment); 
     mb4 = MediaPlayer.create(this, R.raw.atthemoment); 
     mb5 = MediaPlayer.create(this, R.raw.audience); 
     mb6 = MediaPlayer.create(this, R.raw.depression); 
     mb7 = MediaPlayer.create(this, R.raw.disaster); 
     mb8 = MediaPlayer.create(this, R.raw.dizzy); 
     mb9 = MediaPlayer.create(this, R.raw.educated); 
     mb10 = MediaPlayer.create(this, R.raw.emotion); 
     mb11 = MediaPlayer.create(this, R.raw.entertain); 
     mb12 = MediaPlayer.create(this, R.raw.experience); 
     mb13 = MediaPlayer.create(this, R.raw.focus); 
     mb14 = MediaPlayer.create(this, R.raw.however); 
     mb15 = MediaPlayer.create(this, R.raw.humor); 
     mb16 = MediaPlayer.create(this, R.raw.impress); 
     mb17 = MediaPlayer.create(this, R.raw.inspire); 
     mb18 = MediaPlayer.create(this, R.raw.instruct); 
     mb19 = MediaPlayer.create(this, R.raw.massage); 
     mb20 = MediaPlayer.create(this, R.raw.medication); 
     mb21 = MediaPlayer.create(this, R.raw.mental); 
     mb22 = MediaPlayer.create(this, R.raw.offer); 
     mb23 = MediaPlayer.create(this, R.raw.painkiller); 
     mb24 = MediaPlayer.create(this, R.raw.permission); 
     mb25 = MediaPlayer.create(this, R.raw.physician); 
     mb26 = MediaPlayer.create(this, R.raw.possible); 
     mb27 = MediaPlayer.create(this, R.raw.present); 
     mb28 = MediaPlayer.create(this, R.raw.psychologist); 
     mb29 = MediaPlayer.create(this, R.raw.resemble); 
     mb30 = MediaPlayer.create(this, R.raw.sincerely); 
     mb31 = MediaPlayer.create(this, R.raw.specialist); 
     mb32 = MediaPlayer.create(this, R.raw.speech); 
     mb33 = MediaPlayer.create(this, R.raw.splittingheadache); 
     mb34 = MediaPlayer.create(this, R.raw.sprain); 
     mb35 = MediaPlayer.create(this, R.raw.touch); 
     mb36 = MediaPlayer.create(this, R.raw.treat); 
+3

尝试按钮的列表和一个循环。你没有编码40个单独的按钮,是吗? – duffymo

+0

试试[适用于Android的ButterKnife](http://jakewharton.github.io/butterknife/)。它是一个用于字段和方法绑定的库。 – Jordan

+0

也请在你问一些别人的事之前自己做一些研究。 – Jordan

回答

0

你应该做这样的事情,例如,为按钮:

Button[] button=new Button[40]; 

for(i=0;i<40;i++){ 
    String buttonName="button"+(i+1); 
    int buttonId=getResources().getIdentifier(buttonName,"id",mContext.getPackageName()); 
    button[i] = (Button) findViewById(buttonId); 
} 
0

使用Recyclerview的ListView显示按钮的列表