自学内容网 自学内容网

点击按钮提示气泡信息(Toast)

演示效果:

a51331152f6ecb300469230c7332c502.png

目录结构:

 2b7986ed7c41ee4c9d4c2fad6089eb1e.png

activity_main.xml(布局文件)代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/btn_t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="消息提示时间长"
        android:textSize="24sp"

        />



</LinearLayout>

 

MainActivity.java(活动类)代码:

package com.example.duihuakuang;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //1.定义对象
    Button btn_t1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //2.将对象与控件绑定
        btn_t1 = findViewById(R.id.btn_t1);



        //3.btn_t1点击事件处理
        btn_t1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //makeText()方法三个参数:显示的页面,显示的内容,显示的时长
                //最后调用show来显示出来
                Toast.makeText(MainActivity.this,"提示时间长",Toast.LENGTH_LONG).show();
            }
        });



    }
}

 

 


原文地址:https://blog.csdn.net/weixin_59155272/article/details/134148590

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!