How can i add another button intent beside previous one?

后端 未结 3 2017
梦毁少年i
梦毁少年i 2021-01-27 14:17

Here is my code: it contains a button named as button1A and when I click on it, It opens a list named as list1. How can I put a code for my a

3条回答
  •  感动是毒
    2021-01-27 14:40

    Change your addListenerOnButton method using switch-case to minimize code and add single OnClickListener listener to multiple button as :

        public void addListenerOnButton() {
    
            button1A = (Button) findViewById(R.id.button1A);
            button1A.setOnClickListener(clicklistener);
            // add OnClickListener to second Button 
            button1B = (Button) findViewById(R.id.button1B);
            button1B.setOnClickListener(clicklistener);
        OnClickListener clicklistener = new View.OnClickListener() {
         @Override
          public void onClick(View v) {
            switch(v.getId()){
    
              case R.id.button1A: 
                        // start second List Activity
                   Intent a1 = new Intent(v.getContext(), List1.class);
                   startActivity(a1);
                   break;
    
              case R.id.button2A: 
                         // start second List Activity
                   Intent a2 = new Intent(v.getContext(), List2.class);
                   startActivity(a2);
                   break;
    
            }
           }
         };
       }
    

提交回复
热议问题