首页 > c# > 在ARCore中_ShowAndroidToastMessage()的功能是什么?

在ARCore中_ShowAndroidToastMessage()的功能是什么? (What is function of _ShowAndroidToastMessage() in ARCore?)

问题

我在HelloAR中读取HelloARController的脚本时发现了这个方法,但我无法理解这个函数:private void _ShowAndroidToastMessage(string message))

有人可以帮我解释这个功能吗?

/// <summary>
/// Show an Android toast message.
/// </summary>
/// <param name="message">Message string to show in the toast.</param>
private void _ShowAndroidToastMessage(string message)
{
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    if (unityActivity != null)
    {
        AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
        unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
        {
            AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity,
                message, 0);
            toastObject.Call("show");
        }));
    }
}

解决方法

ToastsAndroid中用于显示NotificationActivity。您可能知道HTML中的警报消息。使用javaScript alert()函数,我们可以Alert使用弹出消息向用户发送消息,用户可以看到消息并单击OK按钮以关闭对话框。

Toast在Android的编程消息是类似的,但他们是terminated/ dismissed本身(我们没有任何按键)。我们需要设置一个必须显示消息的时间段,当到达时间消息消失时,它通常显示在Activity页面的底部。

_ShowAndroidToastMessage() 方法是Toast消息传递的变种之一。

这是Toast的一个更简单的例子:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View toastView = toast.getView();
        Toast toast = Toast.makeText(this,
                                     "This is a Toast message!", 
                                     Toast.LENGTH_LONG);

        toast.setTextColor(Color.WHITE);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toastView.setBackgroundColor(Color.RED);
        toast.show();
    }
}

在此输入图像描述

希望这可以帮助。

问题

I found this method while reading script of HelloARController in HelloAR, but I couldn't understand this function: private void _ShowAndroidToastMessage(string message)).

Can someone explain this function for me?

/// <summary>
/// Show an Android toast message.
/// </summary>
/// <param name="message">Message string to show in the toast.</param>
private void _ShowAndroidToastMessage(string message)
{
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    if (unityActivity != null)
    {
        AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
        unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
        {
            AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity,
                message, 0);
            toastObject.Call("show");
        }));
    }
}

解决方法

Toasts in Android are used to show Notification within an Activity. You may be knowing what alert messages are in HTML. Using javaScript alert() function we can Alert the user about something using a popup message, the user sees the message and clicks the OK button to dismiss the dialog.

Toast messages in Android Programming are similar but they are terminated/dismissed by itself (we do not have any buttons). We need to set a time period for which the message has to be displayed, when the time is reached the message fades away, it is usually shown at the bottom of the Activity page.

_ShowAndroidToastMessage() method is one of the varieties for Toast messaging.

Here's one more simple example of Toast:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View toastView = toast.getView();
        Toast toast = Toast.makeText(this,
                                     "This is a Toast message!", 
                                     Toast.LENGTH_LONG);

        toast.setTextColor(Color.WHITE);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toastView.setBackgroundColor(Color.RED);
        toast.show();
    }
}

enter image description here

Hope this helps.

相似信息