Android开发中的三种提示(Dialog,Toast,Snackbar)实现

来源:互联网 发布:nba2k14中文版下载软件 编辑:程序博客网 时间:2024/06/10 06:29

1.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">        <TextView            android:id="@+id/tv_dialog"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="DIALOG"            android:background="@color/green"            android:gravity="center"            android:textSize="20sp"/>        <TextView            android:id="@+id/tv_toast"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="TOAST"            android:background="@color/green"            android:gravity="center"            android:textSize="20sp"/>        <TextView            android:id="@+id/tv_snackbar"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:text="SNACKBAR"            android:background="@color/green"            android:gravity="center"            android:textSize="20sp"/>    </LinearLayout>

2.引入Snackbar框架

在项目文件下的build.gradle文件里加上
    dependencies {    /**snackbar引入框架*/    compile 'com.android.support:design:25.+'

3.在MainActivity中的实现

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {    TextView tvDialog;    TextView tvToast;    TextView tvSnackbar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tvDialog = (TextView) findViewById(R.id.tv_dialog);        tvToast = (TextView) findViewById(R.id.tv_toast);        tvSnackbar = (TextView) findViewById(R.id.tv_snackbar);        tvDialog.setOnClickListener(this);        tvToast.setOnClickListener(this);        tvSnackbar.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            /**实现弹出dialog*/            case R.id.tv_dialog:                AlertDialog.Builder builder = new AlertDialog.Builder(this);                builder.setTitle("提示");                builder.setMessage("这是一则提示消息!");                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                });                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                });                builder.show();                break;            /**实现弹出toast*/            case R.id.tv_toast:                Toast.makeText(this,"我是一则toast提示消息",Toast.LENGTH_SHORT).show();                break;            /**实现弹出snackbar*/            case R.id.tv_snackbar:                Snackbar.make(v,"我是一则提示消息消息",Snackbar.LENGTH_SHORT).setAction("确定", new View.OnClickListener() {                    @Override                    public void onClick(View v) {                    }                }).show();                break;        }    }}

好了,以上就三种提示的实现方式,比较简单,当然如果有什么不理解的可以加我qq:1029063917,欢迎沟通交流技术问题。

原创粉丝点击