Toast和Dialog

来源:互联网 发布:c语言原子锁 编辑:程序博客网 时间:2024/05/16 11:41

Toast

Toast是Android系统提供的一种非常好的提醒方式,在程序中可以适用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间。

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button_toast"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击打开Toast"/>    <Button        android:id="@+id/button_toastzidingyi"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="自定义toast按钮"/></LinearLayout>

在一个界面上设置连个按钮,一个来启动系统自带的Toast,一个用来启动自定义的Toast。

系统自带的Toast

系统自带的Toast很简单,只需要在按钮的监听事件中加入:

Toast.makeText(getApplicationContext(),"这是一个Toast",Toast.LENGTH_LONG).show();

自定义的Toast

自定义的Toast需要自己写一个要显示的Toast的界面

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/text_toasttitle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="title"/>    <ImageView        android:id="@+id/image_toast"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/dog1"/>    <TextView        android:id="@+id/text_toastfoot"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="foot"/></LinearLayout>

在这里简单的写了一个界面,然后在按钮监听事件中加入该界面

 Toast toast= Toast.makeText(getApplicationContext(),"nihao",Toast.LENGTH_LONG);                LayoutInflater inflater=getLayoutInflater();                View toastView=inflater.inflate(R.layout.activity_toastinner, null);                toast.setView(toastView);                toast.show();

Dialog

AlertDialog可以再当前的界面弹出一个对话框,这个对话框是制定与所有的界面元素之上的,能够屏蔽掉其他空间的交互能力,一般用于提示一些非常重要的内容或者警告信息。比如为了防止用户误删重要内容,在删除前弹出一个确认对话框。
同样首先建立一个界面,设置几个按钮,用来点击测试Dialog

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button_dialog"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击得到一个dialog"/>    <Button        android:id="@+id/button_dialogselect"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击得到一个带选项的dialog"/>    <Button        android:id="@+id/button_dialogselectsingle"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击得到一个带单选框的dialog"/>    <Button        android:id="@+id/button_dialogselectmul"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击得到一个带多选框的dialog"/></LinearLayout>

这里设置了四个按钮,用来测试四种不同的Dialog界面

最简单的一个Dialog

private void showDialog(){        AlertDialog.Builder builder= new AlertDialog.Builder(DialogActivity.this);        builder.setIcon(R.mipmap.dog1).setTitle("这是Title").setMessage("这是要显式的内容").setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(getApplicationContext(),"点击的是NeutralButton",Toast.LENGTH_SHORT).show();            }        }).setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(getApplicationContext(),"点击的是NegativeButton",Toast.LENGTH_SHORT).show();            }        }).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(getApplicationContext(),"点击的是PositiveButton",Toast.LENGTH_SHORT).show();            }        });        AlertDialog dialog=builder.create();        dialog.show();    }

首先通过 AlertDialog.Builder创建出一个AlertDialog的实例,然后可以为这个对话框设置标题内容,可否取消等属性。然后可以调用setPositiveButton方法来为对话框设置确定按钮的点击事件,调用setNegativeButton方法来设置取消按钮的点击事件,最后调用show()方法将对话框显示出来。

待选项的Dialog

private void showDialog2(){        AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);        builder.setIcon(R.mipmap.dog2).setTitle("这是一个带选择的de de de de Dialog");        builder.setItems(mData, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(getApplicationContext(),"选中了第"+(which+1)+"个选项",Toast.LENGTH_SHORT).show();            }        });        AlertDialog dialog=builder.create();        dialog.show();

其他步骤都一样,就是调用setItems()方法来在对话框中显示一些可选项。

带单选项的Dialog

private void showDialog3(){        AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);        builder.setIcon(R.mipmap.dog3).setTitle("这是一个带单选框的de de de de Dialog");        builder.setSingleChoiceItems(mData, 2, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                mSelect=mData[which];                Toast.makeText(getApplicationContext(),"你的选择是"+mSelect,Toast.LENGTH_LONG).show();            }        });        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                mButton3.setText("你的选择是"+mSelect);            }        });        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {            }        });        //builder.show();        Dialog dialog=builder.show();        dialog.show();    }

同样,Dialog的生成方法都一样,这里使用的是setSingleChoiceItems()方法来实现对话框中可以生成一个单选的内容。可以再设置的按钮事件中添加事件。

带多选框的Dialog

private void showDialog4() {        AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);        builder.setIcon(R.mipmap.dog1).setTitle("这是一个带多选框的 Dialog");        builder.setMultiChoiceItems(mData, mIsChecked, new DialogInterface.OnMultiChoiceClickListener() {            @Override            public void onClick(DialogInterface dialog, int which, boolean isChecked) {                mIsChecked[which]=isChecked;            }        });        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                mSelectmul=new StringBuffer();                for (int i=0;i<mIsChecked.length;i++){                    if (mIsChecked[i]){                        mSelectmul.append(mData[i]+",");                    }                }               mButton4.setText("你的选择是"+mSelectmul);            }        });        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {            }        });        Dialog dialog=builder.create();        dialog.show();    }

这里用setMultiChoiceItems()方法来实现一个内容为多选框的Dialog,其中可以使用StringBuffer将选择的内容堆叠,然后进行操作。

0 0