Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向特定toast函数添加参数?_Java_Android_Android Studio - Fatal编程技术网

Java 如何向特定toast函数添加参数?

Java 如何向特定toast函数添加参数?,java,android,android-studio,Java,Android,Android Studio,这是一个有点奇怪的问题,这么简单,但我被绊倒了!我有我的自定义toast,所以当我调用它时,我想调用这样的函数,例如showToast(“toast上的文本”),我试图这样做,但当我单击按钮时,它没有显示,这是我的代码 private void customToast(String Content) { Context context = getApplicationContext(); LayoutInflater inflater = getLayoutIn

这是一个有点奇怪的问题,这么简单,但我被绊倒了!我有我的自定义toast,所以当我调用它时,我想调用这样的函数,例如showToast(“toast上的文本”),我试图这样做,但当我单击按钮时,它没有显示,这是我的代码

private void customToast(String Content) {

        Context context = getApplicationContext();
        LayoutInflater inflater = getLayoutInflater();

        View customToastroot = inflater.inflate(R.layout.activity_toast, null);
        Toast customToast = new Toast(context);

        TextView toastText = findViewById(R.id.toast_text);

        Content = "";

        toastText.setText(Content);

        customToast.setView(customToastroot);
        customToast.setDuration(Toast.LENGTH_LONG);
        customToast.setGravity(Gravity.TOP, 0, 0);
        customToast.show();

    }
这是我的按钮,由JavaScriptInterface调用,工作正常,但添加(字符串内容)后,toast没有显示


    public class WebViewJavaScriptInterface{

        private Context context;

        WebViewJavaScriptInterface(Context context){

            this.context = context;

        }

        @JavascriptInterface
        public void showToast() {

            customToast("Thanks Allah");

        }

        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {

            this.context = context;

        }

    }
这是我的活动_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/toast"
        android:alpha="0.80"
        tools:ignore="UselessParent"
        android:gravity="center_horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="this is the TextVie"
            android:textSize="16sp"
            android:textColor="@color/colorWhite"
            android:layout_gravity="center"
            android:fontFamily="@font/open_sans_semibold"
            android:textStyle="bold"
            tools:ignore="HardcodedText"/>

    </LinearLayout>

</LinearLayout>


设置
Content=“”可能是你的问题。@computercarguy我删除了,但没有显示,我添加了一些关于我的问题的信息。感谢您的活动\u toast布局代码,它将help@EmadSeliem我加了它请检查下面的代码谢谢先生,你救了我一天。它工作得很好!
private void customToast(String Content) {

        Context context = getApplicationContext();
        LayoutInflater inflater = getLayoutInflater();
        View customToastroot = inflater.inflate(R.layout. activity_toast, null);
        Toast toast = Toast.makeText(context, Content, Toast.LENGTH_LONG);
        toast.setView(customToastroot);
        TextView text =  customToastroot.findViewById(R.id.toast_text);
        /*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
        text.setText(Content);
        toast.show();
    }