演示效果:
目录结构:
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();
}
});
}
}