简体   繁体   English

Android:有没有一种很好的方法来创建类似Toast的对话框?

[英]Android: Is there a good way to create a Toast-like dialog?

I would like to display some information to the user, but I don't want the information to dismiss until the user taps elsewhere or presses the back button. 我想向用户显示一些信息,但我不希望在用户点击其他地方或按下后退按钮之前解除信息。 I realize the obvious option for this is to display the text in a dialog (as opposed to a toast). 我意识到显而易见的选择是在对话框中显示文本(而不是吐司)。 I would like my dialog to resemble the system Toast. 我希望我的对话框类似于系统Toast。 I recognize that I can just copy the transient_notification.xml layout (and its relevant resources), but because Toast styling varies widely by device and OS, this is unlikely to produce a good match. 我认识到我可以复制transient_notification.xml布局(及其相关资源),但由于Toast样式因设备和操作系统而异,因此不太可能产生良好的匹配。

So, is there a good way to create a Dialog which inherits the styling for the system Toast? 那么,是否有一种很好的方法来创建一个继承了Toast系统样式的Dialog?

or you could use a custom layout 或者您可以使用自定义布局

Custom method to display a toast 自定义方法显示吐司

public static Toast currentToast;
/**
 * Use a custom display for Toasts.
 *
 * @param message
 */
public static void customToast(String message) {
    // Avoid creating a queue of toasts
    if (currentToast != null) {
        // Dismiss the current showing Toast
        currentToast.cancel();
    }       
    //Retrieve the layout Inflater
    LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //Assign the custom layout to view
    View layout = inflater.inflate(R.layout.custom_toast, null);
    //Return the application context
    currentToast = new Toast(context.getApplicationContext());
    //Set toast gravity to center
    currentToast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
    //Set toast duration
    currentToast.setDuration(Toast.LENGTH_LONG);
    //Set the custom layout to Toast
    currentToast.setView(layout);
    //Get the TextView for the message of the Toast
    TextView text = (TextView) layout.findViewById(R.id.text);
    //Set the custom text for the message of the Toast
    text.setText(message);
    //Display toast
    currentToast.show();
    // Check if the layout is visible - just to be sure
    if (layout != null) {
        // Touch listener for the layout
        // This will listen for any touch event on the screen
        layout.setOnTouchListener(new OnTouchListener() {           
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // No need to check the event, just dismiss the toast if it is showing
                if (currentToast != null) {
                    currentToast.cancel();
                    // we return True if the listener has consumed the event
                    return true;
                }   
                return false;
            }
        });
    }
}

and the custom_toast.xml custom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip"
    android:background="@drawable/custom_toast_shape">

    <TextView
        android:id="@+id/title" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="@color/blue"
        android:textSize="16sp"
        android:text="@string/app_name" 
    />

    <View
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="@color/blue" 
    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:maxEms="15"
        android:gravity="center_horizontal"
        android:id="@+id/text" 
    />
</LinearLayout>

To use this just call 要使用这个只是打电话

Utils.customToast("Just a test to see if toast is working!\nPerfect");

EDIT I have modified the method a little. 编辑我已经修改了一点方法。 Now it won't create a queue of toasts and it will be dismissed on touch, like the normal toast is. 现在它不会创建一个吐司队列,它将在触摸时被解雇,就像正常的吐司一样。

If anyone else can improve it, please feel free to do it :) 如果其他人可以改进它,请随意做:)

您可能想尝试Crouton ,它可以自定义,可以在触摸时解散。

Two possible suggestions. 两个可能的建议。 One possibility is to use a PopupWindow and custom XML. 一种可能性是使用PopupWindow和自定义XML。 Shouldn't be too hard to implement. 不应该太难实施。 Another option may be to use the Toast replacement system found in the open source project Crouton ( https://github.com/keyboardsurfer/Crouton ). 另一种选择可能是使用开源项目Crouton( https://github.com/keyboardsurfer/Crouton )中的Toast替换系统。 Basically it offers a more aesthetically pleasing UI, and I know of of the options waits for the user to click before dismissing. 基本上它提供了一个更美观的用户界面,我知道等待用户在解雇之前点击的选项。 You can download their demo in the Play store. 您可以在Play商店下载他们的演示。

You could use this widget (without the Undo button) as shown here. 你可以使用这个小部件(没有撤销按钮),如下所示。 https://plus.google.com/u/0/+RomanNurik/posts/RA9WEEGWYp6 https://plus.google.com/u/0/+RomanNurik/posts/RA9WEEGWYp6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM