condition' labelOk = true'永远是真的'

时间:2015-11-03 13:04:57

标签: android validation fragment

如果五个独立的验证器将其布尔值设置为true,我想启用我的按钮。但如果我说if (labelOk == true){}我会在标题中看到这个消息。这个问题是什么?

public class AddDataFragment extends Fragment {
    DbHelper mydb;

    Button buttonadd;
    Button buttondelete;

    private String inputProduct = ("");
    private EditText inputLabel;


    //Validator booleans
    boolean labelOk = false;
    boolean serialOk= false;
    boolean macOk= false;
    boolean daaIdOk= false;
    boolean billOk= false;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_data, container, false);

        final Context context = getContext();
        mydb = new DbHelper(context);


        Log.d("labelOk am Ende", String.valueOf(labelOk));
        buttonadd.setEnabled(true);
        buttondelete.setEnabled(true);


        //Initialize the input methods//////////////////////////////////////////////////////////////////////////////////////


        //Label
        inputLabel = (EditText) view.findViewById(R.id.editText_label);
        inputLabel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                Pattern p = Pattern.compile("^[A-Z]{3}-[0-9]{4}$");
                Matcher m = p.matcher(s);

                if (s.hashCode() == inputLabel.hashCode()){
                    labelOk = m.find();
                } else {
                    labelOk = m.find();
                }
                Log.d("ButtonAdd aktiviert?", String.valueOf(labelOk));
            }
        });


        if (labelOk = true){ /////////////////////////////////////////////////////HERE
            buttonadd = (Button) view.findViewById(R.id.button_add_addData);
          }
        buttondelete = (Button) view.findViewById(R.id.button_delete_addData);


        buttonadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = getContext();

                String product = inputProduct;
                String label = inputLabel.getText().toString();


                mydb.insertData(product, label);

                inputLabel.setText("");

            }
        });

        return view;
    }

}

这是我Fragment的短版本,但我认为你会得到它。

3 个答案:

答案 0 :(得分:1)

你使用=而不是==。

另外,为什么你有一个if-else在两种情况下做同样的事情?

            if (s.hashCode() == inputLabel.hashCode()){
                labelOk = m.find();
            } else {
                labelOk = m.find();
            }

答案 1 :(得分:0)

U通过labelOk = true将标签改为true!

答案 2 :(得分:0)

试试这个

if(labelOk) {
    //labelOk is true 
} else {
    //labelOk is false
}