我使用this question的答案将TextView点击的基本值放到单个TextView中。这个代码给出了每次点击的值,我想知道如何添加TextView的现有值。 注意:m1aa和m1ab是前一个活动中的数字变量,并通过意图接收到此活动。
UPDATE 这个问题应该是“你如何将字符串转换为小数。”
//Original non working code.
private int productTotal1;
private int productTotal2;
private int overallTotalproduct;
private TextView m1a,
private TextView m1aa,
//and then way down the page
productTotal1 = 0;
productTotal2 = 0;
overallTotalproduct = 0;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
textView1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal1++;
overallTotalproduct = productTotal1 + productTotal2;
textView1.setText(String.valueOf(productTotal1));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
textView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal2++;
overallTotalproduct = productTotal1 + productTotal2;
textView2.setText(String.valueOf(productTotal2));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
***更新***工作精细:) 在获得帮助和“思考”关于我写作的内容之后我最终还是......
//Working version
private double overallTotalproduct;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
final String stringm1ab = textView2.getText().toString();
final double intm1ab = Double.parseDouble(stringm1ab);
答案 0 :(得分:0)
您可以使用以下代码获取TextView
的值并将其转换为整数
String s = textView.getText().toString();
int n = Integer.parseInt(s);
如果字符串不包含可解析的整数,则会抛出NumberFormatException
。