Getting started with Android. Binding listeners to controls

//Bind the listener to the control


//1. Get the object representing the control
private Button button;
button = (Button) findViewById(R.id.button1);

//2. Define a class that implements the listener interface
class ButtonLister implements OnClickListener {

		@Override
		// Each time the Button is clicked, the string 0 on the TextView is incremented by 1
		public void onClick(View v) {
			count++;
			textView.setText(count+"");//Convert to string
		}

	}

//3. Generate a listener object

//4. Bind the listener object to the control


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

		button = (Button) findViewById(R.id.button1);//1. Get the object representing the control
		
		ButtonLister buttonLister=new ButtonLister();//3. Generate a listener object

		button.setOnClickListener(buttonLister);//4. Bind the listener object to the control
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327012692&siteId=291194637