简体   繁体   中英

Toast not appearing after button click within a fragment?

I have a button which goes to another page when the edittext textfields have been entered correctly. However what I want to do is make a toast appear if the user has not entered in the textfields correctly. The error checking does work, as it prevents the user going to the next page, ie if there isn't 16 numbers in cardchecker field or 3 numbers in cvv checker field.

This is for a windows server. I have tried changing after the Toast.makeText(, to getActivity().getApplicationcontext , this & getActivity on its own.

public class DonateFragment extends Fragment {
    EditText cardno;
    Button buy;


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_donate,container,false);
        final EditText cardcheck1 =(EditText) rootView.findViewById(R.id.cardno);
        final EditText sortcheck1 =(EditText) rootView.findViewById(R.id.sortcode);
        final EditText cvvcheck1 =(EditText) rootView.findViewById(R.id.cvv);
        final EditText amountcheck1 =(EditText) rootView.findViewById(R.id.amount);
        Button buy = (Button) rootView.findViewById(R.id.buy);


            buy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String cardcehcker = cardcheck1.getText().toString();
                    String sortchecker = sortcheck1.getText().toString();
                    String cvvchecker = cvvcheck1.getText().toString();
                    String amountchecker = amountcheck1.getText().toString();
                    if (cardcehcker.trim().length() == 16){
                        if (cardcehcker.trim().length() < 16){
                        Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                        }
                            if (sortchecker.trim().length() == 6)
                                if (cvvchecker.trim().length() == 3)
                                    if(amountchecker.trim().length()>0){
                                    {
                                        Intent in = new Intent(getActivity(), Donation_thankyou_activity.class);
                                        startActivity(in);
                                    }}}}
            });
        return rootView;
    }
}

I expect a toast to appear if the user has not entered it in correctly, but no toast appears.

try this

public class DonateFragment extends Fragment {
    EditText cardno;
    Button buy;


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_donate,container,false);
        final EditText cardcheck1 =(EditText) rootView.findViewById(R.id.cardno);
        final EditText sortcheck1 =(EditText) rootView.findViewById(R.id.sortcode);
        final EditText cvvcheck1 =(EditText) rootView.findViewById(R.id.cvv);
        final EditText amountcheck1 =(EditText) rootView.findViewById(R.id.amount);
        Button buy = (Button) rootView.findViewById(R.id.buy);


            buy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String cardcehcker = cardcheck1.getText().toString();
                    String sortchecker = sortcheck1.getText().toString();
                    String cvvchecker = cvvcheck1.getText().toString();
                    String amountchecker = amountcheck1.getText().toString();
                    if (cardcehcker.trim().length() == 16 && sortchecker.trim().length() == 6 && cvvchecker.trim().length() == 3 && amountchecker.trim().length()>0){
                        Intent in = new Intent(getActivity(), Donation_thankyou_activity.class);
                        startActivity(in);
                    }
                    else{
                        Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        return rootView;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM