在碎片中使用吐司

我试图显示一个祝酒词时,用户点击一个按钮内的片段。问题是我无法访问这个活动来显示它上面的祝酒词。

下面是 Fragment的来源:

    public class FrgTimes extends Fragment
{
ScrollView sv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
if (container == null) { return null; }


sv = (ScrollView)inflater.inflate(R.layout.frg_times, container, false);


btnTime1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {


//******  HERE's the PROBLEM  ********
Toast.makeText(<The Activity>, "Please long press the key", Toast.LENGTH_LONG );


}});


return sv;
}

这是我试过的方法。

Toast.makeText( getActivity()  , ...
Toast.makeText( getView().getContext()  , ...
Toast.makeText( getActivity().getApplicationContext()  , ...
Toast.makeText( sv.getContext()  , ...
Toast.makeText( sv.getRootView().getContext()  , ...

在调试中,我可以看到所有这些代码运行没有任何例外,但没有 TOAST被显示。

201977 次浏览

You are not calling show() on the Toast you are creating with makeText().

To help another people with my same problem, the complete answer to Use Toast inside Fragment is:

Activity activity = getActivity();


@Override
public void onClick(View arg0) {


Toast.makeText(activity,"Text!",Toast.LENGTH_SHORT).show();
}

As stated by alfo888_ibg:

@Override
public void onClick(View arg0) {
Toast.makeText(activity,"Text!",Toast.LENGTH_SHORT).show();
}

Just do:

    Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();

this worked for me.

user2564789 said it right
But you can also use this in the place of getActivity()
which will make your toast look like this


Toast.makeText(this,"Message",Toast.LENGTH_SHORT).show();

When making a toast in fragment do as following:

Toast.makeText(getActivity(),"Message", Toast.LENGTH_SHORT).show();

When class is extending fragment it is necessary to use getActivity() since fragment is a subclass of activity.

Cheerse

When calling Toast inside android fragment:

1. Activity mActivity=this.getActivity();


2. Toast.makeText(mActivity,"Text you want to display",Toast.LENGTH_SHORT).show();

This works for me.

        public void onClick(View v) {
Context context = v.getContext();
CharSequence text = "Message";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}

Making a Toast inside Fragment

 Toast.makeText(getActivity(), "Your Text Here!", Toast.LENGTH_SHORT).show();

OR

    Activity activityObj = this.getActivity();


Toast.makeText(activityObj, "Your Text Here!", Toast.LENGTH_SHORT).show();

OR

Toast.makeText(this, "Your Text Here!", Toast.LENGTH_SHORT).show();

You can get the current activity with getActivity()

Toast.makeText(getActivity(),"Toast your message" ,Toast.LENGTH_SHORT).show();

Unique Approach

(Will work for Dialog, Fragment, Even Util class etc...)

ApplicationContext.getInstance().toast("I am toast");

Add below code in Application class accordingly.

public class ApplicationContext extends Application {


private static ApplicationContext instance;


@Override
public void onCreate() {
super.onCreate();
instance = this;
}


public static void toast(String message) {
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
}
}

A simple [Fragment] subclass.
Kotlin!
contextA - is a parent (main) Activity. Set it on create object.

class Start(contextA: Context) : Fragment() {


var contextB: Context = contextA;


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val fl = inflater.inflate(R.layout.fragment_start, container, false)


// only thet variant is worked on me
fl.button.setOnClickListener { view -> openPogodaUrl(view) }


return fl;
}


fun openPogodaUrl(view: View) {
try {
pogoda.webViewClient = object : WebViewClient() { // pogoda - is a WebView
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
pogoda.loadUrl("http://exemple.com/app_vidgets/pogoda.html");
}
catch (e: Exception)
{
Toast.makeText(contextB, e.toString(), Toast.LENGTH_LONG).show();
}
}

}

Using Kotlin

Toast.makeText(view!!.context , "your_text", Toast.LENGTH_SHORT).show()

If you are using kotlin then the context will be already defined in the fragment. So just use that context. Try the following code to show a toast message.

Toast.makeText(context , "your_text", Toast.LENGTH_SHORT).show()

In Kotlin android Toast.makeText(activity!!, "Your Text Here!", Toast.LENGTH_SHORT).show()