zoukankan      html  css  js  c++  java
  • Android里面的Toast

      Toast通知是在窗口表面弹出的一个简短的小消息。它只填充消息所需要的空间,并且用户当前的Activity依然保持可见性和交互性。这种通知可自动的淡入淡出,且不接受用户的交互事件。Toast通知能够被Activity或Service创建并显示。如果你创建了一个源自Service的Toast通知,它会显示在当前的Activity最上层。

    下面介绍一个Toast里面的常用方法:

    1.makeText()方法 

    public static Toast makeText (Context context, CharSequence text, int duration)
    public static Toast makeText (Context context, int resId, int duration)

      可以使用makeText()方法获取一个Toast实例,第一个makeText方法有3个参数:1.应用程序的上下文Context、2.要显示的文本消息;3.Toast通知持续显示的时间,用show()方法显示Toast通知。显示文本也可以使用资源文件里面的字符串。

    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

      当然可以直接使用组合方法且避免创建Toast对象:

    Toast.makeText(context, text, duration).show();

    2.setGravity()

      这个方法很简单,设置Toast的显示位置:  

    toast.setGravity(Gravity.CENTER, 0, 0);//设置Toast显示位置居中

    3.setView()

      通过查看Toast源代码,我们发现该类里面包含有一个View变量mNextView,所以可以使用setView()来实现我们自定义Toast的需求。这个方法可以让我们使用自定义的Toast,如下例子所示:

    //自定义toast_layout.xml布局文件
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/toast_layout_root"
                  android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:padding="8dp"
                  android:background="#DAAA"
                  >
        <ImageView android:src="@drawable/droid"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginRight="8dp"
                   />
        <TextView android:id="@+id/text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#FFF"
                  />
    </LinearLayout>
    //在Activity里面
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.toast_layout_root));
    
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("This is a custom toast");
    
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();

      Note:除非你要用setView(View)方法定义布局(layout),否则不要使用公共的Toast类构造器。如果不使用自定义的布局(layout),必须使用makeText(Context, int, int)方法来创建Toast

      实例演示的话,可以看这个,里面写了5种不同的Toast,还提供了下载:

      http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html#2390550

    还有一点需要注意:
       在使用Toast作为提示信息时,Toast会显示在屏幕下方,一般用来提示用户的误操作。当用户在某些情况下,用户连续误操作多次时,会导致出现很多个Toast,依次显示,会在页面上停留很长时间,这个会严重影响软件的用户亲和性。我们可以通过一下方法来实现在一个Toast没有结束的时候再显示Toast不累加时间,而是打断当前的Toast,显示新的Toast。这样Toast就不会停留在界面很久。而最多显示一个Toast提示时间的。

      private Toast toast = null;
        
        private void showTextToast(String msg) {
            if (toast == null) {
                toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
  • 相关阅读:
    一起学Vue之表单输入绑定
    简单易懂的单元测试框架-gtest(二)
    简单易懂的单元测试框架-gtest(一)
    最常用设计模式-模板方法模式
    最常用设计模式-简单工厂模式
    最常用设计模式-单例模式
    端口复用后门
    内存取证工具-volatility、foremost
    python的exe反编译
    Linux加密known_hosts文件中的IP
  • 原文地址:https://www.cnblogs.com/adm1989/p/2800880.html
Copyright © 2011-2022 走看看