0

我创建了这个小程序,允许用户在 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()));
        }   
    }
}
4

1 回答 1

1

I'm guessing you are trying to do this:

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
}
于 2012-06-06T23:39:01.893 回答