private Button btn_main_download;
@Override
//Main interface Activity
protected void onCreate(Bundle savedInstanceState) {
//Call the onCreate method of the parent class to perform some initialization operations
super.onCreate (savedInstanceState);
//Load the layout and generate the corresponding view object
setContentView(R.layout.activity_main);
//1. Get the Button object
btn_main_download = (Button) findViewById(R.id.btn_main_downioad);
//2. Set click listener for Button
btn_main_download.setOnClickListener(new View.OnClickListener() {
//In the callback method:
@Override
public void onClick(View v) {//Called when the user clicks the button
//1. A small text prompt to start downloading
// Toast toast = Toast.makeText(MainActivity.this, "Start downloading... ...", Toast.LENGTH_SHORT);
// toast.show();
Toast.makeText(MainActivity.this, "Start downloading... ...", Toast.LENGTH_SHORT).show();
//Parameter information (context context, text prompt information, duration prompt time)
//2. Update the text of the Button prompt
btn_main_download.setText("Downloading...");
}
});
}
}
<!--
id: Find the Button control by calling the findViewById method in MainActivity.
Text: The prompt text of the button is configured in strings.xml
"@string/download" 中的download对应strings中的name
-->
<Button
android:id="@+id/btn_main_downioad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/download" />
Activity: one of the four components
onCreate(): a method that is automatically called, in which the layout is loaded and displayed
setContentView(int layout): load layout
View findViewById(int id): Find the corresponding view object according to the id
R: Resources for Applications
R.drawable: An inner class that contains all image resource representations
R.layout: an inner class that contains all layout resource identifiers
R.id: an inner class that contains all view id identifiers
R.string: inner class containing all string identifiers
View/Button: View/Button
setonClickListener(listener): Set a click listener for the view
View.OnClickListener: Internal interface
void onClick(View v): callback method of click event
Toast: class used to display short-term prompt text
static Toast makeText(...): Creates a toast object
show(): show a small tip