Skip to content Skip to sidebar Skip to footer

Setonclicklistener In Fragment

I have simple application which has 1 Edittext and 1 Button.My application get the numbers in the first two Edit text and when the button is pressed the last EditText show the sum

Solution 1:

package info.androidhive.slidingmenu;

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 publicclassFindPeopleFragmentextendsFragment {
EditText Num1, Num2, Eq;
Button Sum;
int S;

publicFindPeopleFragment() {
}

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

    super.onCreateView(inflater, container, savedInstanceState);
    ViewInputFragmentView= inflater.inflate(
            R.layout.fragment_find_people, container, false);
    Num1 = (EditText) InputFragmentView.findViewById(R.id.Num1);
    Num2 = (EditText) InputFragmentView.findViewById(R.id.Num2);
    Eq = (EditText) InputFragmentView.findViewById(R.id.Eq);
    Sum = (Button) InputFragmentView.findViewById(R.id.Sum);
    ;

    Sum.setOnClickListener(newView.OnClickListener() {

        @OverridepublicvoidonClick(View v) {
            StringN1= Num1.getText().toString();
            intN11= Integer.parseInt(N1);

            StringN2= Num2.getText().toString();
            intN22= Integer.parseInt(N2);

            S = N11 + N22;

            Eq.setText("" + S);
            Toast.makeText(getActivity(), "your Sum is : " + S,
                    Toast.LENGTH_LONG).show();
        }
    });
    return InputFragmentView;
}
}

Solution 2:

Try it this way:

Sum = (Button) InputFragmentView.findViewById(R.id.Sum);

Sum.setOnClickListener(this);

returnInputFragmentView;
}


publicvoidonClick(View v) {
    switch (v.getId()){
        case R.id.Sum:
            StringN1 = Num1.getText().toString();
            int N11 = Integer.parseInt(N1);

            StringN2 = Num2.getText().toString();
            int N22 = Integer.parseInt(N2);

            S = N11 + N22;

            Eq.setText("" + S);
            break;
    }
}

Anyway, post your Logcat output to know which is the problem you are facing

Solution 3:

Or just leave it like this, but remove the implementation of OnClickListener. ex: public class FindPeopleFragment extends Fragment implements OnClickListener { ... }

Post a Comment for "Setonclicklistener In Fragment"