How to set onclick listener for a button in a fragment in android

前端 未结 3 1043
星月不相逢
星月不相逢 2020-12-16 03:58

My app contains a form as shown in the following image:

\"register

When I click on menu o

3条回答
  •  抹茶落季
    2020-12-16 04:19

    Since the button is a part of the fragment's layout, set the listener there:

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
        View view = inflater.inflate(R.layout.registerblood, container, false);
        String menu = getArguments().getString("Menu");
        location = (Button) view.findViewById(R.id.etlocation);
        location.setText(menu);
        location.setOnClickListener(this);
    
        return view;
    }
    
    @Override
    public void onClick(View v) {
        RegisterBlood activity = (RegisterBlood) getActivity();
        // Now you can contact your activity through activity e.g.:
        activity.onKeyDown(KeyEvent.KEYCODE_MENU, null);
    }
    

提交回复
热议问题