Four kinds of writing in Android click event Comments

Four kinds of writing Android Click event of
the use of inner classes implement the click event
using an anonymous inner classes implement the click event
let MainActivity achieve View.OnClickListener Interface
property by the first method layout file controls: Use internal class
basic steps are as follows:

Create a MyOnClickListener class and implement the interface View.OnClickListener

Rewriting View.OnClickListener interface OnClick (View view) Method

Button binding to a listener, and listen for a click event
sample code as follows:

public class MainActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定
btn = (Button) findViewById(R.id.button);
//使用内部类(实现OnClickListener)
btn.setOnClickListener(new MyOnClickListener());
}

MyOnClickListener the implements View.OnClickListener {class
@Override
public void the onClick (View V) {
Toast.makeText (the this, "I do not point", Toast.LENGTH_SHORT) the .Show ();
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
a second method: use anonymous inner classes

The basic steps are as follows:

Button binding to a listener, and listen for a click event

Directly into a OnClickListener object setOnClickListener method, and a method to realize OnClick
following sample code:

public class MainActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this, "别点我", Toast.LENGTH_SHORT).show();
}
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
The third method: Let MainActivity direct interface to achieve View.OnClickListener

The basic steps are as follows:

Let MainActivity direct implementation View.OnClickListener

OnClick method override category MainActivity

Button binding to a listener, and listen for a click event.
Note: this is the case setOnClickListener incoming
sample code is as follows:

public class MainActivity extends Activity implements OnClickListener {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate (savedInstanceState);
the setContentView (R.layout.activity_main);
BTN = (the Button) the findViewById (R.id.button);
btn.setOnClickListener (the this);
} (http://www.amjmh.com)
/ / implement interface onClick method OnClickListener
@Override
public void onClick (View V) {
Toast.makeText (the this, "I do not point", Toast.LENGTH_SHORT) the .Show ();
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
fourth method: layout file attributes controls

The basic steps are as follows:

Android to add a Button control in activity_main.xml in: onClick = "action" attribute

Activity in the method implemented action, action needs to satisfy the following three conditions:

(1) method access modifier must be public

Name (2) method must be followed by android: onClick = name "action" in exactly the same

(3) View type parameter must be
the following sample code:

public class MainActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);

}
// use reflection to bind a button listener, listen for the click event
/ **
* 1: Method of access modifier must be public
* 2: name of the method must be followed by android: onClick name = "call" in exactly the same
* 3: View type parameter must be
* Note: The parameter View Button object itself is actually
* * /
public void Call (View View) {
Toast.makeText (the this, "I do not point", Toast.LENGTH_SHORT) .show ( );
}
}

Guess you like

Origin www.cnblogs.com/ly570/p/11498541.html