Now that you have spare time, take the opportunity to write a chapter first.
In this chapter, we will learn how to use the Button control and refer to the Toast prompt control.
In Android development, the most used user interaction control may be a Button, and the most used event is probably an onclick event.
These events are also the simplest events. We can call them through the APIS provided by google. Let's take a look at how to do this.
Step 1. Create a new project Ep. Toast. I used the default values for the activity and main view names. Set the view activity_main.xml:
[Java]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignTop = "@ + id/textView1"
Android: layout_marginRight = "61dp"
Android: text = "OK"/>
</RelativeLayout>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignTop = "@ + id/textView1"
Android: layout_marginRight = "61dp"
Android: text = "OK"/>
</RelativeLayout>
It contains a text label and a button control.
Step 2. We will write the core file MainActivity. java. Here I will give you more comments as much as possible:
[Java]
Package com. example. ep. toast;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private TextView txtview1;
Private Button btn1;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Bind the text label Control
Txtview1 = (TextView) findViewById (R. id. textView1 );
// Bind a button control
Btn1 = (Button) findViewById (R. id. button1 );
// Add a click event listener and instantiate a click event listener
Btn1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Click the button to set the text label
Txtview1.setText ("Joven ");
// Click the button and a prompt box appears. The parameters in the prompt box are (BIND activity, prompt content, and display time)
Toast. makeText (MainActivity. this, "Joven", Toast. LENGTH_LONG). show ();
}
});
}
}
Package com. example. ep. toast;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private TextView txtview1;
Private Button btn1;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Bind the text label Control
Txtview1 = (TextView) findViewById (R. id. textView1 );
// Bind a button control
Btn1 = (Button) findViewById (R. id. button1 );
// Add a click event listener and instantiate a click event listener
Btn1.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Click the button to set the text label
Txtview1.setText ("Joven ");
// Click the button and a prompt box appears. The parameters in the prompt box are (BIND activity, prompt content, and display time)
Toast. makeText (MainActivity. this, "Joven", Toast. LENGTH_LONG). show ();
}
});
}
}
Finally, let's take a look at the effect.
Well, this chapter will talk about this. Although these are all basic things. However, no matter how large and complex a project is, it is actually built on these things. Only when knowledge is accumulated will it become the essence.