onClickListener 调用 TextWatcher:非法前向引用
onClickListener calls TextWatcher: Illegal forward reference
我正在研究单选按钮,我希望每个 RadioButton
在每个 onClick
中重新调用 TextWatcher
来更新我的 TextView
但它没有没用。
所有这些代码都在 Fragment
中,而我只有 一个 Activity
单选按钮:
View.OnClickListener radio1Clicked = v ->{
radioButton1.addTextChangedListener(textWatcher); //error under textWatcher : Illegal forward reference
//More Operations Running here ... };
View.OnClickListener radio2Clicked = v ->{
radioButton2.addTextChangedListener(textWatcher)};//error under textWatcher: Illegal forward reference
//More Operations Running here ... };
View.OnClickListener radio3Clicked = v ->{
radioButton2.addTextChangedListener(textWatcher)};//error under textWatcher: Illegal forward reference
//More Operations Running here ... };
文字观察者:
TextWatcher textWatcher= new TextWatcher(){
//Declarations and other stuff
// RadioButton1
if (radioButton1.isChecked()) {
basePrintable = 50;
calculatePcs = (int) (50 / sqIn);
iPieces.setText(String.valueOf(calculatePcs ));
if (!checker)//check Switch {
iPriceOut.setText("150.0");
} else if (checker) //re-check Switch{
iPriceOut.setText("50.0");
}
}
// RadioButton1
if (radioButton1.isChecked()) {
basePrintable = 50;
calculatePcs = (int) (50 / sqIn);
iPieces.setText(String.valueOf(calculatePcs ));
if (!checker)//check Switch {
iPriceOut.setText("150.0");
} else if (checker) //re-check Switch{
iPriceOut.setText("50.0");
}
}
// RadioButton2
if (radioButton1.isChecked()) {...}
// RadioButton3
if (radioButton1.isChecked()) {...}
basePcs = Integer.parseInt(iPieces.getText().toString());
baseSheet =
Integer.parseInt(iShit.getText().toString());
basePrice =
Double.parseDouble(iPriceOut.getText().toString());
}
我知道它可以被称为因为我已经在这里做了:(而且它有效)
public void onCheckedChanged(CompoundButton switchInput, boolean isChecked) {
switchInput.addTextChangedListener(size);
if (!isChecked) {
switchInput.setText("...");
radioButton1.setText("...");
radioButton2.setText("...");
radioButton3.setText("...");
} else {
switchInput.setText("...");
radioButton1.setText("...");
radioButton2.setText("...");
radioButton3.setText("...");
}
}
我是一个彻头彻尾的菜鸟,只会跟着教程为自己拼接这个应用程序。这是我必须解决的最后一个问题,因为其他的都或多或少地完成了。
感谢您的帮助
我认为问题在于您不能引用内部单击的对象,而是使用传递的视图
View.OnClickListener o1 = v ->{
iOpt1.addTextChangedListener(size); //error: Illegal Forward Reference
if (iOpt1.isChecked()) {
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
iOpt1.setChecked(true);
};
改为
View.OnClickListener o1 = v ->{
v.addTextChangedListener(size); <--change here //error: Illegal Forward Reference
if (v.isChecked()) { <--change here
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
v.setChecked(true);<--change here
};
在单击视图时使用 v
我只需要更新或刷新 TextWatcher
,方法是将它自己的内容重新分配回注入它的 EditText
,这将导致更新并将 运行 TextWatcher
因为基本上已经更新了。
View.OnClickListener o1 = v ->{
if (iOpt1.isChecked()) {
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
iOpt1.setChecked(true);
//SOLUTION: save the value on where the **TextWatcher** is injected then re-assign the same value you saved
refesher = iLength.getText().toString();
iLength.setText(refesher);
//This will cause an update to the **TextWatcher** and will run the code again
};
TextWatcher size = new TextWatcher() {...}
这里有更多上下文
public class PrintFragment extends Fragment{
EditText iLength, iHeight;
RadioButton iOpt1, iOpt2, iOpt3;
String refresher;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//inflate
iLength = V.findViewById(R.id.length);
iHeight = V.findViewById(R.id.height);
iOpt1 = V.findViewById(R.id.opt1);
iOpt2 = V.findViewById(R.id.opt2);
iOpt3 = V.findViewById(R.id.opt3);
iLength.addTextChangedListener(size);
iHeight.addTextChangedListener(size);
iOpt1.setOnClickListener(o1);
iOpt2.setOnClickListener(o2);
iOpt3.setOnClickListener(o3);
return;
}
View.OnClickListener o1 = v ->{
if (iOpt1.isChecked()) {
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
iOpt1.setChecked(true);
refesher = iLength.getText().toString();
iLength.setText(refesher);
};
TextWatcher size = new TextWatcher() {...}
}
我正在研究单选按钮,我希望每个 RadioButton
在每个 onClick
中重新调用 TextWatcher
来更新我的 TextView
但它没有没用。
所有这些代码都在 Fragment
中,而我只有 一个 Activity
单选按钮:
View.OnClickListener radio1Clicked = v ->{
radioButton1.addTextChangedListener(textWatcher); //error under textWatcher : Illegal forward reference
//More Operations Running here ... };
View.OnClickListener radio2Clicked = v ->{
radioButton2.addTextChangedListener(textWatcher)};//error under textWatcher: Illegal forward reference
//More Operations Running here ... };
View.OnClickListener radio3Clicked = v ->{
radioButton2.addTextChangedListener(textWatcher)};//error under textWatcher: Illegal forward reference
//More Operations Running here ... };
文字观察者:
TextWatcher textWatcher= new TextWatcher(){
//Declarations and other stuff
// RadioButton1
if (radioButton1.isChecked()) {
basePrintable = 50;
calculatePcs = (int) (50 / sqIn);
iPieces.setText(String.valueOf(calculatePcs ));
if (!checker)//check Switch {
iPriceOut.setText("150.0");
} else if (checker) //re-check Switch{
iPriceOut.setText("50.0");
}
}
// RadioButton1
if (radioButton1.isChecked()) {
basePrintable = 50;
calculatePcs = (int) (50 / sqIn);
iPieces.setText(String.valueOf(calculatePcs ));
if (!checker)//check Switch {
iPriceOut.setText("150.0");
} else if (checker) //re-check Switch{
iPriceOut.setText("50.0");
}
}
// RadioButton2
if (radioButton1.isChecked()) {...}
// RadioButton3
if (radioButton1.isChecked()) {...}
basePcs = Integer.parseInt(iPieces.getText().toString());
baseSheet =
Integer.parseInt(iShit.getText().toString());
basePrice =
Double.parseDouble(iPriceOut.getText().toString());
}
我知道它可以被称为因为我已经在这里做了:(而且它有效)
public void onCheckedChanged(CompoundButton switchInput, boolean isChecked) {
switchInput.addTextChangedListener(size);
if (!isChecked) {
switchInput.setText("...");
radioButton1.setText("...");
radioButton2.setText("...");
radioButton3.setText("...");
} else {
switchInput.setText("...");
radioButton1.setText("...");
radioButton2.setText("...");
radioButton3.setText("...");
}
}
我是一个彻头彻尾的菜鸟,只会跟着教程为自己拼接这个应用程序。这是我必须解决的最后一个问题,因为其他的都或多或少地完成了。
感谢您的帮助
我认为问题在于您不能引用内部单击的对象,而是使用传递的视图
View.OnClickListener o1 = v ->{
iOpt1.addTextChangedListener(size); //error: Illegal Forward Reference
if (iOpt1.isChecked()) {
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
iOpt1.setChecked(true);
};
改为
View.OnClickListener o1 = v ->{
v.addTextChangedListener(size); <--change here //error: Illegal Forward Reference
if (v.isChecked()) { <--change here
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
v.setChecked(true);<--change here
};
在单击视图时使用 v
我只需要更新或刷新 TextWatcher
,方法是将它自己的内容重新分配回注入它的 EditText
,这将导致更新并将 运行 TextWatcher
因为基本上已经更新了。
View.OnClickListener o1 = v ->{
if (iOpt1.isChecked()) {
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
iOpt1.setChecked(true);
//SOLUTION: save the value on where the **TextWatcher** is injected then re-assign the same value you saved
refesher = iLength.getText().toString();
iLength.setText(refesher);
//This will cause an update to the **TextWatcher** and will run the code again
};
TextWatcher size = new TextWatcher() {...}
这里有更多上下文
public class PrintFragment extends Fragment{
EditText iLength, iHeight;
RadioButton iOpt1, iOpt2, iOpt3;
String refresher;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//inflate
iLength = V.findViewById(R.id.length);
iHeight = V.findViewById(R.id.height);
iOpt1 = V.findViewById(R.id.opt1);
iOpt2 = V.findViewById(R.id.opt2);
iOpt3 = V.findViewById(R.id.opt3);
iLength.addTextChangedListener(size);
iHeight.addTextChangedListener(size);
iOpt1.setOnClickListener(o1);
iOpt2.setOnClickListener(o2);
iOpt3.setOnClickListener(o3);
return;
}
View.OnClickListener o1 = v ->{
if (iOpt1.isChecked()) {
iOpt2.setChecked(false);
iOpt3.setChecked(false);
}
iOpt1.setChecked(true);
refesher = iLength.getText().toString();
iLength.setText(refesher);
};
TextWatcher size = new TextWatcher() {...}
}