onClickListener does not work in fragment

前端 未结 5 1067
梦谈多话
梦谈多话 2020-11-28 12:59

I\'ve got some problems with the onClicklistener in the fragment. If I click on the button nothing happens at all. I get neither a message from the onClicklistener in Logcat

相关标签:
5条回答
  • 2020-11-28 13:25

    setOnItemClickListener works with me with fragment instead of setOnClickListener

    0 讨论(0)
  • 2020-11-28 13:31

    Do this changes to your code

    1. on class definition:

       public class InputFragment extends Fragment implements View.OnClickListener
      
    2. on calling setOnClickListener:

          translate_button.setOnClickListener(new View.OnClickListener()
          {
                @Override
                public void onClick(View InputFragmentView)
                {
                        Log.d("Test", "onClickListener ist gestartet");
                        Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();                                    
                        saveInString();
                }
           });
      
    0 讨论(0)
  • 2020-11-28 13:33

    The best way to get all views created you should to redefinition the methode :

    @Override
    public void onActivityCreated(Bundle saved) {
        super.onActivityCreated(saved);
         input_text = (EditText) InputFragmentView.findViewById(R.id.input_field);
    
    translate_button = (Button) InputFragmentView.findViewById(R.id.translate);
    
    
    translate_button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Log.d("Test", "onClickListener ist gestartet");
            Toast.makeText(getActivity().getApplicationContext(), "Test",        Toast.LENGTH_LONG).show();
            saveInString();
    
        }
    });
    

    because this methode called after all view have been created. you should read the life cycle of Fragment.

    0 讨论(0)
  • 2020-11-28 13:39

    I think problem is here in your code

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
    .....
    ....
     //problem is here below line
     return inflater.inflate(R.layout.input_fgmt, container, false);
    }
    

    return your already inflated view

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        { 
            View inputFragmentView = inflater.inflate(R.layout.input_fgmt, container, false);
        .....
        ....
    
         return inputFragmentView;
        }
    
    0 讨论(0)
  • 2020-11-28 13:40

    change

     return inflater.inflate(R.layout.input_fgmt, container, false);
    

    to

     return InputFragmentView ;
    

    Also change with this:

    translate_button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Log.d("Test", "onClickListener ist gestartet");
            Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
            saveInString();
    
        }
    });
    

    and import as a import android.view.View;

    0 讨论(0)
提交回复
热议问题