简体   繁体   中英

Multiple buttons in MainActivity Android studio

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText textmsg;
static final int READ_BLOCK_SIZE = 100;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textmsg = (EditText) findViewById(R.id.editText1);

}

 @Override
 public void onClick(View v) {
   Button noteBtn = (Button) findViewById(R.id.noteBtn);
   Button resuBtn = (Button) findViewById(R.id.resuBtn);
   Button agenBtn = (Button) findViewById(R.id.agenBtn);


 noteBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(MainActivity.this, Notes.class);
     }
 });
 resuBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(MainActivity.this, Results.class);
     }
 });
 agenBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(MainActivity.this, Agenda.class);
     }
 });

When I run the application the buttons don't work. If I set the buttons as so..

public class MainActivity extends AppCompatActivity {      
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button agenBtn = (Button) findViewById(R.id.agenBtn);
    Button resuBtn= (Button) findViewById(R.id.resuBtn);
    Button noteBtn = (Button) findViewById(R.id.noteBtn);


    agenBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Agenda.class);
            startActivity(intent);
        }
    });  etc...

If I use this code above, the code works fine and the buttons work correctly. But other functionality with different classes/activities won't run. Could someone please show me a solution or explain how to solve this issue.

You should place:

 this.noteBtn = (Button) findViewById(R.id.noteBtn);
 this.notBtn.setOnClickListener(this);
 this.resuBtn = (Button) findViewById(R.id.resuBtn);
 this.resuBtn.setOnClickListener(this);
 this.timeBtn = (Button) findViewById(R.id.timeBtn);
 this.timeBtn.setOnClickListener(this);

in onCreate() instead of onClick() . Make the buttons belong to the class, so you can reference them in onClick() . You should not be setting new onClickListeners in onClick() , but should rather have a switch statement based on the clicked view's id to determine which button was pressed.

this code

@Override
 public void onClick(View v) {
   Button noteBtn = (Button) findViewById(R.id.noteBtn);
   Button resuBtn = (Button) findViewById(R.id.resuBtn);
   Button timeBtn = (Button) findViewById(R.id.timeBtn);
...

}

is not working because it's never going to get call, since none of your buttons is implementing it, worse yet they have not even been initialized because the onClick has not and will never be called. the correct way to do it is like:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button but1 = (Button) findViewById(R.id.but1);
    Button resBtn = (Button) findViewById(R.id.resBtn);
    Button noteBtn = (Button) findViewById(R.id.noteBtn);


    agendaBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View viewTimeTable) {
            Intent intent = new Intent(MainActivity.this, Agenda.class);
            startActivity(intent);
        }
    });  etc...

because you are first getting a reference to your buttons and assigning them the onClickListener to each one of it.

I will suggest reading more about the Android Activity life cycle you can find it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM