dimanche 22 février 2015

How to make an action after a button click?



I've been looking all over the forum for answers but no matter how many times I modify it, there's always an error that comes up. it says "Main activity must be abstract or implement abstract method onClick(View) in onClickListener" Here is my code.



import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;


public class MainActivity extends ActionBarActivity implements OnClickListener
{
/*
*Everything here is Array Strings and other variables that are unrelated to the problem.
* I chose not to post them because they would have tooken to much unnecessary space
*/
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1, button2, button3;
button1 = ((Button) findViewById(R.id.button1));
button2 = ((Button) findViewById(R.id.button2));
button3 = ((Button) findViewById(R.id.button3));
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);

OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick( View v) {
switch (v.getId()) {
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
case R.id.button3:
//DO something
break;
}
}

};
}



Aucun commentaire:

Enregistrer un commentaire