Click the button to trigger Toast Android event

Learning record:

We want the effect , pop up a small message box , automatically disappear after a few seconds

Create a new infrastructure projects:

In MainActivity add the following content:

package com.example.reactest;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

public class MainActivity extends AppCompatActivity {

//     downward compatible Activity wherein AppCompatActivity is Activity subclass Activity is Android a system provides a base class event

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        /*

        * Load a layout, the layout of the incoming ID R.layout.activity_main

        *

        * */

        setContentView(R.layout.activity_main);

 

        /*

        * By button trigger Toast by onCreate add code method

        * First through the findViewById () obtained buttonPanel    this example operation of element : findViewById (R.id.buttonPanel);

        * Since this method returns the     view     objects , need to be downcast Button objects

        * Downcast : downcast: subclass references to parent class object   Father f1 = new Son (); // this is called upcasting (upcast ) , now f1 references to a Son Object

        * Upcast: references to the parent class subclass object            Son s1 = (Son) f1; // this is called downcasting ( downward transition ) , now f1 is pointing Son objects

         * */

        Button button_Panel = (Button)findViewById(R.id.buttonPanel);

        /*

        * With setOnClickListener () to register an instance of this listener   OnClickListener ()    but using statement View.OnClickListener ()

        * Why you want to add a front View of it, the reason is behind OnClickListener a View inside the class interface, if the direct use of this interface is not found.

        * */

        button_Panel.setOnClickListener(new View.OnClickListener() {

            /*

            * Monitor is created , click the button will execute if listening to the onClick () method , so

            * Toast function to the onClick () write .

            * */

            @Override

            public void onClick(View v) {

                /*

                * We want the effect , pop up a small message box , automatically disappear after a few seconds

                * Idea : a text box , a timer , a layout position . A display method

                * Static method makeText () to create a   Toast objects , call the   show () The Toast displayed .

                * 说明:   makeText(参数一[Toast要求的上下文  ],    参数二[Toast显示的文本内容],  参数三[Toast显示的时长])

                *   参数一:    Context,由于i活动本身就是一个Context对象,因此直接传入 MainActivity.this

                *   参数二:    内容

                *   参数三:    有两个内置常量 Toast.LENGTH_SHORT    Toast.LENGTH_LONG

                *

                *   引用结束之后记得用   show()方法显示

                * */

                Toast.makeText(MainActivity.this,"你好啊",Toast.LENGTH_SHORT).show();

            }

        });

 

    }

}

在布局视图下添加:

<?xml version="1.0" encoding="utf-8"?>

function(){ //交易杠杆 http://www.fx61.com/definitions/muniu/481.html

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

 

    <!--    只要创建任何资源都会在R文件中生成一个资源id

    在引用布局的时候会调用R.layout.(Activity)可以得到XXXX.xml布局中的id,然后将这个值传到setContentVies()方法即可-->

 

    <!--    wrap_content    当前宽度只要刚好包含里边的内容就行-->

    <!--    match_parent    当前元素和父元素一样宽 相配,相称    -->

 

    <Button

        android:id="@+id/buttonPanel"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="1"

        tools:ignore="MissingConstraints" />

 

<!--    通过button触发Toast 通过onCreate方法中添加代码-->

 

 

</androidx.constraintlayout.widget.ConstraintLayout>


Guess you like

Origin blog.51cto.com/14511863/2463066