我正在使用Tablelayout,我正在实际添加5行。
每行有4列,在pos 0的Textview描述,2 edittext尝试&在pos 1和2处制作了视图,最后在pos 3处制作了TextView pct。
当用户在编辑文本尝试中提供输入时,我只想在textview pct中计算madeET / attemptET * 100和setText
。
这是目前为止的当前代码。所需结果仅发生在第6行的最终输入中。我在addTextChangedListener
以及madeET
setOnFocusChangeListener
private void setTableRows(){
String[] desc = new String[]{"Flat level putts","Uphill putts","Downhill putts","Left-to-right putts","Right-to-left putts"};
int totalputts = 0;
for (int i=0;i<desc.length;i++){
TableRow tr = new TableRow(this);
TextView descTV = new TextView(this);
descTV.setText(desc[i]);
descTV.setTag(i);
descTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
descTV.setPadding(5, 0, 0, 0);
tr.addView(descTV);
final TextView pctMade = new TextView(this);
pctMade.setGravity(Gravity.CENTER);
pctMade.setTag("pct"+i);
//pctMade.setText(String.valueOf(pctmadecalc));
pctMade.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
pctMade.setPadding(5, 0, 0, 0);
final EditText madeET = new EditText(this);
madeET.setInputType(InputType.TYPE_CLASS_NUMBER);
madeET.setTag("m"+i);
madeET.setFilters(new InputFilter[]{new GolfAppUtils.InputFilterMinMax("0","10")});
madeET.setGravity(Gravity.CENTER);
madeET.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
madeET.setPadding(5, 0, 0, 0);
final int finalI = i;
madeET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
/*if(s.length() > 0){
double pctmadecalc = Integer.parseInt(s.toString()) / PUTTS_PER_CATEGORY;
}*/
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
pctMade.setText(null);
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() > 0) {
//calcPct(finalI);
//float pctmadecalc = (((float)(Integer.parseInt(s.toString()))) / ((float) PUTTS_PER_CATEGORY)) * 100;
/*NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(4);
df.setRoundingMode(RoundingMode.DOWN);*/
//Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
//pctMade.setText(String.valueOf(String.format("%.2f",pctmadecalc)));
}
}
});
madeET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
calcPct();
}
});
EditText attemptET = new EditText(this);
attemptET.setInputType(InputType.TYPE_CLASS_NUMBER);
attemptET.setTag("a"+i);
attemptET.setFilters(new InputFilter[]{new GolfAppUtils.InputFilterMinMax("0","10")});
attemptET.setGravity(Gravity.CENTER);
attemptET.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
attemptET.setPadding(5, 0, 0, 0);
/*attemptET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
*//*if(s.length() > 0){
double pctmadecalc = Integer.parseInt(s.toString()) / PUTTS_PER_CATEGORY;
}*//*
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
pctMade.setText(null);
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() > 0) {
float pctmadecalc = (((float)(Integer.parseInt(s.toString()))) / ((float) PUTTS_PER_CATEGORY)) * 100;
*//*NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(4);
df.setRoundingMode(RoundingMode.DOWN);*//*
//Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
pctMade.setText(String.valueOf(String.format("%.2f",pctmadecalc)));
}
}
});*/
tr.addView(attemptET);
tr.addView(madeET);
tr.addView(pctMade);
table.addView(tr);
}
}
private void calcPct() {
EditText attempt = null,made = null;
TextView pct = null;
/*TableRow row = (TableRow) table.getChildAt(pos);
attempt = (EditText) row.findViewWithTag("a" + pos);
made = (EditText) row.findViewWithTag("m" + pos);
pct = (TextView) row.findViewWithTag("pct" + pos);*/
for(int i = 1, j = table.getChildCount(); i < j; i++) { //i=0 is header
TableRow row = (TableRow)table.getChildAt(i);
attempt = (EditText) row.getChildAt(1);
made = (EditText) row.getChildAt(2);
pct = (TextView) row.getChildAt(3);
}
if (!TextUtils.isEmpty(attempt.getText())) {
if (!TextUtils.isEmpty(made.getText())) {
float pctmadecalc = ((float) Integer.parseInt(made.getText().toString()) /
((float) Integer.parseInt(attempt.getText().toString()))) * 100;
//Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
pct.setText(String.valueOf(String.format("%.2f", pctmadecalc)));
}
}
}
提前感谢您的帮助
以下回应的解决方案
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
pctMade.setText("");
float pctmadecalc = ((float) Integer.parseInt(madeET.getText().toString()) /
((float) Integer.parseInt(attemptET.getText().toString()))) * 100;
//Toast.makeText(getBaseContext(), String.valueOf(pctmadecalc), Toast.LENGTH_SHORT).show();
pctMade.setText(String.valueOf(String.format("%.2f", pctmadecalc)));
//pctMade.setText(calcPct());
} else {
pctMade.setText("");
}
}
答案 0 :(得分:0)
这将是:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
pctMade.setText("");
pctMade.setText(s);
//etc..
}