我想從editText得到一個數字並在計算中使用,在我的代碼中沒有任何錯誤,但是我沒有得到我想要的,r的值是alwayas 1,這裏是代碼:如何從editText獲取數字?
public class Counter extends Activity {
Button add, sub;
TextView result;
EditText range;
double r;
public double c=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
add = (Button) this.findViewById(R.id.add);
sub = (Button) this.findViewById(R.id.sub);
result = (TextView) this.findViewById(R.id.result);
range = (EditText) this.findViewById(R.id.range);
try {
r = new Double(range.getText().toString());
} catch (NumberFormatException e) {
r = 1; // your default value
}
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
c=c+r;
result.setText(""+c);
}
});
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
c=c-r;
result.setText(""+c);
}
});
}
任何幫助?
你不能double轉換成一個類型,所以你不能寫'新Double.parseDouble ...' –
抱歉,它只是一個錯誤的我,看到我的編輯 – wael