如何将字符串更改为双精度?

时间:2012-06-06 23:32:00

标签: android

我创建了这个小程序,允许用户在EditText中输入数值。

  • 当他们第一次点击按钮时,应用程序应该在TextView
  • 中显示它
  • 之后,只有在大于之前的值
  • 时才应保存新值

请帮帮我。

public class BidActivity extends Activity implements OnClickListener {
    public TextView tt;
    public EditText textbo;
    public String total;
    public Button btnnn;
    public Double protein;
    double price = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textbo = (EditText)findViewById(R.id.txtbid);
        tt = (TextView)findViewById(R.id.textView1);

        btnnn = (Button)findViewById(R.id.btnbit);
        btnnn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String total = textbo.getText().toString();
        price = Double.parseDouble(textbo.getText().toString());

        //tt.setText("double: "+price);
        if(Double.parseDouble(textbo.getText().toString()) < price){
            tt.setText("double: "+price);
        }else{
            tt.setText("double: "+Double.parseDouble(textbo.getText().toString()));
        }   
    }
}

1 个答案:

答案 0 :(得分:1)

我猜你正在努力做到这一点:

double price = 0;
double savedPrice = 0.0;
...

@Override
public void onClick(View v) {
    price = Double.parseDouble(textbo.getText().toString());

    if(savedPrice < price){
        tt.setText("double: "+price);
        savedPrice = price;
    }
    // Otherwise leave tt as it is
}