Before adding a new activity to the app, we used the button and clicked the button to enter a new activity. Here is a simple summary of the use of buttons on Android.
Design a button is very simple, in the IDE generally have a graphical interface, you can also write yourself in XML, such as: Macau casino play
View Source print?
2 |
android:id="@+id/nowamagicbtn" |
3 |
android:layout_width="wrap_content" |
4 |
android:layout_height="wrap_content" |
5 |
android:text="前往PaintingActivity"/> |
The next key is to give the button a couple of ways to bind the event.
The first type:
View Source print?
04 |
protectedvoidonCreate(Bundle savedInstanceState) { |
05 |
super.onCreate(savedInstanceState); |
06 |
setContentView(R.layout.activity_main); |
08 |
button = (Button)findViewById(R.id.nowamagicbtn); |
10 |
button.setOnClickListener(newOnClickListener(){ |
13 |
publicvoidonClick(View v) { |
14 |
// TODO Auto-generated method stub |
15 |
Intent intent = newIntent(); |
16 |
intent.setClass(MainActivity.this, PaintingActivity.class); |
17 |
startActivity(intent); |
The second type:
View Source print?
04 |
protectedvoidonCreate(Bundle savedInstanceState) { |
05 |
super.onCreate(savedInstanceState); |
06 |
setContentView(R.layout.activity_main); |
08 |
button = (Button)findViewById(R.id.nowamagicbtn); |
09 |
button.setOnClickListener(newButtonListener()); |
12 |
classButtonListener implementsOnClickListener{ |
15 |
publicvoidonClick(View v) { |
16 |
if( v.getId() == R.id.nowamagicbtn){ |
17 |
Intent intent = newIntent(); |
18 |
intent.setClass(MainActivity.this, PaintingActivity.class); |
19 |
startActivity(intent); |
The third type:
View Source print?
04 |
protectedvoidonCreate(Bundle savedInstanceState) { |
05 |
super.onCreate(savedInstanceState); |
06 |
setContentView(R.layout.activity_main); |
08 |
button = (Button)findViewById(R.id.nowamagicbtn); |
09 |
button.setOnClickListener(newButtonListener()); |
12 |
classButtonListener implementsOnClickListener{ |
15 |
publicvoidonClick(View v) { |
16 |
// TODO Auto-generated method stub |
17 |
Intent intent = newIntent(); |
18 |
intent.setClass(MainActivity.this, PaintingActivity.class); |
19 |
startActivity(intent); |
In fact, all are the same, are inherited Onclicklistener, and then rewrite its OnClick method. Summarize the convenience Yimeimei use.
Basics: Setting up Android buttons and binding events