English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android Programming Basics: Simple Button Event Response and Toast Application Example

This article gives an example of the application of Android simple Button event response and Toast prompt control. Shared for everyone's reference, as follows:

The previous section described the definition of the Button object in main.xml, and here we will learn how to implement event response for Button.

The event handling triggered by the Button button, which we call Event Handle, is just that in Android, button events are controlled by the system's Button.OnClickListener. Readers familiar with Java programming should not be unfamiliar with OnXxxListener. In the following Demo, we will implement that when the Button is clicked, the text of the TextView will change, and a Toast reminder will appear on the screen for a period of time.

Let's take a look at the effect diagram:

Before clicking the button:

After clicking the button:

We mainly modified two places in the program, one is main.xml, and the other is ButtonDemo.java

Main.xml Code as follows:

<?xml version="1.0" encoding="utf-8"-8"&63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"://schemas.android.com/apk/res/android" //1.5By default, the LinearLayout layout is used in the future
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:id="@"+id/textview1" //Define the ID for easy access and control by the Java class
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string"/hello"
  />
<Button
  android:id="@"+id/Add event response1"
  android:layout_width="60px"
  android:layout_height="wrap_content"
  android:layout_gravity="right" //Place the Button on the right side
  android:text="确定"
/>
</LinearLayout>

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);
    //Find the TextView and Button controls defined in main.xml by ID
    textview1 = (TextView)findViewById(R.id.textview)1);}}
    Add event response1 = (Button)findViewById(R.id.button)1);}}
    //);}}
    Add event response1button
      .setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v)
        //{
        Toast hint control
            Toast.makeText(ButtonDemo.this,63;",
            Toast.LENGTH_LONG).show();
        //Change the text of TextView
        textview1.setText("Welcome to Wei Zhulin's blog!");
      }
    });
  }
}

That's all for today.

Readers who are interested in more content related to Android can check the special topics on this site: 'Android Development入门与进阶教程', 'Android View View Skills Summary', 'Summary of Activity Operation Skills in Android Programming', 'Summary of SQLite Database Operation Skills in Android', 'Summary of JSON Format Data Operation Skills in Android', 'Summary of Database Operation Skills in Android', 'Summary of File Operation Skills in Android', 'Summary of SD Card Operation Methods in Android Programming Development', 'Summary of Resource Operation Skills in Android', and 'Summary of Android Widget Usage'.

I hope this article will be helpful to everyone in designing Android programs.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like