java

1.Allactivity.java

package com.zjc.remindingdemo;

import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AllActivity extends AppCompatActivity {
    /**
     *
     * 1.AlertDialog——导入appcompat-v7库可以使得老版本获得新版本效果
     * 2.Toast的调用封装成一个接口,写在一个公共的类中,传入Application对象和Toast要显示的内容
     * (若将context对象传入当contextactivity时会导致内存泄露,故将Application作为传入的context     * 3.Snackbar使用一个动画效果从屏幕的底部弹出来,过一段时间后也会自动消失。
     *
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);
        Button toast = findViewById(R.id.toast_button);
        Button dialog = findViewById(R.id.dialogAlert_button);
        Button snackBar = findViewById(R.id.snackBar_button);
        toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(getApplication(),"This is a toast");
            }
        });

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.setTitle("Title")
                        .setMessage("Dialog content.")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                            }
                        })
                        .show();
            }
        });

        snackBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "data deleted",Snackbar.LENGTH_LONG)
                        .setAction("Undo", new View.OnClickListener(){
                            @Override
                            public void onClick(View v) {
                            }
                        })
                        .show();
            }
        });
    }
}

2.ToastUtil.java

package com.zjc.remindingdemo;

import android.app.Application;
import android.widget.Toast;

/**
 * Created by ZJC on 2018-04-13.
 */

public class ToastUtil {

    private static Toast toast;

    public static void showToast(Application context,
                                 String content) {
        if (toast == null) {
            toast = Toast.makeText(context,
                    content,
                    Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

}

xml布局

activityall.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"
    android:orientation="vertical"
    tools:context="com.zjc.remindingdemo.AllActivity">

    <Button
        android:id="@+id/toast_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast"
        android:textAllCaps="false"/>

    <Button
        android:id="@+id/dialogAlert_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="DialogAlert"
        android:textAllCaps="false"/>

    <Button
        android:id="@+id/snackBar_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SnackBar"
        android:textAllCaps="false"/>

</LinearLayout>

效果:

安卓提示安卓提示安卓提示



相关文章:

  • 2022-01-27
  • 2021-11-26
  • 2021-11-07
  • 2021-04-22
  • 2021-12-16
  • 2022-12-23
  • 2021-11-06
  • 2021-11-17
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2021-06-04
  • 2021-07-13
  • 2021-05-02
  • 2021-12-20
  • 2022-12-23
相关资源
相似解决方案