This example describes the Android Simple button event response synthesis hint control toast application. Share to everyone for your reference, specific as follows:
We've talked about defining a button object in Main.xml, and here we'll learn how button implements the event response.
Event handling that is triggered by button buttons, we call the event Handle, except that in Android, button events are controlled by the button.onclicklistener of the system, Readers familiar with Java programming should not be unfamiliar with Onxxxlistener. The following demo, we will implement when clicked button, TextView text will change, and appear on the screen for a period of time toast Reminder.
Let's take a look at the effect chart:
Before clicking the button:
After clicking the button:
We've changed the main two places in the program, one is Main.xml, the other is Buttondemo.java.
The Main.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "//1.5 after the default is LinearLayout layout
android:orientation=" vertical "android:layout_width=" fill_parent
"
android:layout_height=" fill_parent "
>
<textview
android:id=" @+id/textview1 "/ The definition ID facilitates Java classes to find it, and controls it
android:layout_width= "fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button
android:id= "@+id/button1"
android:layout_ Width= "60px"
android:layout_height= "wrap_content"
android:layout_gravity= "right"//Let the button on the right-hand
side android:text= "OK"
/>
</LinearLayout>
The Button.java code is as follows:
Package com.android.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Buttondemo extends activity {
private TextView textview1;
Private Button button1;
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
The TextView and button controls
textview1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1) that are defined in Main.xml are found through IDs;
Button1 = (Button) Findviewbyid (r.id.button1);
Add Event Response
Button1.setonclicklistener (new Button.onclicklistener () {public
void OnClick (View v)
{
//toast Hint Control
Toast.maketext (Buttondemo.this,
"the words in TextView have changed, have you noticed?",
Toast.length_long). Show ();
Change the text of the TextView
Textview1.settext ("Welcome to Wei Shinlin's blog!");}}
That's all for today.
More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"
I hope this article will help you with the Android program.