Showing posts with label android application development tutorial. Show all posts
Showing posts with label android application development tutorial. Show all posts

Tuesday, 21 May 2013

Registering events for views


Watch this on YouTube

MainActivity.java

  1. package com.registerevent;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9. public class MainActivity extends Activity {
  10. Button b1, b2;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. b1 = (Button) findViewById(R.id.button1);
  16. b2 = (Button) findViewById(R.id.button2);
  17. b1.setOnClickListener(bListener);
  18. b2.setOnClickListener(bListener);
  19. }
  20. private OnClickListener bListener = new OnClickListener()
  21. {
  22. @Override
  23. public void onClick(View v) {
  24. // TODO Auto-generated method stub
  25. String label = ((Button)v).getText().toString();
  26. Toast.makeText(getBaseContext(), "you click" +label, Toast.LENGTH_LONG).show();
  27. }
  28. };
  29. @Override
  30. public boolean onCreateOptionsMenu(Menu menu) {
  31. // Inflate the menu; this adds items to the action bar if it is present.
  32. getMenuInflater().inflate(R.menu.main, menu);
  33. return true;
  34. }
  35. }