android studio edittext字段值转换

标签 android if-statement android-edittext try-catch

我有一个带有 EditText 和保存按钮的 fragment 。当用户点击保存按钮时,它应该检查edittext值是否超出范围或相同的数据类型。对于这种情况,值类型应为“FLOAT”,范围为“0.00 - 1000.00”。

这是我的 fragment.java 代码。

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

    View v = inflater.inflate(R.layout.field_fragment,container, false);

    int field = getArguments().getInt("field");

    //Modify label
   // TextView fieldLabel = (TextView)v.findViewById(field);
    //fieldLabel.setText(MainActivity.pageNames[field]);

    return v;


}

public void onStart() {
    super.onStart();
    //---Button view---
    Button save = (Button)
            getActivity().findViewById(R.id.saveButton);


    save.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {




            try{
            Float conductivity = Float.parseFloat(conductivity_field.getText().toString());

                if (conductivity > 0.F && conductivity < 1000.F) {
                    Toast.makeText(getActivity(), "The value is correct", Toast.LENGTH_SHORT).show();
                } else {

                    //conductivity_field.setText(R.string.conductivity_field);//set text of the edittext back to the hint
                    Toast.makeText(getActivity(), "Wrong value for float", Toast.LENGTH_SHORT).show();

                }


        }catch(Exception e){
                Toast.makeText(getActivity(), "Error converting values. Please enter correct value.", Toast.LENGTH_SHORT).show();
            }

        }
    });
}

所以我试图显示这些错误,但是当我单击它时,它会忽略 if 条件并捕获异常并显示“error converting values msg...”

还有其他办法吗?

最佳答案

用这段代码替换你的代码->

EditText conductivity_field;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.field_fragment,container, false);
        conductivity_field = (EditText) v.findViewById(R.id.conductivity_value);

        int field = getArguments().getInt("field");

        //Modify label
        // TextView fieldLabel = (TextView)v.findViewById(field);
        //fieldLabel.setText(MainActivity.pageNames[field]);

        return v;


    }

    public void onStart() {
        super.onStart();
        //---Button view---
        Button save = (Button)
                getActivity().findViewById(R.id.saveButton);


        save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                try{
                    Float conductivity = Float.parseFloat(conductivity_field.getText().toString());

                    if (conductivity > 0.F && conductivity < 1000.F) {
                        Toast.makeText(getActivity(), "The value is correct", Toast.LENGTH_SHORT).show();
                    } else {

                        //conductivity_field.setText(R.string.conductivity_field);//set text of the edittext back to the hint
                        Toast.makeText(getActivity(), "Wrong value for float , BITCH", Toast.LENGTH_SHORT).show();

                    }


                }catch(Exception e){
                    Toast.makeText(getActivity(), "Error converting values. Please enter correct value.", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

关于android studio edittext字段值转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36588627/

相关文章:

java - 如何将制表符转换为空格并在 android studio 中保存时自动格式化

android - Maven不会因引入的编译错误而失败

非重复整数的Python总和

android:errorMessageBackground 在 styles.xml 中找不到资源错误

java - android java 搜索 listview clickedItem

android - 如何在 Android 的 Cloud Endpoints 构建器中禁用 GZipContent

android - 在读取 xml 文件时需要帮助

MySQL IF NOT NULL,则显示 1,否则显示 0

BASH - 使用 Loop 和 If 语句从唯一字段中的多个字段汇总信息

android软键盘横向覆盖editText