How do I get a button to run a function in a fragment?

omnivore :

I have a fragment and I want a button to run the function Calculate() when I press it. My fragment is also based from one of the templates.

I've tried the XML android:onClick thing, though I assume its looking inside MainActivity instead of the Fragment so it throws an error. I've also followed trying to use an OnClickListener from another post, though setting it up (as in code inside OnCreateCiew) gave errors, though it might just be my amateur knowledge of how it works.

This is my calculate function (the function I'm trying to get to run when my button is clicked), which is inside the fragment activity:

    public void Calculate(View v) {
        EditText InitialVelocityInput = getView().findViewById(R.id.initialVelocityInput);
        EditText VelocityInput = getView().findViewById(R.id.velocityInput);
        EditText AccelerationInput = getView().findViewById(R.id.accelerationInput);
        EditText TimeInput = getView().findViewById(R.id.timeInput);

        Log.d("bing", "bing");
    }

If needed, heres the whole fragment activity

When using the XML onclick, my app just crashes when the button is pressed. When using OnClickListener, I can't even compile because of errors. I don't have much coding expertise to know what the template fragment code is doing, but I assume its conflicting with however I'm setting up the OnClickListener.

Here's how I tried to set up my OnClickListener:

    Button calculatebutton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View kinematicsCalculatorView = inflater.inflate(R.layout.fragment_fragment_calculator_kinematics, container, false);
        calculatebutton = (Button) kinematicsCalculatorView.findViewById(R.id.calculateButton);
        calculatebutton.setOnClickListener(this);
        return kinematicsCalculatorView;
        // Inflate the layout for this fragment
       // return inflater.inflate(R.layout.fragment_fragment_calculator_kinematics, container, false);

    }


    public void onClick(View v) {
        EditText InitialVelocityInput = getView().findViewById(R.id.initialVelocityInput);
        EditText VelocityInput = getView().findViewById(R.id.velocityInput);
        EditText AccelerationInput = getView().findViewById(R.id.accelerationInput);
        EditText TimeInput = getView().findViewById(R.id.timeInput);

        Log.d("bing", "bing");
    }

However, the this in calculatebutton.setOnClickListener(this); shows up as an error, (setOnClickListener(android.view.View.OnClickListener) in View cannot be applied to (com.example.myApp.FragmentCalculatorKinematics)). onClick method is shown as never being used.

omnivore :

I've found somewhat of a solution... Instead of putting my method inside the FragmentActivity, I just placed it into MainActivity and set android:OnClick="Calculate" in the fragment's XML file. Though I'm not sure if theres any repurcussions from doing this.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=108815&siteId=1