Java 如何向特定toast函数添加参数?
这是一个有点奇怪的问题,这么简单,但我被绊倒了!我有我的自定义toast,所以当我调用它时,我想调用这样的函数,例如showToast(“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
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();
}