在片段内单击按钮后,吐司没有出现吗?

时间:2019-04-26 10:18:32

标签: android

当正确输入了edittext文本字段时,我有一个转到另一个页面的按钮。但是,如果用户未正确输入文本字段,我想做的是吐司。错误检查确实有效,因为它可以防止用户进入下一页,即,在cardchecker字段中没有16个数字,或者在cvv checker字段中没有3个数字。

这是针对Windows服务器的。在Toast.makeText()之后,我尝试自行将其更改为getActivity().getApplicationcontextthisgetActivity

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;
    }
}

如果用户未正确输入祝酒词,我希望它会出现,但不会出现祝酒词。

1 个答案:

答案 0 :(得分:3)

尝试

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;
    }
}