How can i add another button intent beside previous one?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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 another button named as button2A which open a list as List2.

import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener;  import android.widget.Button;  public class Tab_1st extends Activity {  Button button1A;   @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.tablayout1);      addListenerOnButton();   }  public void addListenerOnButton() {      button1A = (Button) findViewById(R.id.button1A);      button1A.setOnClickListener(new OnClickListener() {          @Override         public void onClick(View v) {              Intent a1 = new Intent(v.getContext(), List1.class);             startActivity(a1);          }     });  }  }

回答1:

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;          }        }      };    }


回答2:

you can add more click listeners if you want. try like this.

public void addListenerOnButton() {  button1A = (Button) findViewById(R.id.button1A);  button1A.setOnClickListener(new OnClickListener() {      @Override     public void onClick(View v) {          Intent a1 = new Intent(v.getContext(), List1.class);         startActivity(a1);      } });  button2A = (Button) findViewById(R.id.button2A);  button2A.setOnClickListener(new OnClickListener() {      @Override     public void onClick(View v) {          Intent i = new Intent(v.getContext(), List2.class);         startActivity(i);      } });

}



回答3:

One way of doing this can be by implementing the OnClickListener.

public class Tab_1st extends Activity implements OnClickListener {  Button button1A; Button button2A;  @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.tablayout1);     button1A = (Button) findViewById(R.id.button1A);     button2A = (Button) findViewById(R.id.button2A);     button1A.setOnClickListener(this);     button2A.setOnClickListener(this); }  @Override public void onClick(View v) {     if (v == button1A) {         // Start Activity a1         Intent a1 = new Intent(v.getContext(), List1.class);         startActivity(a1);     } else if (v == button2A) {         // Start Activity a2         Intent a2 = new Intent(v.getContext(), List2.class);         startActivity(a2);     }  }  }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!