How can i add another button intent beside previous one?

后端 未结 3 1976
梦毁少年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:29

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

提交回复
热议问题