gpt4 book ai didi

java - 使用 RadioButtons 实现 TextWatcher

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:52 24 4
gpt4 key购买 nike

请参阅随附的图像和代码 fragment 以帮助解释。从附图中,我希望用户输入成本数量并选择包含税不含税,并且会在指示的位置自动生成新的成本,而无需按按钮,但无济于事,我无法执行此操作。请有人帮忙。谢谢

在这里查看图片 enter image description here

实现建议的更改并尝试在成本字段中输入输入后,我遇到了如下错误。请提供额外的反馈。谢谢

错误图像 enter image description here

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;


public class Calculator extends Fragment {

private static EditText itemText, editCost, editQuantity, calCost, rTax;
private static RadioGroup rGroup;
private static RadioButton rb;
View gView;

private double bTotal = 0, aTotal = 0, trueCost = 0, taxValue = 16.5, cost = 0, newCost = 0;
private int quantity = 1;

DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat decimalFormat = new DecimalFormat("###,###.##", symbols);

CalculatorListener activityCommander;


public interface CalculatorListener {
void addtoCart(String itemName, int qty, double beforeTax, double afterTax, double bTotal, double aTotal);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
activityCommander = (CalculatorListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
}


public Calculator() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
gView = inflater.inflate(R.layout.fragment_calculator, container, false);


editCost = (EditText) gView.findViewById(R.id.editcost);
itemText = (EditText) gView.findViewById(R.id.itemText);
editQuantity = (EditText) gView.findViewById(R.id.editquantity);
calCost = (EditText) gView.findViewById(R.id.calcost);
rTax = (EditText) gView.findViewById(R.id.rtax);
rGroup = (RadioGroup) gView.findViewById(R.id.rgroup);

final ImageButton FieldButton = (ImageButton) gView.findViewById(R.id.FieldButton);
final ImageButton TaxButton = (ImageButton) gView.findViewById(R.id.TaxButton);
final ImageButton CalButton = (ImageButton) gView.findViewById(R.id.CalButton);

rTax.setEnabled(false);
calCost.setEnabled(false);

rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

rb = (RadioButton)gView.findViewById(checkedId);
}
});

editCost.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) {

try{
update();
}catch (NumberFormatException e)
{
e.printStackTrace();
}
}
});

editQuantity.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) {

try{
update();
}catch (NumberFormatException e)
{
e.printStackTrace();
}
}
});


FieldButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
clearfield();
}

}
);
TaxButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
adjtax();
}

}
);
CalButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
//toCart();
}

}
);


return gView;

}

public void clearfield() {
editCost.setText("");
editCost.setBackgroundResource(R.drawable.edittxt);
editQuantity.setText("");
editQuantity.setBackgroundResource(R.drawable.edittxt);
calCost.setText("");
calCost.setBackgroundResource(R.drawable.edittxt);
itemText.setText("");
itemText.setBackgroundResource(R.drawable.edittxt);
rGroup.clearCheck();
}

public void adjtax() {
editCost.setBackgroundResource(R.drawable.edittxt);
editQuantity.setBackgroundResource(R.drawable.edittxt);
calCost.setBackgroundResource(R.drawable.edittxt);
itemText.setBackgroundResource(R.drawable.edittxt);
rTax.setEnabled(true);
rTax.setText("");
rTax.setBackgroundResource(R.drawable.edittxtfocus);
rTax.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
rTax.setEnabled(true);

} else {
rTax.setEnabled(false);
v.setBackgroundResource(R.drawable.edittxt);
}
}
});

}


public void update(){


if (rTax.getText().toString().isEmpty()) {
taxValue = 16.5;
} else if (!rTax.getText().toString().isEmpty()) {
taxValue = Double.parseDouble(rTax.getText().toString());
}


//CHECKS THE TAX VALUE IF IT NEEDS TO BE CONVERTED
if (taxValue > 1) {
taxValue = taxValue / 100;
} else {
taxValue = taxValue * 1;
}

//CUSTOM VALIDATOR FOR QUANTITY FIELD
if (editQuantity.getText().toString().isEmpty()) {
quantity = 1;
} else if (!editQuantity.getText().toString().isEmpty()) {
quantity = Integer.parseInt(editQuantity.getText().toString());
}


if(rb.getText() == "Include Tax"){
newCost = (((cost = Double.parseDouble(editCost.getText().toString())) * taxValue) + cost) * quantity;
calCost.setText(decimalFormat.format(newCost).toString());
}
else if(rb.getText() == "Exclude Tax"){
newCost = ((cost = Double.parseDouble(editCost.getText().toString())) * quantity);
calCost.setText(decimalFormat.format(newCost).toString());
}

trueCost = cost * quantity;
bTotal = trueCost;
aTotal = newCost;
}


}

最佳答案

rgroup.setOnCheckedChangeListener 从 update() 方法移至 onCreateView() 中。您不必在每次更新文本时都设置监听器。

如果输入了有效值并且选中了复选框,则文本输入后调用的更新方法可能只更新税值。

更新其他建议

我会像您一样通过比较文本来查找单选按钮,将来某个时候您可能想要更改资源文件中的文本或应用其他区域设置,并且此代码将停止工作。

if(rb.getText() == "Include Tax")

我建议与 id 本身进行比较:

if (checkedId == R.id.radio1 )

另一个建议:

考虑更改变量名称以小写字符开头。以大写字母开头使其看起来像类名或常量,并使代码更难以阅读。

private EditText itemText;
private EditText editCost;
private EditText editQuantity;
private EditText calcost;
private EdtiText rTax;

您还可以删除所有(部分)您拥有的焦点更改监听器,并在 Android 可绘制资源中设置这些属性。看看here

更新于2016年9月9日22:22

好一点,但正如您发现的那样,如果 rb 从未初始化过,则对 update 的调用可能会抛出空指针。您应该在更新方法中检查空 rb 并通知用户选择一个选项。您还可以在 onCreateView 中从一开始就将 rb 分配给这两个值中的任何一个,因此它永远不会为 null。

在单选组回调中设置 rb 后,您可能还应该添加对 update() 的调用。这将使屏幕在他们选择一个选项后立即更新。

关于java - 使用 RadioButtons 实现 TextWatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39418092/

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