gpt4 book ai didi

android studio edittext字段值转换

转载 作者:行者123 更新时间:2023-11-29 19:57:37 24 4
gpt4 key购买 nike

我有一个带有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com