Android Fragment 实现 OnClickListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11857022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:02:09  来源:igfitidea点击:

Fragment implements OnClickListener

androidandroid-fragmentsonclicklistener

提问by Nick

I've got an application that I'm modernizing. One step of this process is changing to a Fragment based layout (using the Fragments from the support library). I converted my Activities into Fragments, and got the layout working nicely (using a ViewPager, cool stuff!)

我有一个正在现代化的应用程序。此过程的一个步骤是更改为基于 Fragment 的布局(使用支持库中的 Fragment)。我将我的活动转换为片段,并使布局很好地工作(使用 ViewPager,很酷的东西!)

I was having my Activities implement OnClickListener for all of my button-pressing needs. I have the new Fragment incarnations doing the same thing of course, but it looks like "onClick" is never getting hit. Is there something special about Fragments that prevents them from working this way?

我让我的活动实现 OnClickListener 来满足我所有的按钮按下需求。我当然有新的 Fragment 化身在做同样的事情,但看起来“onClick”永远不会被击中。Fragment 有什么特别之处可以阻止它们以这种方式工作吗?

回答by Abhijit Chakra

Just do one this

做一个这个

public class fragmentOne extends Fragment implements OnClickListener {
    Button myButton;

    @Override
    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
        View myView = inflater.inflate(R.layout.fragment_1, container, false);
        myButton = (Button) myView.findViewById(R.id.myButton);
        myButton.setOnClickListener(this);
        return myView;
    }

    @Override
    public void onClick(View v) {
        // implements your things
    }
}

very simple

很简单

回答by MohanRaj S

I will Focus to use the OnClick action for global access, You have to do like this is your project, Must Implement the View.OnClickListener, then Override the Method OnClick(), In OnCreateView()have to do like this button_submit.setOnClickListener(this);for the Views you need, Please see the below code for Clear Answer,Thankyou.

我将重点使用 OnClick 操作进行全局访问,您必须像这是您的项目一样,必须实现View.OnClickListener,然后覆盖方法 OnClick(),在OnCreateView() 中必须像这样button_submit.setOnClickListener(这个); 对于您需要的视图,请参阅以下代码以获得明确的答案,谢谢。

public class New_Project extends Fragment implements View.OnClickListener{

                private View mView;
                private EditText edttxt_projectname;
                private Button button_submit;

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {

                    mView = inflater.inflate(R.layout.fragment_newproject, container,false);
                    edttxt_projectname=(EditText)mView.findViewById(R.id.edttxt_projectname);
                    button_submit=(Button)mView.findViewById(R.id.button_submit);

                    button_submit.setOnClickListener(this);

                    return mView;
                }


                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                    case R.id.button_submit:
                        edttxt_projectname.setText("Test Submit!#@%!#%");
                        break;
                default:
                        break;
                    }

                }
    }

回答by lallu Sukendh

view.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
    switch (v.getId()) {
      case R.id.imgView1:
        Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
        updateImage();
        break;
      case R.id.imgView2:
        Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
        updateImage();
        break;
      case R.id.imgView3:
        Toast.makeText(getActivity(), "Long pressing", Toast.LENGTH_SHORT).show();
        updateImage();
        break;
      default:
        break;
    }

回答by Abdul Rahman A Samad

I want to comment on Abhijit Chakra answer but it seems that I need to have 50 reps for that. For those who are wondering if you can't use Abhijit's answer, it is because of:

我想评论 Abhijit Chakra 的答案,但似乎我需要为此进行 50 次重复。对于那些想知道您是否不能使用 Abhijit 的答案的人,这是因为:

public void OnClick(View v) {
    // implements your things
}

You need to make sure that it is onClick, NOT OnClick. Thankfully Android Studio internal error message come to rescue.

您需要确保它是 onClick,而不是 OnClick。幸运的是,Android Studio 内部错误消息得以解救。