在片段中的onClick方法中敬酒 [英] Toast inside onClick method in Fragment

查看:88
本文介绍了在片段中的onClick方法中敬酒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下片段:

public class FragmentSocial extends Fragment implements ActionBar.TabListener, OnClickListener 
{

private Fragment mFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setContentView(R.layout.fragment_social);
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // relevant code...
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // relevant code...
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.imgBtnFB:
        Toast.makeText(this, "FB pressed", Toast.LENGTH_LONG).show();
        break;
    case R.id.imgBtnTwitter:
        Toast.makeText(this, "Twitter pressed", Toast.LENGTH_LONG).show();
        break;
    }

}

我的fragment_social布局中有一些图像按钮.现在,我只想发送一条Toast消息以查看所按下的按钮.但是,如果我使用this作为上下文,则会收到以下错误消息: Toast类型的makeText(Context,CharSequence,int)方法不适用于参数(FragmentSocial,String,int) 我试图将this更改为FragmentSocial.this,更改为FragmentSocial.this.getActivity(),尝试创建onc方法中的private Context mContext并将其实例化为mContext = (I tried various options here)-但没有任何效果.我或者没有错误消息,但也没有看到Toast,或者有其他错误.

I have a few image buttons in my fragment_social layout. For now I just want to make a Toast message to see what button was pressed. However, if I use this as context, I'm getting this error message: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (FragmentSocial, String, int) I tried to change this to FragmentSocial.this, to FragmentSocial.this.getActivity(), tried to create private Context mContext and to instantiate it to mContext = (I tried various options here) inside the onCreate method - but nothing worked. I either didn't have an error message, but also didn't see the Toast, or got other errors.

那么我如何在这里制作吐司?

So how can I create a Toast here?

谢谢!

推荐答案

使用:

getActivity().getBaseContext();

这篇关于在片段中的onClick方法中敬酒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆