Wednesday, May 23, 2012

Android : Way of Using setOnClickListener for an View

I'm going to use most common View Button for Setting onClick event :
Method One:- One Listener for One button:


Button btn = (Button) findViewById(R.id.myButton); btn. SetOnClickListener (new View.OnClickListener() { Public void onClick(View v) the { // Do Something } });


Method Two:- Share One Listener with Multiple buttons:


Button btn = (Button)findViewById(R.id.mybutton); Button btn2 = (Button)findViewById(R.id.mybutton2); btn.setOnClickListener(handler); btn2.setOnClickListener(handler); View.OnClickListener handler = View.OnClickListener() { Public void onClick(View v) the { switch(v.getId()) { case R.id.mybutton: // Do Something break; case R.id.mybutton2: // Do Something break; } }


Method Three:- Clicklistener bundled with an XML the layout of the Views element, Listener methods defined in the program with a View type of parameters as:


Buttontst.Xml : java code:: Button btn = (Button) findViewById(R.id.mybutton); public void mybuttonlistener(View target){ //do something }








2 comments:

Imran said...

i need to get data from existing database that should be display on android device but iam trying
it is not displaying to my device

Imran said...

source can send to my mailid imrant24@gmail.com

Post a Comment

 
Powered by Blogger